我想在我的源文件中获取测试覆盖结果的行覆盖。生成的表中的测试结果列有遗漏的行,甚至还有遗漏的控制流。根据我读过的东西,这意味着如果找到源文件,我将能够在我的源文件中显示行覆盖率。
目前,我在单独的报告中有单元测试结果和 android 测试结果。android 测试结果显示“在生成报告期间未找到源文件“com/example/helloworld/KeyboardUtil.kt”。”
这是 gradle 的相关部分。
jacoco {
toolVersion = jacoco_version
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']
}
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
reports {
xml.enabled = true
html.enabled = true
csv.enabled = true
}
def fileFilter = ['android/databinding/**/*.class',
'**/android/databinding/*Binding.class',
'**/android/databinding/*',
'**/androidx/databinding/*',
'**/BR.*',
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/*_MembersInjector.class',
'**/Dagger*Component.class',
'**/Dagger*Component$Builder.class',
'**/*Module_*Factory.class',
'**/di/module/*',
'**/*_Factory*.*',
'**/*Module*.*',
'**/*Dagger*.*',
'**/*Hilt*.*',
'**/*MapperImpl*.*',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/BuildConfig.*',
'**/*Component*.*',
'**/*BR*.*',
'**/Manifest*.*',
'**/*$Lambda$*.*',
'**/*Companion*.*',
'**/*Module*.*',
'**/*Dagger*.*',
'**/*Hilt*.*',
'**/*MembersInjector*.*',
'**/*_MembersInjector.class',
'**/*_Factory*.*',
'**/*_Provide*Factory*.*',
'**/*Extensions*.*',
'**/*$Result.*',
'**/*$Result$*.*']
def debugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/debug", excludes: fileFilter)
def mainSrc = fileTree(dir: "${projectDir}/src/main/java", excludes: fileFilter)
getSourceDirectories().setFrom(files([mainSrc]))
getClassDirectories().setFrom(files([debugTree]))
def unitTestsData = "$project.buildDir/jacoco/testDebugUnitTest.exec"
def androidTestsData = fileTree(dir: "${buildDir}/outputs/code_coverage/debugAndroidTest/connected/",
includes: ["**/*.ec"])
getExecutionData().setFrom(files([unitTestsData, androidTestsData]))
}
我认为问题出在以下几行:
def debugTree = fileTree(dir: "${buildDir}/tmp/kotlin-classes/debug", excludes: fileFilter)
def mainSrc = fileTree(dir: "${projectDir}/src/main/java", excludes: fileFilter)
getSourceDirectories().setFrom(files([mainSrc]))
getClassDirectories().setFrom(files([debugTree]))
主 src 目录是正确的,但是在报告中,结果的路径是“app > com.example.helloworld > KeyboardUtil”,而应该是“app > src > main > java > com.example.helloworld > KeyboardUtil”
不知道如何修复它。