在单独的 maven 模块中获取单元和集成测试的声纳正确覆盖

Get correct coverage in sonar for unit and integration tests in separate maven modules

我的项目设置很简单(所有源代码都可以在 github 获得):

parent
↳ 后端
↳ 客户
↳ integration-tests

并且在运行 maven 之后:

mci sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=12...9

我看到单元和集成测试对于声纳是可见的,但 IT 的覆盖范围不是。

对于 Intelij IDEA jacoco-it.exec 看起来不错:

我假设罪魁祸首在这里:

[INFO] Sensor JaCoCoSensor [java]
[INFO] No JaCoCo analysis of project coverage can be done since there is no class files.
[INFO] Sensor JaCoCoSensor [java] (done) | time=1ms

所以我做了小hack(简而言之:将所有源文件复制到integration-test模块):

    <properties>
        <sonar.sources>${basedir}/target/copied</sonar.sources>
    </properties>
    [...]
        <!-- hack to generate coverage reports -->
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>${maven-resources-plugin.version}</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${sonar.sources}</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${basedir}/../backend/src/main/java</directory>
                                <filtering>false</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>${sonar.sources}</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

但现在我所有的 类 啤酒都重复了(声纳显示 类 来自 ../target/copied 目录):

声纳版本:6.5
Jacoco maven 插件:0.7.5.201505241946(也尝试了最新的 0.7.9)

有什么想法我应该在这里做什么?

我主要使用 jacoco 进行覆盖。在开始之前,您需要准备好一些东西

  1. 启用并配置 surefire 插件
  2. 启用并配置 jacoco 插件

这是一次性配置,您将构建并忘记,我作为父项的项目之一启用了此功能。随意检查一下: https://github.com/slixes/parent/blob/master/pom.xml

看来我对这个问题有答案了:

  1. 未生成报告,因为 post-unit-test 执行处于错误的阶段。而不是 <phase>test</phase> 我现在有 <phase>verify</phase>

  2. post-integration-test 的目标是错误的。变化是从 <goal>report-integration</goal><goal>report-aggregate</goal>

    • This goal 允许我在测试与被测代码在不同的项目中时创建覆盖率报告。
  3. 新增属性: <jacoco.itReportPath>${project.basedir}/../integrations-tests/target/jacoco-it.exec</jacoco.itReportPath><sonar.jacoco.reportPaths>${jacoco.itReportPath},${project.build.directory}/jacoco-it.exec,${project.build.directory}/jacoco.exec</sonar.jacoco.reportPaths>

github

上可用的所有更改和更新项目