android - Jacoco 无法读取 .ec 文件

标签 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 ->
        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/

相关文章:

android - 获取选中复选框的位置

c++ - 具有大量相互依赖条件的单元测试代码

java - 如何将Gradle项目编译为可以在Android应用程序中使用的jar文件

gradle - 使用Gradle编译JasperReports会得到不受支持的版本错误

android - 当 Android 设备进入休眠状态时,CCSpriteFrame 消失了

android - 从网络摄像头Android流媒体直播视频

android - FusedLocationProviderClient 构造函数被标记为内部

php - 单元测试 - 这里有多少测试用例

c++ - eclipse g++ 中的单元测试

android - react-native run-android plugin not found and compile sdk is not specified