Android 杰科科可以';t读取.ec文件
为了生成单元和Ui测试的代码覆盖率,我实现了这个Android 杰科科可以';t读取.ec文件,android,unit-testing,gradle,code-coverage,jacoco,Android,Unit Testing,Gradle,Code Coverage,Jacoco,为了生成单元和Ui测试的代码覆盖率,我实现了这个jacoco.gradle脚本 apply plugin: 'jacoco' jacoco { toolVersion = "0.8.5" } tasks.withType(Test) { jacoco.includeNoLocationClasses = true } project.afterEvaluate { android.applicationVariants.all { variant
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测试覆盖率时,我遇到了以下错误:
无法读取执行数据文件/../app/build/outputs/code\u coverage/debugAndroidTest/connected/*coverage.ec
我的项目层次结构如下所示
我正在使用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"
])
)
您解决了这个问题吗?@dudi是
testinstrumentationrunnearguments clearPackageData:“true”
存在于您的gradle文件中?非常感谢您在@Jim Roobbins为我提供这个解决方案。我会尽快试试这个。
//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)
}