我一直在使用Jacoco生成代码覆盖率报告,并且工作正常。我的项目包含使用JUnit 5的单元测试。我使用“de.mannodermaus.android”插件在JUnit 5中编写测试。
最近我更新了下面的依赖关系-
App的Build.gradle文件-
apply from: '../jacoco-config.gradle'
apply plugin: "de.mannodermaus.android-junit5"
androidTestImplementation "de.mannodermaus.junit5:android-test-core:1.3.0"
androidTestRuntimeOnly "de.mannodermaus.junit5:android-test-runner:1.3.0"
// (Required) Writing and executing Unit Tests on the JUnit Platform
testImplementation "org.junit.jupiter:junit-jupiter-api:5.8.2"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.8.2"
// (Optional) If you need "Parameterized Tests"
testImplementation "org.junit.jupiter:junit-jupiter-params:5.8.2"
// (Optional) If you also have JUnit 4-based tests
testRuntimeOnly "org.junit.vintage:junit-vintage-engine:5.8.2"
testImplementation "junit:junit:4.13.2"
字符串
项目级Build.gradle文件-
classpath "org.jacoco:org.jacoco.core:0.8.8"
classpath "de.mannodermaus.gradle.plugins:android-junit5:1.8.2.1"
型
我已经创建了一个gradle文件,用于应用Jacoco更改和设置。该文件的主要内容如下。
jacoco-config.gradle
apply plugin: 'jacoco'
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']}
jacoco {
toolVersion = "0.8.8"
reportsDir = file("$buildDir/JacocoReport")}
android {
buildTypes {
debug {
testCoverageEnabled = true
}
}
testOptions {
animationsDisabled true
unitTests {
includeAndroidResources = true
returnDefaultValues = true
}
}
}
project.afterEvaluate {
project.gradle.taskGraph.whenReady {
testDebugUnitTest {
ignoreFailures true
}
}
def androidExclusion = [
'**/BR.*',
'**/R.*',
'**/R$*.*',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/databinding/*.*'
]
// Create coverage task ex: 'jacocoTestReport<Flavor>' depending on
task "jacocoTestReport"(type: JacocoReport, dependsOn: ['testDebugUnitTest']) {
group = "Reporting"
description = "Generate Jacoco coverage reports on the testDebugUnitTest build."
def javaClasses = fileTree(
dir: "${project.buildDir}/intermediates/javac/debug",
excludes: androidExclusion)
def kotlinClasses = fileTree(
dir: "${project.buildDir}/tmp/kotlin-classes/debug",
excludes: androidExclusion)
classDirectories.from = files([ kotlinClasses , javaClasses])
def coverageSourceDirs = ["src/main/java"]
getAdditionalSourceDirs().setFrom(files(coverageSourceDirs))
getSourceDirectories().setFrom(files(coverageSourceDirs))
getExecutionData().setFrom(fileTree(dir: project.projectDir, includes: ["**/*.exec", "**/*.ec"]))
reports {
xml.required = true
html.required = true
}
}
}
型
1.运行Jacoco gradle任务时,代码覆盖率报告显示0%覆盖率。x1c 0d1x
1.但是,测试结果报告是正确的,并分别显示成功和失败测试。
我已经检查了jacoco gradle文件的源路径和目标路径是正确的。此外,Java和Kotlin的类文件路径似乎是正确的。
我使用的是gradle plugin version 7.2.2和distributionUrl 7.3.3
任何帮助都很感激。谢谢。
2条答案
按热度按时间tp5buhyn1#
testCoverageEnabled false
应该解决测试中的插装错误问题,如果测试通过但出现异常,请查看详细答案:https://issuetracker.google.com/issues/178015739#:text = go...%40gmail.com%3E-,%232,-Jan%2022%2C%202021zqdjd7g92#
我已经评论了androidExclusion的列表:
字符串
覆盖率不再为0。
编辑:
testCoverageEnabled
必须是false
!