2013-02-28 106 views
3

對於Maven插件項目(使用調用者插件進行集成測試)中的集成測試和聲納單元測試獲取代碼覆蓋率報告存在一些問題。使用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 2013-06-24 15:16:04

回答

2

既然你已經配置聲納作爲

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

這意味着你是從sonar.jacoco告訴聲納重用現有的報表。 itReportPath。如果沒有現有報告,則沒有任何報道。

在可能的情況下,我使用cobertura並重新使用它的報告maven網站生成。如下面的配置屬性: -

<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並且不知道哪個maven階段產生了報告文件。我建議像下面執行命令: -

mvn clean test sonar:sonar 

mvn clean install sonar:sonar 

或和我一樣

mvn clean install site sonar:sonar 

我希望這可以幫助。

Regards,

Charlee Ch。

相關問題