我已经看了一段时间关于 stackoverflow 的不同文章和答案,但我还没有找到适合我的案例的有效解决方案。我对 jacoco、maven 和 sonar 如何共同创建报告的理解肯定有问题,所以我要寻求帮助。
我有一个具有以下结构的多模块 maven 项目(简化了一点):
root-folder/
├── module1/
│ ├── module1_1/
│ ├── module2_2/
├── module2/
│ ├── module2_1/
│ └── module2_2/
├── module3/
│ ├── module3_1/
│ ├── module3_1_1/
│ └── module3_2/
│ └── module3_3/
│ └── module3_4/
├── parent/
├── itest/
│ ├── itest_helper1/
│ └── itest_helper2/
│ └── actual_tests/
├── module4/
请允许我扩大一点。父模块只是一个包含整个依赖项及其版本的 pom。这个 pom 被用作 level1 的每个其他模块的父级(直接在 root 下)。例如,模块 3_1_1 具有父模块 3_1/pom,而后者又具有父模块/pom。我还有一个我排除的功能模块,因为它用于将模块公开给其他项目,这在我的情况下没有用。
大多数模块(但不是全部)都使用 junit 框架进行单元测试。集成测试位于一个单独的模块中,该模块具有一些辅助子模块和一个具有实际 ITests 类的子模块。我的测试涵盖了我项目中所有的代码,因此他们测试了所有其他模块的代码。
我的根 pom 也有一些内部框架作为父级,它基本上是一个 osgi 容器、Web 服务并处理第 3 方依赖项。
我有声纳 5.6 版,但未能为集成测试创建报告。我也希望它们被合并,这样我就可以向下钻取并查看每个模块/类的测试覆盖率。我尝试了不同的选项和设置,我最近的尝试在我的根文件夹/pom.xml 中有以下内容。
<!-- properties section-->
<properties>
<jacoco.version>0.7.2.201409121644</jacoco.version>
<sonar.language>java</sonar.language>
<sonar.sourceEncoding>UTF-8</sonar.sourceEncoding>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<jacoco.outputDir>${project.build.directory}</jacoco.outputDir>
<jacoco.out.ut.file>jacoco-ut.exec</jacoco.out.ut.file>
<sonar.jacoco.reportPath>${jacoco.outputDir}/${jacoco.out.ut.file}</sonar.jacoco.reportPath>
<jacoco.out.it.file>jacoco-it.exec</jacoco.out.it.file>
<sonar.jacoco.itReportPath>${jacoco.outputDir}/${jacoco.out.it.file}</sonar.jacoco.itReportPath>
</properties>
<!-- plugins section-->
<profiles>
<profile>
<id>coverage</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${jacoco.agent.ut.arg}</argLine>
<!-- test failure ignore -->
<testFailureIgnore>true</testFailureIgnore>
<includes>
<include>**/*Test*</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<argLine>-Xmx1024m -XX:MaxPermSize=256m ${jacoco.agent.it.arg}
</argLine>
<!-- Let's put failsafe reports with surefire to have access to tests
failures/success reports in sonar -->
<reportsDirectory>${project.build.directory}/surefire-reports
</reportsDirectory>
<includes>
<include>**/*IT*</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<!-- Prepares a variable, jacoco.agent.ut.arg, that contains the info
to be passed to the JVM hosting the code being tested. -->
<execution>
<id>prepare-ut-agent</id>
<phase>process-test-classes</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<propertyName>jacoco.agent.ut.arg</propertyName>
<append>true</append>
</configuration>
</execution>
<!-- Prepares a variable, jacoco.agent.it.arg, that contains the info
to be passed to the JVM hosting the code being tested. -->
<execution>
<id>prepare-it-agent</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${sonar.jacoco.itReportPath}</destFile>
<propertyName>jacoco.agent.it.arg</propertyName>
<append>true</append>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<!-- Integraton tests -->
<profile>
<id>run-its</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
我使用 mvn -Pcoverage,run-its clean install sonar:sonar 运行我的项目。我的单元测试似乎没问题,覆盖率和成功率都很高。老实说,我只对集成测试感兴趣,但是单元测试很容易设置,所以我也添加了它们。对于我的集成测试报告,它实际上报告了我的 itest 模块的覆盖率,这是唯一不应该报告的模块。我希望我的测试报告我的整个项目的覆盖率。
这是运行在我的 itest 模块上的声纳的 jacoco 部分:
[WARNING] You are not using the latest JaCoCo binary format version, please consider upgrading to latest JaCoCo version.
[INFO] Analysing D:\Home\project\root\itest\testcase\target\jacoco-it.exec
[WARNING] You are not using the latest JaCoCo binary format version, please consider upgrading to latest JaCoCo version.
[INFO] Analysing D:\Home\project\root\itest\testcase\target\sonar\jacoco-overall.exec
[INFO] No information about coverage per test.
此外,由于我的插件配置位于根 pom 中,它会尝试分析我所有模块的报告(包括没有单元测试的模块)并在声纳运行日志中引发错误,但我认为它不是问题。非常感谢任何帮助,如果您需要,请询问更多详细信息。谢谢。