在声纳中获得正确的覆盖率,以在单独的Maven模块中进行单元和集成测试

Paw*_*wel 6 java integration-testing code-coverage maven sonarqube

我的项目设置很简单(所有资源都可以在github上找到):

父母
?后端
?客户
?整合测试

在运行Maven之后:

mci sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=12...9
Run Code Online (Sandbox Code Playgroud)

我看到声纳可以看到单元测试和集成测试,但是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
Run Code Online (Sandbox Code Playgroud)

所以我做了一个小技巧(总之:将所有源文件复制到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>
Run Code Online (Sandbox Code Playgroud)

但是现在我所有的课程都重复了(声纳还显示了../target/copieddir中的课程): 声纳覆盖范围重复

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

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

Paw*_*wel 5

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

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

  2. 我有一个错误的目标post-integration-test。变化是从<goal>report-integration</goal><goal>report-aggregate</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>

所有这些 chages 和更新项目都可以在github 上找到