集成测试覆盖范围在SonarQube从JaCoCo Maven插件显示0% [英] Integration Test Coverage in SonarQube from JaCoCo Maven plug-in showing 0%

查看:740
本文介绍了集成测试覆盖范围在SonarQube从JaCoCo Maven插件显示0%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个多模块多语言maven java项目,使用jacoco进行覆盖分析。
模块的主要部分是带有REST API的后端(Java代码),我们的webapp模块包含前端(AngularJS)和java中的Integration-Tests。
我们的Jacoco-IT.exec文件包含大约100Kb的数据,因此我们猜测可以收集一些集成测试的覆盖数据。然而,我们不能在SonarQube中看到任何覆盖(使用版本5.0和4.5)

We have a multi-module multi-language maven java project with coverage analysis with jacoco. The main part of the modules is backend (Java code) with a REST API and our webapp module contains the frontend (AngularJS) and the Integration-Tests in java. Our Jacoco-IT.exec file contains around 100Kb of data so we guess some Coverage data for the Integration tests could get collected. Nevertheless we cant see any coverage in SonarQube (using Version 5.0 and 4.5)

我们运行构建项目并运行集成测试

We run build the Project and run the integration tests with

mvn clean install

资料

mvn sonar:sonar

项目被配置为多语言,但我们只是分析java代码(我们以前有一个单一的语言配置,但与coverage数据相关的结果没有区别)

The Project is configured as multi language, but we just analyse the java code yet (we used to have a monolanguage configuration but there was no difference in the results related to the coverage data)

我们的父POM:

<properties>
    <hibernate.version>4.2.2.Final</hibernate.version>
    <spring.version>3.2.3.RELEASE</spring.version>
    <resteasy.version>3.0.4.Final</resteasy.version>
    <cucumber.version>1.1.3</cucumber.version>
    <selenium-java.version>2.33.0</selenium-java.version>
    <!-- Sonar -->
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
    <sonar.jacoco.itReportPath>${project.basedir}/../target/jacoco-it.exec</sonar.jacoco.itReportPath>
    <jacoco.dataFile>${project.basedir}/../target/jacoco-it.exec</jacoco.dataFile>
</properties>
<build>
   <plugins>
       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <debug>true</debug>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>

            <executions>
                <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-prepare-agent-integration</id>
                    <goals>
                        <goal>prepare-agent-integration</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-report</id>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-report-integration</id>
                    <goals>
                        <goal>report-integration</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
        </plugin>
    </plugins>
    <pluginManagement>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.2.201409121644</version>
            </plugin>
    </pluginMangement>
</build>

我们的POM来自webapp,带有集成测试:

Our POM from the webapp with the Integration Tests:

<properties>
    <sonar.sources>src/main</sonar.sources>
</properties>
<build>
    <finalName>web</finalName>
    <plugins>
        <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>8.1.7.v20120910</version>
        <configuration>...</configuration>
        <executions>...</executions>
    </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <configuration>
                <!-- The destination file for the code coverage report has to be set to the same value
                in the parent pom and in each module pom. Then JaCoCo will add up information in
                the same report, so that, it will give the cross-module code coverage. -->
                <destFile>${project.basedir}/../target/jacoco-it.exec</destFile>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.16</version>
            <executions>
                <execution>
                    <id>default</id>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>


$ b < 。因此,当Sonar分析所有模块时,它总是读取相同的coverage数据,并将它们与当前模块的二进制文件中的调试信息对齐。
在maven(mvn sonar:sonar)的分析阶段,我们得到一些消息,我们不知道他们是否是一个问题:

As you can see in our parent POM we configured the itReportPath to be in the same directory for all Modules. So when Sonar analyses all modules it reads always the same coverage data and should align them with the debug information from the binaries of the current module. During the analyse phase of maven (mvn sonar:sonar) we get some messages where we are not sure if they are a problem:

我们得到下面的行:

    [INFO] [17:30:42.992] Sensor SurefireSensor...
    [INFO] [17:30:42.992] parsing \git\airport-core\rest\target\surefire-reports
    [INFO] [17:30:43.009] Sensor SurefireSensor done: 17 ms
    [INFO] [17:30:43.009] Sensor JaCoCoItSensor...
    [INFO] [17:30:43.010] Analysing \git\airport-core\rest\..\target\jacoco-it.exec
    [INFO] [17:30:43.018] No information about coverage per test.
    [INFO] [17:30:43.018] Sensor JaCoCoItSensor done: 9 ms
    [INFO] [17:30:43.018] Sensor JaCoCoOverallSensor...
    [INFO] [17:30:43.027] Analysing \git\airport-core\rest\target\sonar\jacoco-overall.exec
    [INFO] [17:30:43.046] No information about coverage per test.
    [INFO] [17:30:43.046] Sensor JaCoCoOverallSensor done: 28 ms

没有关于每次测试的覆盖率的信息。一个问题?好的,我们不知道哪个测试导致覆盖,但是我们应该在SonarQube中得到一个不同于0%

Is "No information about coverage per test." a Problem? Ok, we dont know which test caused the coverage, but we should get a different value in SonarQube than 0%

的值,对于我们的webapp模块,它不包含任何java源/类除了集成测试源本身,我们得到以下行:

and for our webapp module, which does not contain any java sources/classes except for the integration test sources itself, we get the following lines:

    [INFO] [17:30:48.370] Sensor JaCoCoItSensor...
    [INFO] [17:30:48.370] No JaCoCo analysis of project coverage can be done since there is no class files.
    [INFO] [17:30:48.370] Sensor JaCoCoItSensor done: 0 ms

应该不是一个问题,因为在这个模块中没有类。
这个观察是否正确?

This shouldn't be a problem, as there are no classes in this module. Is this observation correct?

在Main部分,我们的配置基于官方SonarCube演示配置(https://github.com/SonarSource/ sonar-examples / tree / master / projects / languages / java / code-coverage / combined%20ut-it / combined-ut-it-multimodule-maven-jacoco ),我们刚刚添加了prepare-集成目标。当然更改为多语言项目。

To the Main part our configruation bases on the official SonarCube demo configuration (https://github.com/SonarSource/sonar-examples/tree/master/projects/languages/java/code-coverage/combined%20ut-it/combined-ut-it-multimodule-maven-jacoco), we just added the "prepare-agent-integration" goal. And of course changed it to a multi-language Project.

我们在网络上发现了很多的depricated信息(argLine设置等),没有改变任何东西

We found a lot of depricated information on the web (argLine settings and so on) which didn't change anything for us.

我们使用的文档和教程的链接:

Links to the Documentation and Tutorials we used:

  • http://docs.sonarqube.org/display/SONAR/Code+Coverage+by+Integration+Tests+for+Java+Project
  • http://www.sonarqube.org/measure-coverage-by-integration-tests-with-sonar-updated/

推荐答案

与Jacoco和Maven战斗了很长时间,只有这样为我工作:

After fighting very long time with Jacoco and Maven only this way it is working for me:


  1. 在pom.xml中创建个人资料

  1. Create profile in pom.xml

<profile>
  <id>coverage-per-test</id>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
         <!-- disable parallel execution so that JaCoCo listener can properly work -->
          <parallel>none</parallel>
          <perCoreThreadCount>false</perCoreThreadCount>
          <forkCount>1</forkCount>
          <properties>
            <property>
              <name>listener</name>
              <value>org.sonar.java.jacoco.JUnitListener</value>
            </property>
          </properties>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>org.sonarsource.java</groupId>
      <artifactId>sonar-jacoco-listeners</artifactId>
      <version>3.10</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</profile>


  • 将maven surefire插件更新到最新版本

  • Update maven surefire plugin to latest version

    执行命令:

    mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Pcoverage-per-test -Dmaven.test.failure.ignore=true
    
    mvn sonar:sonar
    


  • github sonar example

    这篇关于集成测试覆盖范围在SonarQube从JaCoCo Maven插件显示0%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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