我目前正在使用具有良好覆盖率的gradle multi模块java项目和sonarqube 6.2与sonarJava插件4.10.0.1026。我正在使用Gradle 4.0.1,sonarqube插件2.5和jacoco 0.7.9!代码是java 8.Gradle多模块项目中sonarqube 6.2的覆盖率计算错误
由于API驱动的开发,API测试在API项目中被编写为抽象测试,并从提供测试构造函数的实现项目中调用。
在分析sonarqube服务器上的项目时,实施项目的覆盖率被正确测量,但IMPL项目测试中包含的API项目覆盖率为0.0%。这些项目的覆盖率结果被忽略。
当简单地使用jacoco插件时,我能够得到相同的行为。之后做一些研究,我发现一个解决方案,以获得适当的jacoco报道:
task codeCoverageReport(type: JacocoReport) {
description "Creates a unified JaCoCo test report for the project."
// Gather execution data from all subprojects
// (change this if you e.g. want to calculate unit test/integration test
coverage separately)
executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
// Add all relevant sourcesets from the subprojects
subprojects.each {
sourceSets it.sourceSets.main
}
reports {
xml.enabled true
html.enabled true
html.destination file("${buildDir}/reports/jacoco")
csv.enabled false
}
}
// always run the tests before generating the report
codeCoverageReport.dependsOn {
subprojects*.test
}
我现在的结果如下:
JaCoCo:
- JaCoCo(codeCoverageReport任务)
- 73%指导覆盖范围
- 91%分支覆盖范围
- 声纳
- 43.1%线Coverage(仅〜30%线在计算中考虑!)
- 82.1%条件覆盖(仅〜覆盖20%的条件!)
因此声纳的覆盖率结果是不可用的。我读过一篇帖子,宣布“sonar.jacoco.reportPaths”参数以sonar 6.2开头,我认为java-analyzer 4.4或者某些东西。像那样。将此参数添加到我的gradle构建脚本时,脚本不再编译。当通过声纳项目管理将jacoco.exe文件添加到声纳时,没有任何更改。
如果能够管理声纳来计算正确的覆盖范围,那将是非常好的选择。
有一个[JacocoMerge](https://docs.gradle.org/4.0/dsl/org.gradle.testing.jacoco.tasks.JacocoMerge.html)任务可以合并将多个执行文件合并为一个 –
在接下来的几天内会再次尝试。第一次测试的最后几天没有正常工作。 –
有一个例子[这里](https://github.com/gradle/gradle/blob/28fa962a330ea6085ea324381eec4f31dcf92d1c/subprojects/jacoco/src/integTest/groovy/org/gradle/testing/jacoco/plugins/JacocoPluginMultiVersionIntegrationTest.groovy#L142)在Gradle测试中,它将单个项目中的多个“测试”任务合并并生成合并报告。您可以调整您的多项目设置 –