2013-06-01 90 views
1

我一直在试图在我的本地Jenkins服务器上用Maven设置代码覆盖率。我的测试似乎没有被纳入报道报道。Cobertura代码覆盖率是错误的

我在我的pom.xml如下:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
     <artifactId>sonar-maven-plugin</artifactId> 
     <version>2.0</version> 
</plugin> 
<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
     <artifactId>cobertura-maven-plugin</artifactId> 
     <version>2.5.2</version> 
     <configuration> 
     <check> 
     <haltOnFailure>false</haltOnFailure> 
      <totalLineRate>85</totalLineRate> 
     </check> 
     <formats> 
      <format>xml</format> 
      <format>html</format> 
     </formats> 
     </configuration> 
     <executions> 
      <execution> 
       <id>clean</id> 
       <phase>pre-site</phase> 
       <goals> 
        <goal>clean</goal> 
       </goals> 
      </execution> 
      <execution> 
       <id>instrument</id> 
       <phase>site</phase> 
       <goals> 
        <goal>instrument</goal> 
        <goal>cobertura</goal> 
       </goals> 
      </execution> 
     </executions> 
</plugin> 

这里是我定义要运行

<plugin> 
    <!-- Separates the unit tests from the integration tests. --> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <configuration> 
     <skip>true</skip> 
     <trimStackTrace>false</trimStackTrace> 
     </configuration> 
    <executions> 
     <execution> 
      <id>unit-tests</id> 
      <phase>test</phase> 
      <goals> 
       <goal>test</goal> 
      </goals> 
      <configuration> 
       <skip>false</skip> 
       <includes> 
        <include>**/*Test*.java</include> 
       </includes> 
       <excludes> 
        <exclude>**/*IT.java</exclude> 
       </excludes> 
      </configuration> 
     </execution> 

     <execution> 
      <id>integration-tests</id> 
      <phase>integration-test</phase> 
      <goals> 
       <goal>test</goal> 
      </goals> 
      <configuration> 
       <skip>false</skip> 
       <includes> 
        <include>**/*IT.java</include> 
       </includes> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 

而且我詹金斯服务器上的测试,我有以下几点: 詹金斯配置: 声纳 声纳名称:声纳本地3.5.1 服务器网址:http://local host:9000

项目级别: 发布的Cobertura Coverate报告: XML报告模式:** /目标/ site.covertura/coverage.xml

但是当我运行无论是Maven的任务 “MVN的Cobertura:的Cobertura” 或查看声纳詹金斯报道,所有的覆盖率都在0%,这让我觉得我的单元和集成测试没有被看到。

这是我的pom分离单元和集成测试的问题吗?

回答

1

cobertura无法查看集成测试以查看代码覆盖率。

尝试看看code coverage by integration tests with sonarseparating integration and unit tests

作一个简短的故事:必须启动jacoco Maven插件谁将会设置两个属性,一个单元测试时,另一个用于它测试,并启动万无一失并与这些属性进行失败保护。

然后,您可以在独立的小程序中显示声纳中的结果。

在我自己的POM文件,你会为本身启用jacoco的一部分:“**/**目标”

  <properties> 
       <!-- Coverage and report --> 
       <jacoco.destFile.unit>${project.build.directory}/jacoco-unit.target</jacoco.destFile.unit> 
       <jacoco.destFile.it>${project.build.directory}/jacoco-it.target</jacoco.destFile.it> 
       <coverage.reports.dir>${project.build.directory}/coverage-reports</coverage.reports.dir> 
       <version.surefire>2.12.4</version.surefire> 

       <!-- Sonar --> 
       <sonar.jacoco.itReportPath>${jacoco.destFile.it}</sonar.jacoco.itReportPath> 
       <sonar.jacoco.reportPath>${jacoco.destFile.unit}</sonar.jacoco.reportPath> 
       <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin> 
       <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis> 
      </properties> 


      <build> 
      .... 
      <plugins> 
       <!-- Jacoco configuration --> 
       <plugin> 
        <groupId>org.jacoco</groupId> 
        <artifactId>jacoco-maven-plugin</artifactId> 
        <version>0.6.0.201210061924</version> 
        <executions> 
         <execution> 
          <goals> 
           <goal>prepare-agent</goal> 
          </goals> 
          <configuration> 
           <propertyName>jacoco.argLine.unit</propertyName> 
           <destFile>${jacoco.destFile.unit}</destFile> 
          </configuration> 
         </execution> 
         <execution> 
          <id>pre-integration-test</id> 
          <phase>pre-integration-test</phase> 
          <goals> 
           <goal>prepare-agent</goal> 
          </goals> 
          <configuration> 
           <propertyName>jacoco.argLine.it</propertyName> 
           <destFile>${jacoco.destFile.it}</destFile> 
          </configuration> 
         </execution> 
        </executions> 
       </plugin> 
       <!-- And in the surefire and failsafe plugins you need to enable jacoco like this -> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-surefire-plugin</artifactId> 
        <version>${version.surefire}</version> 
        <dependencies> 
         <dependency> 
          <groupId>org.apache.maven.surefire</groupId> 
          <artifactId>surefire-junit47</artifactId> 
          <version>${version.surefire}</version> 
         </dependency> 
        </dependencies> 
        <configuration> 
         <argLine>${jacoco.argLine.unit} 
          -Dfile.encoding=${project.build.sourceEncoding} -Xmx512m</argLine> 
         <forkMode>always</forkMode> 
         <parallel>classes</parallel> 
        </configuration> 
       </plugin--> 

       <!-- Play the /src/test/java/**IT.java with goal verify --> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-failsafe-plugin</artifactId> 
        <version>${version.surefire}</version> 
        <dependencies> 
         <dependency> 
          <groupId>org.apache.maven.surefire</groupId> 
          <artifactId>surefire-junit47</artifactId> 
          <version>${version.surefire}</version> 
         </dependency> 
        </dependencies> 
        <configuration> 
         <forkMode>pertest</forkMode> 
         <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory> 
         <argLine>${jacoco.argLine.it} 
          -Dfile.encoding=${project.build.sourceEncoding} -Xmx512m</argLine> 
        </configuration> 
        <executions> 
         <execution> 
          <id>integration-test</id> 
          <phase>integration-test</phase> 
          <goals> 
           <goal>integration-test</goal> 
          </goals> 
         </execution> 
         <execution> 
          <id>verify</id> 
          <phase>verify</phase> 
          <goals> 
           <goal>verify</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 

在詹金斯,则必须在设置了“postbuild” JaCoCo插件与 为exec文件路径

希望这会对你有所帮助。

+0

好吧,我已经用JaCoco替换了我的cobertura用法(使它看起来就像你的pom.xml示例),并尝试使用“mvn clean package”运行,但现在所有的测试都运行了,但没有为Jenkins生成.exec报告显示。 – sonoerin

+0

你是否在声纳中看到报告?是你的jacoco插件配置为“\ * \ */\ * \ *。dump”(用于exec文件的路径?) – twillouer

+0

不知道我做了什么,但它现在似乎正在工作。如果我看一下Sonar而不是Jenkins,我可以看到在代码覆盖范围内运行的测试(单元和集成)的数量。非常感谢你的帮助。您发布的帖子比我试图通过的许多教程更有帮助! – sonoerin

相关问题