gpt4 book ai didi

android - Jacoco 无法读取 .ec 文件

转载 作者:行者123 更新时间:2023-12-04 15:23:54 28 4
gpt4 key购买 nike

为了生成单元和 UI 测试的代码覆盖率,我实现了这个 jacoco.gradle脚本

apply plugin: 'jacoco'

jacoco {
toolVersion = "0.8.5"
}

tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
}

project.afterEvaluate {
android.applicationVariants.all { variant ->
def variantName = variant.name
def testTaskName = "test${variantName.capitalize()}UnitTest"
def uiTestCoverageTaskName = "create${variantName.capitalize()}CoverageReport"

tasks.create(
name: "${testTaskName}Coverage",
type: JacocoReport,
dependsOn: ["$testTaskName", "$uiTestCoverageTaskName"]) {
group = "Reporting"
description = "Generate Jacoco coverage reports for the ${variantName.capitalize()} build."

reports {
html.enabled = true
xml.enabled = true
}

def excludes = [
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
'**/*Application*.*',
'**/*Dagger*.*',
'**/*Hilt*.*',
'**/*GeneratedInjectorModuleDeps*.*'

]
def javaClasses = fileTree(dir: variant.javaCompiler.destinationDir, excludes: excludes)
def kotlinClasses = fileTree(dir: "${buildDir}/tmp/kotlin-classes/${variantName}", excludes: excludes)
classDirectories.setFrom(files([javaClasses, kotlinClasses]))

sourceDirectories.setFrom(
files([
"$project.projectDir/src/main/java",
"$project.projectDir/src/${variantName}/java",
"$project.projectDir/src/main/kotlin",
"$project.projectDir/src/${variantName}/kotlin"
]))

executionData.setFrom(
files([
"${project.buildDir}/jacoco/${testTaskName}.exec",
"${project.buildDir}/outputs/code_coverage/${variantName}AndroidTest/connected/*coverage.ec"
])
)

}
}
}

我在我的 app.build 中应用了这个脚本,像这样 apply from: 'buildscripts/jacoco.gradle'此任务可以为特定风格生成单元和 ui 测试覆盖率。
但是当我用 ./gradlew testDebugUnitTestCoverage 开始 gradle 任务时测试执行得很好,但是在收集 UI 测试覆盖率时,我收到了这个错误: Unable to read execution data file /.../app/build/outputs/code_coverage/debugAndroidTest/connected/*coverage.ec我的项目层次结构如下所示
enter image description here
我正在使用 Gradle 6.1.1 和 Android Gradle Build Tools 4.0.0

最佳答案

在以前的版本中,文件的名称只是 coverage.ec所以通配符查找找到该文件没有问题。并且有许多设备提供了不包含空格的模型,这些也很好。
您遇到的问题是由名称中包含空格的设备名称/型号引起的。 “*coverage.ec”的通配符查找无法正确解析包含空格的文件名。
仍然可以抓取文件,只是需要更多的工作。
换出:

executionData.setFrom(
files([
"${project.buildDir}/jacoco/${testTaskName}.exec",
"${project.buildDir}/outputs/code_coverage/${variantName}AndroidTest/connected/*coverage.ec"
])
)
//Set your UnitTest .exec file
executionData.setFrom(files(["${project.buildDir}/jacoco/${testTaskName}.exec"]))

//You need to make sure this evaluates after the task begins, not at the gradle configuration stage
doFirst {
//Now look for any files matching *.ec - You can add more details about specific flavor directories if needed.
def instrumentationTestCoverageDirs = project.fileTree("${project.buildDir}/outputs/code_coverage")
.matching { include "**/*.ec" }

//Take this file set and combine it with the UnitTest file set
def allCodeCoverageFiles = instrumentationTestCoverageDirs.files + executionData.files
//If you want to log out what files you are including, use this (if it gives warnings on the info lines, you can simply change them to `println`
project.logger.with {
info("using following code coverage files for ${taskName}")
allCodeCoverageFiles.each { coverageFile ->
info(coverageFile.path)
}
}

executionData.setFrom(allCodeCoverageFiles)
}

关于android - Jacoco 无法读取 .ec 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62703323/

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