maven – 与Cobertura和Jacoco一起运行代码覆盖

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

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

所以我需要的是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>

最佳答案 因为你将声纳配置为

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

这意味着您要告诉sonar重用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

我希望这可能有所帮助.

问候,

Charlee Ch.

点赞