我正在尝试在Gradle中生成仪器测试覆盖率报告以发送给SonarQube。由于这是Kotlin代码库,因此我需要使用Jacoco版本0.8.3+来生成报告,以避免出现here概述的问题。
我创建的用于生成单元测试覆盖率报告的自定义任务可以在指定的Jacoco版本中正常运行。但是使用./gradlew createDebugCoverageReport
生成的已测试的测试覆盖率报告仍会生成Jacoco版本0.7.9的报告。
是否有一种方法可以制作./gradlew createDebugCoverageReport
或其他方法来使用较新版本的Jacoco插件0.8.3+创建仪器测试覆盖率报告?
顶级build.gradle文件的相关部分:
buildscript {
dependencies {
classpath "org.jacoco:org.jacoco.core:0.8.5"
}
}
模块级build.gradle文件的相关部分:
apply plugin:"com.android.library"
apply plugin: "jacoco"
android {
buildTypes {
debug {
testCoverageEnabled = true
}
}
}
jacoco {
toolVersion = "0.8.5"
}
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) {
group "Reporting"
description "Generate Jacoco coverage reports from unit tests."
reports {
xml.enabled = true
html.enabled = true
}
def fileFilter =
['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*',
'android/**/*.*']
def javaDebugTree =
fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
def kotlinDebugTree =
fileTree(dir: "${buildDir}/tmp/kotlin-classes/debug", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"
sourceDirectories.from.addAll(files([mainSrc]))
classDirectories.from.addAll(files([javaDebugTree, kotlinDebugTree]))
executionData.from.addAll(fileTree(dir: "$buildDir", includes: [
"jacoco/testDebugUnitTest.exec",
"outputs/code-coverage/connected/*coverage.ec"
]))
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
}
依次运行任务test
connectedDebugAndroidTest
jacocoTestReport
生成两组jacoco报告。来自使用较旧版本的jacoco进行连接的测试的报告,以及使用正确的jacoco版本的单元测试的报告。