JaCoCo Maven 插件的 SonarQube 中的集成测试覆盖率显示为 0% [英] Integration Test Coverage in SonarQube from JaCoCo Maven plug-in showing 0%

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

问题描述

我们有一个多模块多语言 maven java 项目,使用 jacoco 进行覆盖率分析.模块的主要部分是带有 REST API 的后端(Java 代码),我们的 webapp 模块包含前端(AngularJS)和 java 中的集成测试.我们的 Jacoco-IT.exec 文件包含大约 100Kb 的数据,因此我们猜测可能会收集一些用于集成测试的 Coverage 数据.尽管如此,我们在 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代码(我们曾经有一个单语言配置,但与覆盖率数据相关的结果没有差异)

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>

我们来自 webapp 的 POM 和集成测试:

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>

您可以在我们的父 POM 中看到,我们将 itReportPath 配置为所有模块的同一目录中.因此,当 Sonar 分析所有模块时,它总是读取相同的覆盖率数据,并应将它们与当前模块的二进制文件中的调试信息对齐.在 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:

对于所有 java 模块,我们得到以下几行:

for all java modules we get the folloing lines:

    [INFO] [17:30:42.992] Sensor SurefireSensor...
    [INFO] [17:30:42.992] parsing gitairport-core
est	argetsurefire-reports
    [INFO] [17:30:43.009] Sensor SurefireSensor done: 17 ms
    [INFO] [17:30:43.009] Sensor JaCoCoItSensor...
    [INFO] [17:30:43.010] Analysing gitairport-core
est..	argetjacoco-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 gitairport-core
est	argetsonarjacoco-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?

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

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.

我们在网络上发现了很多被贬低的信息(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:

推荐答案

我使用了类似的设置,并且在以下配置下一切正常:

Im using a similar setup and everything works perfectly with the following configuration:

但是 maven-surefire-plugin 最初会引起麻烦.这就是为什么我必须在 argLine 标记中添加 @{argLine} 的原因,因为 jacoco-maven-plugin:prepare-agent 目标在 maven 之前执行-surefire 插件.(参见https://stackoverflow.com/a/25774988)

However the maven-surefire-plugin was causing troubles initially. Thats why I had to add the @{argLine} in the argLine tag, since the jacoco-maven-plugin:prepare-agent goal is executed before the maven-surefire-plugin. (see https://stackoverflow.com/a/25774988)

希望我能帮上忙

<plugins>
    <plugin>
        <groupId>org.sonarsource.scanner.maven</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <version>3.4.0.905</version>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>

        <executions>
            <execution>
                <phase>test</phase>
            </execution>
        </executions>
        <configuration>
            <!-- the @{argLine} is needed for proper jacoco execution -->
            <argLine>@{argLine} -Xmx256m</argLine>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.9</version>
        <executions>
            <execution>
                <id>jacoco-initialize</id>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
            </execution>
            <!-- generate fancy html report -->
            <execution>
                <id>jacoco-site</id>
                <phase>package</phase>
                <goals>
                    <goal>report</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>

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

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