用Cobertura和Jacoco运行代码覆盖

对于Maven插件项目(使用调用者插件进行集成测试)中的集成测试和声纳单元测试获取代码覆盖率报告存在一些问题。

我不能使用默认的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>

由于您已将声纳配置为

 <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

我希望这可能会有所帮助。

问候,

Charlee Ch。

链接地址: http://www.djcxy.com/p/59905.html

上一篇: Running code coverage with Cobertura and Jacoco

下一篇: Deselect single select listbox