使用Cobertura和Jacoco进行代码覆盖 [英] Running code coverage with Cobertura and Jacoco

查看:109
本文介绍了使用Cobertura和Jacoco进行代码覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为Maven插件项目(使用调用程序插件进行集成测试)的Sonar中获取集成测试和单元测试的代码覆盖率报告时,我遇到了一些问题.

I have a bit of a problem getting code coverage reports for both Integration Tests and Unit Tests in Sonar for a Maven Plugin project (which uses invoker plugin for the integration tests).

我不能使用默认的Jacoco覆盖率工具进行单元测试,因为它们使用Powermock,这会导致使用该类的类的覆盖率为0%.另一方面,如果不使用Jacoco,我找不到一种可靠的方法来获得基于Groovy的集成测试的结果.

I can't use the default Jacoco coverage tool for the unit tests, as these use Powermock, which results in 0% coverage for classes using that. On the other hand, I can't find a reliable way to get results for the Groovy-based integration tests without using Jacoco.

所以我需要的是Cobertura制作单元测试报告,Jacoco制作集成测试报告,并且Sonar能够阅读很多东西.

So what I need is for Cobertura to produce a Unit Test report, Jacoco to produce an Integration Test report, and for Sonar to be able to read the lot.

我尝试在此处使用示例 https://github.com/Godin/sonar-experiments/tree/master/jacoco-examples/maven-invoker-plugin-example ,但取消了绑定到测试阶段的执行,但是我得到了单元测试声纳中-"的覆盖范围.我认为这样做的原因是,要使这种方法起作用,我需要专门指定Jacoco作为Sonar的核心报道工具.

I tried using the example here https://github.com/Godin/sonar-experiments/tree/master/jacoco-examples/maven-invoker-plugin-example but eliminating the executions bound to the test phase, but I then get a unit test coverage of '-' in Sonar. I think the reason for this is that to get this method to work, I need to speicify Jacoco as the core coverage tool for Sonar.

有什么办法解决这个问题吗?我的pom.xml如下:

Any ideas on a way round this? My pom.xml follows:

<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>

推荐答案

由于将声纳配置为

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

这意味着您要告诉声纳从 sonar.jacoco.itReportPath 重用 .如果没有现有报告,则没有任何报道.

This means you are telling sonar to reuse the existing report from sonar.jacoco.itReportPath. If there is no existing report, there is no any coverage.

在可能的情况下,我使用 cobertura 并重用 maven网站生成中的报告.作为以下配置属性:-

In may case, I use the cobertura and reuse its report from maven site generation. as the following configuration properties: -

<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>

我可以通过使用以下命令来获得重用:-

I can get the reuse by using the following command :-

mvn clean install site sonar:sonar

我可以使用以下命令来重现您的问题:-

I can reproduce your issue by using the following command :-

mvn clean install sonar:sonar

覆盖率为0%.由于 报告路径 中没有现有报告.

The coverage is 0%. Since there is no existing report at the report path.

然后在执行声纳之前,请确保指定了名为"jacoco-t.exec" 的报告.

Then please make sure that there is a report named "jacoco-t.exec" as specified before executing the sonar.

由于我不熟悉JaCoCo,也不知道哪个Maven阶段会生成报告文件.我建议执行如下命令:-

Since I'm not familiar with the JaCoCo and do not know which maven phase that produces thae report file. I would suggest to execute the command like the following:-

mvn clean test sonar:sonar

mvn clean install sonar:sonar

或与我的相同

mvn clean install site sonar:sonar

我希望这会有所帮助.

此致

Charlee Ch.

Charlee Ch.

这篇关于使用Cobertura和Jacoco进行代码覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆