在Sonarqube集成和单元测试报告与Powermock配置jacoco

时间:2019-02-01 21:15:47

标签: maven sonarqube jacoco instrumentation jacoco-maven-plugin

我使用Sonarqube跟踪两个单元和集成测试覆盖率的多模块Maven项目的。

这是父pom.xml中的现有配置文件,用于在进行更改之前在本地生成Sonarqube报告:

配置文件在本地生成Sonarqube所有单元测试覆盖率

<profiles>
  <profile>
    <id>coverage</id>
    <properties>
       <maven.compiler.source>1.8</maven.compiler.source>
       <maven.compiler.target>1.8</maven.compiler.target> 
       <sonar.jacoco.reportPaths>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPaths>
       <sonar.projectName>plan-advantage-serverless-${project.artifactId}</sonar.projectName>
       <sonar.projectKey>${project.groupId}-MPA-${project.artifactId}</sonar.projectKey>
       <sonar.exclusions>file:**/generated-sources/**,**/*Model.java,**/models/**/*</sonar.exclusions>
       <sonar.test.exclusions>**/test/*</sonar.test.exclusions>
       <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
       <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
       <sonar.language>java</sonar.language>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
    <build>
      <plugins>
        <plugin>
          <groupId>org.jacoco</groupId>
          <artifactId>jacoco-maven-plugin</artifactId>
          <version>0.8.2</version>
          <configuration>
            <append>true</append>
            <excludes>
              <exclude>**/test/*</exclude>
            </excludes>
          </configuration>
          <executions>
            <execution>
              <id>default-instrument</id>
              <goals>
                <goal>instrument</goal>
              </goals>
            </execution>
            <execution>
              <id>default-restore-instrumented-classes</id>
              <goals>
                <goal>restore-instrumented-classes</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.3</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <showWarnings>true</showWarnings>
            <compilerArgs>
              <arg>-Xlint:all</arg>
              <arg>-Xlint:-processing</arg>
            </compilerArgs>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.0</version>
          <configuration>
            <argLine>-XX:-UseSplitVerifier</argLine>
            <systemPropertyVariables>
              <jacoco-agent.destfile>${sonar.jacoco.reportPaths}</jacoco-agent.destfile>
            </systemPropertyVariables>
          </configuration>

        <dependencies>
          <!-- needed for powermock to run correctly with surefire-->
          <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>2.22.0</version>
          </dependency>
        </dependencies>
      </plugin>
      <plugin>
        <groupId>org.sonarsource.scanner.maven</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <version>3.5.0.1254</version>
      </plugin>
    </plugins>
  </build>
</profile>

这是产生期望的测试覆盖面(没有集成测试)在 Sonarqube本地当我运行mvn clean install -P coverage sonar:sonar

到目前为止,我已经能够使用以下新增功能来添加集成范围作为概念证明 到母体的pom.xml:

pom.xml的,其包括集成的测试覆盖率在Sonarqube但排除一些单元测试

<properties>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <jacoco.version>0.7.9</jacoco.version> . <sonar.jacoco.reportPaths>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPaths>
  <sonar.jacoco.itReportPath>${project.basedir}/../target/jacoco-it.exec</sonar.jacoco.itReportPath>
  <sonar.language>java</sonar.language>
  <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
</properties>
...
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-failsafe-plugin</artifactId>
      <version>3.0.0-M3</version>
      <goals>
        <goal>integration-test</goal>
          <goal>verify</goal>
        </goals>
        <dependencies>
          <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>3.0.0-M3</version>
          </dependency>
        </dependencies>
    </plugin>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>${jacoco.version}</version>
      <executions>
          <execution>
              <id>agent-for-ut</id>
              <goals>
                  <goal>prepare-agent</goal>
              </goals>
              <configuration>
                  <append>true</append>
                  <destFile>${sonar.jacoco.reportPaths}</destFile>
              </configuration>
          </execution>
          <execution>
              <id>agent-for-it</id>
              <phase>package</phase>
              <goals>
                  <goal>prepare-agent-integration</goal>
              </goals>
              <configuration>
                  <append>true</append>
                  <destFile>${sonar.jacoco.itReportPath}</destFile>
              </configuration>
          </execution>
      </executions>
    </plugin>
  </plugins>
</build>

这是受发现的here示例的启发。

然而,当我与命令运行它mvn clean install failsafe:integration-test sonar:sonar,它会导致一些其先前被覆盖在Sonarqube输出不会显示单元测试的。我相信,准备代理,并准备代理integrationgoals正在使用即时检测仪器。据JaCo​​Co的文档,在即时在使用PowerMock(其中我的项目是利用),所以我们必须使用离线仪器仪表的仪表JaCoCo是不可能的。

我查看了这个example使用脱机检测的情况,并使用了以下pom.xml和命令mvn clean install test sonar:sonar

这未能构建由于NoClassDefFound错误的亲的pom.xml

<build>
...
  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco.version}</version>
    <executions>
      <execution>
        <id>default-instrument</id>
        <goals>
          <goal>instrument</goal>
        </goals>
      </execution>
      <execution>
        <id>default-restore-instrumented-classes</id>
        <goals>
          <goal>restore-instrumented-classes</goal>
        </goals>
      </execution>
    </executions> 
  </plugin>
</build>

和这里的所产生的误差:enter image description here

对适当的pom.xml配置有什么想法,以使离线检测能够集成并在Sonarqube中显示单元测试范围?

1 个答案:

答案 0 :(得分:0)

我知道这个问题已有10个月的历史了,但是对于遇到此问题的其他人,您需要将此问题添加到pom.xml文件中的依赖项中。

<dependency>
    <groupId>org.jacoco</groupId>
    <artifactId>org.jacoco.agent</artifactId>
    <classifier>runtime</classifier>
    <version>${jacoco.version}</version>
    <scope>test</scope>
</dependency>