我有一个包含两个模块的项目,一个包含代码(submodule1
),另一个包含单元测试(submodule2
)。在submodule1
上运行覆盖测试会提供0%的覆盖率结果,因为这些方法应该在submodule2
中进行测试。然而,在submodule2
上运行覆盖测试仅返回该模块中定义的类的覆盖范围。
我一直在努力创建项目范围的覆盖范围,我发现了以下内容:
subprojects {
apply plugin: 'jacoco'
def coverageSourceDirs = [
'src/main/java'
]
jacocoTestReport {
additionalSourceDirs = files(sourceSets.main.allSource.srcDirs)
sourceDirectories = files(sourceSets.main.allSource.srcDirs)
classDirectories = files(sourceSets.main.output)
reports {
html.enabled = true
xml.enabled = true
csv.enabled = false
}
}
test.finalizedBy(project.tasks.jacocoTestReport)
}
task jacocoRootReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) {
additionalSourceDirs = files(subprojects.sourceSets.main.allSource.srcDirs)
sourceDirectories = files(subprojects.sourceSets.main.allSource.srcDirs)
classDirectories = files(subprojects.sourceSets.main.output)
dependsOn = subprojects.test
executionData = files(subprojects.jacocoTestReport.executionData)
reports {
html.enabled = true
xml.enabled = true
}
onlyIf = {
true
}
doFirst {
executionData = files(executionData.findAll {
it.exists()
})
}
}
但是我无法运行它,因为我在尝试运行./gradlew jacocoRootReport
时出现此错误:
Could not find method jacocoTestReport() for arguments [build_6nnhcgikryugeat1c382yp5m6$_run_closure3$_closure6@1a3e8e24] on root project 'MyProject'.
我该如何解决这个问题?