我有多个模块的 maven 设置,设置看起来像这样
root module
- domain module
- repository module
- service module
- controllers module
Jacoco 从单元测试中正确生成了测试覆盖率,并且 Sonar 显示了正确的百分比(假设为 20%)。 Surefire 用于单元测试。
对于更棘手的集成测试,我们使用故障安全和集成测试生成 jacoco-it.exec
文件,该文件由 Sonar 扫描。我的问题是集成测试位于 controllers module
中,它只显示集成测试对 Controller 内部类的测试覆盖率,而不显示另一个模块(如服务模块)中的类。由于集成测试的总体测试覆盖率从 35+% 增加到 21% 左右。
如果集成测试在controllers module
?
供引用,这是相关设置
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
...
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco-it.exec</destFile>
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
</executions>
</plugin>
我使用 mvn verify
运行测试,使用 mvn sonar:sonar
运行扫描器
最佳答案
您可以通过将覆盖率报告全部写入同一个目标文件来汇总它们。
例如,目前我们只在多模块项目中覆盖单元测试,所以我们的父 pom 包含:
<properties>
...
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
...
</properties>
和
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<append>true</append>
</configuration>
<inherited>true</inherited>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
这会生成所有模块的单一汇总报告。
我希望类似的模式可以应用于您的 jacoco IT 配置。
关于java - 用于集成测试的多模块 Maven jacoco 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53614520/