JacocoReport 任务:通过简单地将子模块文件添加到 sourceDirectories 和 classDirectories 来获得全面覆盖?
JacocoReport Task: Get overall coverage by simply adding submodules files to sourceDirectories and classDirectories?
工具版本
gradle 5.6.4
java1.8
jacoco 工具版本 0.8.2
项目结构
├── build.gradle
├── app
│ ├── build.gradle (1)
│ └── src
├── submodule_1
│ ├── build.gradle (2)
│ └── src
├── submodule_2
│ ├── build.gradle (3)
│ └── src
├── gradle
│ ├── jacoco.gradle
│ └── other_scripts.gradle
描述
(1) 进口 jacoco.gradle
.
(1)、(2) 和 (3) 具有以下配置:
buildTypes {
debug {
testCoverageEnabled true
}
}
问题
我想得到一个全面的覆盖,我可以简单地在属性classDirectories
和sourceDirectories
中包含子模块的文件吗,或者shell 我将它们包含在 additionalClassDirs
中?我不太明白他们的区别。
这是 app/build.gradle
导入的 gradle/jacoco.gradle
。
apply plugin: 'jacoco'
def coverageSourceDirs = [
"${rootDir.absolutePath}/app/src",
"${rootDir.absolutePath}/submodule_1/src",
"${rootDir.absolutePath}/submodule_2/src",
]
def coverageClassDirs = [
fileTree(dir: "${rootDir.absolutePath}/app/build/intermediates/javac/SNMAPP__10009Debug/compileSNMAPP__10009DebugJavaWithJavac/classes", excludes: androidExclusion),
fileTree(dir: "${rootDir.absolutePath}/submodule_1/build/intermediates/javac/debug/compileDebugJavaWithJavac/classes", excludes: androidExclusion),
fileTree(dir: "${rootDir.absolutePath}/submodule_2/build/intermediates/javac/debug/compileDebugJavaWithJavac/classes", excludes: androidExclusion),
]
task jacocoTestReport_test(type: JacocoReport) {
group = "Reporting"
description = "Generate Jacoco coverage reports runing tests."
reports {
xml.enabled = true
html.enabled = true
html.destination file("${rootDir.absolutePath}/app/build/reports/jacoco")
}
sourceDirectories = files(coverageSourceDirs)
classDirectories = files(coverageClassDirs)
executionData = files("${rootDir.absolutePath}/app/build/outputs/code_coverage/SNMAPP__10009DebugAndroidTest/connected/coverage.exec")
}
是的,你只需要在classDirectories
和sourceDirectories
中包含子模块的文件。顺便说一下,您应该在 executionData
中包含子模块的所有测试结果
def appAndroidTests = fileTree(dir: "${buildDir}/outputs/code_coverage/{your flavor}AndroidTest/connected", includes: ["*.ec"])
executionData = files(
"${buildDir}/jacoco/test{your flavor}UnitTest.exec",
"../submodule_1/build/jacoco/test{your flavor}UnitTest.exec",
"../submodule_2/build/jacoco/test{your flavor}UnitTest.exec",
appAndroidTests
)
工具版本
gradle 5.6.4
java1.8
jacoco 工具版本 0.8.2
项目结构
├── build.gradle
├── app
│ ├── build.gradle (1)
│ └── src
├── submodule_1
│ ├── build.gradle (2)
│ └── src
├── submodule_2
│ ├── build.gradle (3)
│ └── src
├── gradle
│ ├── jacoco.gradle
│ └── other_scripts.gradle
描述
(1) 进口 jacoco.gradle
.
(1)、(2) 和 (3) 具有以下配置:
buildTypes {
debug {
testCoverageEnabled true
}
}
问题
我想得到一个全面的覆盖,我可以简单地在属性classDirectories
和sourceDirectories
中包含子模块的文件吗,或者shell 我将它们包含在 additionalClassDirs
中?我不太明白他们的区别。
这是 app/build.gradle
导入的 gradle/jacoco.gradle
。
apply plugin: 'jacoco'
def coverageSourceDirs = [
"${rootDir.absolutePath}/app/src",
"${rootDir.absolutePath}/submodule_1/src",
"${rootDir.absolutePath}/submodule_2/src",
]
def coverageClassDirs = [
fileTree(dir: "${rootDir.absolutePath}/app/build/intermediates/javac/SNMAPP__10009Debug/compileSNMAPP__10009DebugJavaWithJavac/classes", excludes: androidExclusion),
fileTree(dir: "${rootDir.absolutePath}/submodule_1/build/intermediates/javac/debug/compileDebugJavaWithJavac/classes", excludes: androidExclusion),
fileTree(dir: "${rootDir.absolutePath}/submodule_2/build/intermediates/javac/debug/compileDebugJavaWithJavac/classes", excludes: androidExclusion),
]
task jacocoTestReport_test(type: JacocoReport) {
group = "Reporting"
description = "Generate Jacoco coverage reports runing tests."
reports {
xml.enabled = true
html.enabled = true
html.destination file("${rootDir.absolutePath}/app/build/reports/jacoco")
}
sourceDirectories = files(coverageSourceDirs)
classDirectories = files(coverageClassDirs)
executionData = files("${rootDir.absolutePath}/app/build/outputs/code_coverage/SNMAPP__10009DebugAndroidTest/connected/coverage.exec")
}
是的,你只需要在classDirectories
和sourceDirectories
中包含子模块的文件。顺便说一下,您应该在 executionData
def appAndroidTests = fileTree(dir: "${buildDir}/outputs/code_coverage/{your flavor}AndroidTest/connected", includes: ["*.ec"])
executionData = files(
"${buildDir}/jacoco/test{your flavor}UnitTest.exec",
"../submodule_1/build/jacoco/test{your flavor}UnitTest.exec",
"../submodule_2/build/jacoco/test{your flavor}UnitTest.exec",
appAndroidTests
)