gpt4 book ai didi

Android 使用 jacocoTestCoverageVerification 检查代码覆盖阈值

转载 作者:行者123 更新时间:2023-11-29 01:10:10 27 4
gpt4 key购买 nike

我想要什么:

我想在 Android gradle 中将代码覆盖率阈值与最小值(如 60% 等)相匹配。

我尝试过的

stack overflow question i have looked into

jacoco plugin gradle

我面临的问题

我的 Gradle 文件是这样写的:

apply plugin: 'com.android.application'

apply plugin: 'jacoco'


jacoco {
toolVersion = "0.7.6.201602180812"

reportsDir = file("$buildDir/customJacocoReportDir")
}

def coverageSourceDirs = [
'src/main/java',
]

jacocoTestReport {

reports {

xml.enabled false
csv.enabled false
html.destination "${buildDir}/jacocoHtml"
}
}



jacocoTestCoverageVerification {

violationRules {
rule {
limit {
minimum = 0.5
}
}

rule {
enabled = false
element = 'CLASS'
includes = ['org.gradle.*']

limit {
counter = 'LINE'
value = 'TOTALCOUNT'
maximum = 0.3
}
}
}
}

现在,当我同步我的 gradle 文件时,我会遇到以下错误

在 org.gradle.api.Project 类型的项目“:app”上找不到参数 [build_5t0t9b9hth6zfihsyl5q2obv8$_run_closure2@41a69b20] 的方法 jacocoTestReport()。

然后如果我评论 jacocoTestReport 任务

无法在 org.gradle.api.Project 类型的项目“:app”上找到参数 [build_5t0t9b9hth6zfihsyl5q2obv8$_run_closure2@3da790a8] 的方法 jacocoTestCoverageVerification()。

我无法理解这里到底发生了什么。为什么 jacocoTestCoverageVerification 方法不在插件中。我做错了什么。

gradle 是否正在从 android 插件中挑选 jacoco 插件?

我曾尝试将 jacoco 的版本提到 0.6.3,正如该文档中提到的那样,jacocoTestCoverageVerification 方法是在该版本之上编写的。

如果有人能解决这个问题,那将非常有帮助。

如果需要任何其他信息,请告诉我。

谢谢

最佳答案

这个问题一直困扰着我几次,每次关键字 android jacocoTestCoverageVerification 都会将我引导到此页面但没有得到任何答案。最后,我成功地让 jacoco 工作了,我想在这里分享我的解决方案。

gradle找不到jacocoTestCoverageVerification和jacocoTestReport的原因是对于同样应用了Java Plugin的项目,JaCoCo插件会自动添加如下任务jacocoTestReport和jacocoTestCoverageVerification。这意味着对于未应用java插件的项目,它不会添加jacocoTestReport和jacocoTestCoverageVerification。

所以我们必须自己添加它们。

点击链接:Code Coverage for Android Testing ,我们可以添加任务jacocoTestReport。

同样的方法,我们也可以添加任务jacocoTestCoverageVerification。

完整功能如下

// https://engineering.rallyhealth.com/android/code-coverage/testing/2018/06/04/android-code-coverage.html
ext.enableJacoco = { Project project, String variant ->
project.plugins.apply('jacoco')

final capVariant = variant.capitalize()

StringBuilder folderSb = new StringBuilder(variant.length() + 1)
for (int i = 0; i < variant.length(); i++) {
char c = variant.charAt(i)
if (Character.isUpperCase(c)) {
folderSb.append('/')
folderSb.append(Character.toLowerCase(c))
} else {
folderSb.append(c)
}
}
final folder = folderSb.toString()


project.android {
buildTypes {
debug {
testCoverageEnabled true
}
}

testOptions {
unitTests.all {
jacoco {
//You may encounter an issue while getting test coverage for Robolectric tests.
//To include Robolectric tests in the Jacoco report, one will need to set the includeNolocationClasses flag to true.
// This can no longer be configured using the android DSL block, thus we search all tasks of Test type and enable it

includeNoLocationClasses = true
}
}
}
jacoco {
version = '0.8.1'
}
}

project.jacoco {
toolVersion = '0.8.1'
}

project.tasks.create(
name: 'jacocoTestCoverageVerification',
type: JacocoCoverageVerification,
dependsOn: ["test${capVariant}UnitTest",
"create${capVariant}CoverageReport"
]
) {
onlyIf = {
true
}

violationRules {
rule {
limit {
minimum = 0.5
}
}

rule {
enabled = false
element = 'CLASS'
includes = ['org.gradle.*']

limit {
counter = 'LINE'
value = 'TOTALCOUNT'
maximum = 0.3
}
}
}
def coverageSourceDirs = [
"src/main/java",
"src/main/kotlin"
]

def fileFilter = [
'**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/BuildConfig.*',
'**/*_MembersInjector.class',
'**/Dagger*Component.class',
'**/Dagger*Component$Builder.class',
'**/*Module_*Factory.class',
'**/*_MembersInjector.class',
'**/Dagger*Subcomponent*.class',
'**/*Subcomponent$Builder.class',
'**/Manifest*.*'
]

def javaClasses = fileTree(
dir: "${project.buildDir}/intermediates/javac/$folder",
excludes: fileFilter
)
def kotlinClasses = fileTree(
dir: "${project.buildDir}/tmp/kotlin-classes/$variant",
excludes: fileFilter
)

group = "Reporting"
description = "Applying Jacoco coverage verification for the ${project.name} with the " +
"$variant variant."
classDirectories = files([javaClasses], [kotlinClasses])
additionalSourceDirs = files(coverageSourceDirs)
sourceDirectories = files(coverageSourceDirs)
executionData = fileTree(dir: "${project.buildDir}", includes: [
"jacoco/testDebugUnitTest.exec",
"outputs/code_coverage/debugAndroidTest/connected/*.ec",
"outputs/code_coverage/connected/*.ec" //Check this path or update to relevant path
])
}
project.tasks.create(
name: 'jacocoTestReport',
type: JacocoReport,
dependsOn: ["test${capVariant}UnitTest",
"create${capVariant}CoverageReport"
]
) {
def coverageSourceDirs = [
"src/main/java",
"src/main/kotlin"
]

def fileFilter = [
'**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/BuildConfig.*',
'**/*_MembersInjector.class',
'**/Dagger*Component.class',
'**/Dagger*Component$Builder.class',
'**/*Module_*Factory.class',
'**/*_MembersInjector.class',
'**/Dagger*Subcomponent*.class',
'**/*Subcomponent$Builder.class',
'**/Manifest*.*'
]

def javaClasses = fileTree(
dir: "${project.buildDir}/intermediates/javac/$folder",
excludes: fileFilter
)
def kotlinClasses = fileTree(
dir: "${project.buildDir}/tmp/kotlin-classes/$variant",
excludes: fileFilter
)

group = "Reporting"
description = "Generate Jacoco coverage reports for the ${project.name} with the " +
"$variant variant."
classDirectories = files([javaClasses], [kotlinClasses])
additionalSourceDirs = files(coverageSourceDirs)
sourceDirectories = files(coverageSourceDirs)
executionData = fileTree(dir: "${project.buildDir}", includes: [
"jacoco/testDebugUnitTest.exec",
"outputs/code_coverage/debugAndroidTest/connected/*.ec",
"outputs/code_coverage/connected/*.ec" //Check this path or update to relevant path
])
onlyIf = {
true
}
println project
println "current $project buildDir: $buildDir project buildDir: ${project.buildDir}"
System.out.flush()
reports {
html.enabled = true
html.destination file("reporting/jacocohtml")
}
}


}

Gist version

关于Android 使用 jacocoTestCoverageVerification 检查代码覆盖阈值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43736464/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com