我在项目中添加了一组没有测试的源,我不希望它们破坏我的测试覆盖率统计信息。我用下一个方式配置了Jacoco:
test {
jacoco{
excludes = ['org/bla/**']
includes = ['com/bla/**']
append = false
}
}
jacocoTestReport {
dependsOn test
description = "Generate Jacoco coverage reports after running tests."
reports {
xml.enabled true
html.enabled true
}
classDirectories = fileTree(dir: 'build/classes/main', include: 'com/bla/**')
sourceDirectories = fileTree(dir: 'scr/main/java', include: 'com/bla/**')
}
但无论如何,在生成报告时,Jacoco还包括来自 org.bla 的课程
有人可以帮助我吗?
修改
经过一些调试后,似乎所有默认输出都以私有方法org.gradle.testing.jacoco.tasks.JacocoReport#classDirectories
org.gradle.testing.jacoco.plugins.JacocoPlugin#addDefaultReportTasks
使用此类代码时可见:
jacocoTestReport {
classDirectories = files('build/classes/main/com/bla')
println("-----------------------------------------------------------")
getAllClassDirs().each { File file ->
println(file.absolutePath)
}
println("-----------------------------------------------------------")
getAdditionalClassDirs().each{ File file ->
println(file.absolutePath)
}
}
jacocoTestReport << {
println("-----------------------------------------------------------")
getAllClassDirs().each { File file ->
println(file.absolutePath)
}
println("-----------------------------------------------------------")
getAdditionalClassDirs().each{ File file ->
println(file.absolutePath)
}
}
Output
-----------------------------------------------------------
<path_here>\build\classes\main\com\bla
-----------------------------------------------------------
....more text here
-----------------------------------------------------------
<path_here>\build\classes\main\com\bla
<path_here>\build\classes\main
<path_here>\build\resources\main
-----------------------------------------------------------
所以 - 问题是:是否可以覆盖某种org.gradle.testing.jacoco.plugins.JacocoPlugin#addDefaultReportTasks
方法,或完全覆盖org.gradle.testing.jacoco.plugins.JacocoPlugin
类?
答案 0 :(得分:10)
好的,找到了解决方法:
jacocoTestReport.doFirst{
classDirectories = files('build/classes/main/com/bla')
}
这将覆盖由JacocoPlugin类设置的classDirector。
答案 1 :(得分:-1)
如果您的任务名称不是
test
运行单元测试的,你应该能够生成报告,将以下任务添加到build.gradle文件中:
jacocoTestReport {
group = "Reporting"
description = "Generates Jacoco coverage reports for unit tests"
executionData = files('build/jacoco/unitTest.exec')
reports {
html.destination = 'build/reports/coverage'
}
}
这需要在运行单元测试的任务之后显式调用,例如:
gradle unitTest jacocoTestReport