2013-02-28 3 views
3

통합 테스트를위한 호출자 플러그인을 사용하는 Maven Plugin 프로젝트의 Sonar에서 통합 테스트 및 유닛 테스트에 대한 코드 커버리지 보고서를받는 데 약간의 문제가 있습니다.Cobertura 및 Jacoco 코드 실행 범위

단위 테스트에 기본 Jacoco 커버리지 도구를 사용할 수 없습니다.이 도구는 Powermock을 사용하므로 클래스를 사용하는 클래스에 대해 0 % 적용 범위가됩니다. 반면 Jacoco를 사용하지 않고 Groovy 기반 통합 테스트의 결과를 얻을 수있는 확실한 방법을 찾을 수 없습니다.

그래서 내가 필요로하는 것은 Cobertura가 단위 테스트 보고서를 작성하고, Jacoco가 통합 테스트 보고서를 작성하고, Sonar가 많은 것을 읽을 수 있도록하는 것입니다.

여기서 예제 https://github.com/Godin/sonar-experiments/tree/master/jacoco-examples/maven-invoker-plugin-example을 사용해 보았지만 테스트 단계에 연결된 실행은 제거했지만, 나는 Sonar에서 '-'의 단위 테스트 적용 범위를 얻습니다. 그 이유는이 방법을 작동시키기 위해서는 Jacoco를 Sonar의 핵심 커버리지 도구로 지정해야한다는 것입니다.

방법에 대한 아이디어가 있습니까? 내 pom.xml 파일은 다음과 같습니다

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.acme.myproj.plugins</groupId> 
    <artifactId>slice2java-maven-plugin</artifactId> 
    <version>0.1-SNAPSHOT</version> 
    <packaging>maven-plugin</packaging> 

    <name>Slice2Java Maven Plugin</name> 

    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <sonar.exclusions>**/generated*/*.java</sonar.exclusions> 
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin> 
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis> 
    <sonar.jacoco.itReportPath>${project.basedir}/target/jacoco-it.exec</sonar.jacoco.itReportPath> 
    </properties> 

    <dependencies> 
    <dependency> 
     <groupId>org.apache.maven</groupId> 
     <artifactId>maven-plugin-api</artifactId> 
     <version>2.0</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.maven.plugin-tools</groupId> 
     <artifactId>maven-plugin-annotations</artifactId> 
     <version>3.2</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.codehaus.plexus</groupId> 
     <artifactId>plexus-utils</artifactId> 
     <version>3.0.8</version> 
    </dependency> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.11</version> 
     <scope>test</scope> 
    </dependency> 
     <dependency> 
      <groupId>org.mockito</groupId> 
      <artifactId>mockito-all</artifactId> 
      <version>1.9.5</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.powermock</groupId> 
      <artifactId>powermock-core</artifactId> 
      <version>1.5</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.powermock</groupId> 
      <artifactId>powermock-module-junit4</artifactId> 
      <version>1.5</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.powermock</groupId> 
      <artifactId>powermock-api-mockito</artifactId> 
      <version>1.5</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-plugin-plugin</artifactId> 
     <version>3.2</version> 
     <configuration> 
      <goalPrefix>slice2java</goalPrefix> 
      <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound> 
     </configuration> 
     <executions> 
      <execution> 
      <id>mojo-descriptor</id> 
      <goals> 
       <goal>descriptor</goal> 
      </goals> 
      </execution> 
      <execution> 
      <id>help-goal</id> 
      <goals> 
       <goal>helpmojo</goal> 
      </goals> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 
    <profiles> 
    <profile> 
     <id>run-its</id> 
     <build> 
     <plugins> 
      <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-invoker-plugin</artifactId> 
      <version>1.8</version> 
      <configuration> 
       <debug>true</debug> 
       <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo> 
       <pomIncludes> 
       <pomInclude>*/pom.xml</pomInclude> 
       </pomIncludes> 
       <postBuildHookScript>verify</postBuildHookScript> 
       <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath> 
       <settingsFile>src/it/settings.xml</settingsFile> 
       <goals> 
       <goal>clean</goal> 
       <goal>test-compile</goal> 
       </goals> 
      </configuration> 
      <executions> 
       <execution> 
       <id>integration-test</id> 
       <goals> 
        <goal>install</goal> 
        <goal>integration-test</goal> 
        <goal>verify</goal> 
       </goals> 
       </execution> 
      </executions> 
      </plugin> 
      <plugin> 
       <groupId>org.jacoco</groupId> 
       <artifactId>jacoco-maven-plugin</artifactId> 
       <version>0.5.3.201107060350</version> 
       <configuration> 
        <includes>com.acme.*</includes> 
       </configuration> 
       <executions> 
        <execution> 
         <id>pre-integration-test</id> 
         <phase>pre-integration-test</phase> 
         <goals> 
          <goal>prepare-agent</goal> 
         </goals> 
         <configuration> 
          <destFile>${project.build.directory}/jacoco-it.exec</destFile> 
          <propertyName>invoker.mavenOpts</propertyName> 
         </configuration> 
        </execution> 
        <execution> 
         <id>post-integration-test</id> 
         <phase>post-integration-test</phase> 
         <goals> 
          <goal>report</goal> 
         </goals> 
         <configuration> 
          <dataFile>${project.build.directory}/jacoco-it.exec</dataFile> 
          <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
     </build> 
    </profile> 
    </profiles> 

</project> 
+0

나는 내 프로젝트에서 같은 문제를 겪고있다. 코드 커버리지에 문제가있는 유일한 파일은 @PrepareForTest 주석을 사용하는 파일이라는 것을 알았습니다. – HardcoreBro

답변

2

당신이

<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis> 
<sonar.jacoco.itReportPath> 
    ${project.basedir}/target/jacoco-t.exec 
</sonar.jacoco.itReportPath> 

와 수중 음파 탐지기를 구성했습니다 때문에 당신이 sonar.jacoco에서 재사용 기존 보고서에 수중 음파 탐지기를 말하는 의미합니다. itReportPath. 기존 보고서가 없으면 적용 범위가 없습니다.

5 월의 경우, 나는 cobertura를 사용하고 받는다는 사이트 생성에서 보고서를 다시 사용합니다. 다음 구성 속성으로 : -

<sonar.java.coveragePlugin>cobertura</sonar.java.coveragePlugin> 
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis> 
<sonar.surefire.reportsPath> 
    ${project.build.directory}/surefire-reports 
</sonar.surefire.reportsPath> 
<sonar.cobertura.reportPath> 
    ${project.build.directory}/site/cobertura/coverage.xml 
</sonar.cobertura.reportPath> 

나는 다음과 같은 명령을 사용하여 재사용을 얻을 수 있습니다 : -

mvn clean install site sonar:sonar 

나는 다음과 같은 명령을 사용하여 문제를 재현 할 수 있습니다 : -

mvn clean install sonar:sonar 

적용 범위는 0 %입니다. 보고서 경로에 기존 보고서가 없으므로

그런 다음 수중 음파 탐지기를 실행하기 전에 지정된대로 "jacoco-t.exec"이라는 보고서가 있는지 확인하십시오.

저는 JaCoCo에 익숙하지 않아서 thae 보고서 파일을 생성하는 maven 단계를 알지 못합니다.나는 다음과 같은 명령을 실행하는 것이 좋습니다 것입니다 : -

mvn clean test sonar:sonar 

또는

mvn clean install sonar:sonar 

또는 같은 내

mvn clean install site sonar:sonar 

내가이 도움이 될 수 있습니다 희망한다.

감사합니다,

Charlee Ch.

관련 문제