android - Jacoco 报告不排除指定文件

标签 android gradle jacoco

我正在使用 Jacoco 0.8.5 和 Gradle 6.4,我有一个 Android project我正在尝试设置我的代码覆盖率。这是我的 jacoco.gradle 文件的方式:

apply plugin: 'jacoco'

def flavor = "debug"
def unitTestTask = "testDebugUnitTest"

jacoco {
    toolVersion = "0.8.5"
}

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

final androidExcludes =
        ["**/R.class",
         "**/R\$*.class",
         "**/BuildConfig.*",
         "**/Manifest*.*",
         "**/*Test*.*",
         "android/**/*.*",
         "**/*_MembersInjector.class",
         "**/Dagger*Component.class",
         "**/Dagger*Component\$Builder.class",
         "**/*Module_*Factory.class",
         "**/*_Provide*Factory*.*",
         "**/*_Factory*.*",
         "**/*Activity*.*",
         "**/*Fragment*.*",
         "**/*ViewHolder*.*",
         "**/*Adapter*.*"]


task jacocoReport(type: JacocoReport, dependsOn: "${unitTestTask}") {
    reports {
        xml.enabled = true
        html.enabled = true
    }

    afterEvaluate {

        def debugTree = fileTree(dir: "$project.buildDir/intermediates/javac/${flavor}/classes",
                excludes: androidExcludes)
        def kotlinDebugTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/${flavor}/",
                excludes: androidExcludes)
        def mainSrc = "$project.projectDir/src/main/java"

        sourceDirectories.setFrom(files([mainSrc]))
        classDirectories.setFrom(files([debugTree], [kotlinDebugTree]))
        executionData.setFrom(fileTree(dir: project.buildDir, includes: ["jacoco/${unitTestTask}.exec"]))
    }
}


我想从覆盖范围中删除一些在 androidExcludes 中设置的文件,例如 Activity 或适配器。但是目前该报告没有考虑到我的排除项,正如您在 CodeCov 的以下报告中看到的那样,我仍然有排除文件(ViewHolder 或适配器)

enter image description here

最佳答案

JaCoCo 可能不会生成覆盖率报告,而 app/src/test 中没有测试(或 app/src/androidTest 用于集成测试)。对于 JUnit 5,它还需要以下依赖项:

dependencies {

    // (Required) Writing and executing Unit Tests on the JUnit Platform
    testImplementation ("org.junit.jupiter:junit-jupiter-api:5.6.2")
    testRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.6.2")

    // (Optional) If you need "Parameterized Tests"
    testImplementation ("org.junit.jupiter:junit-jupiter-params:5.6.2")

    // (Optional) If you also have JUnit 4-based tests
    testImplementation ("junit:junit:4.13")
    testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.6.2")

    androidTestImplementation ("org.junit.jupiter:junit-jupiter-api:5.6.2")

    // The instrumentation test companion libraries
    androidTestImplementation ("de.mannodermaus.junit5:android-test-core:1.2.0")
    androidTestRuntimeOnly ("de.mannodermaus.junit5:android-test-runner:1.2.0")

    // testImplementation ("androidx.test:core:1.2.0")
    // androidTestImplementation("androidx.test:runner:1.2.0")
    androidTestImplementation("androidx.test:rules:1.2.0")
}

PR #23修复测试。 :jacocoTestReportDebug 的输出看起来像这样:

JaCoCo HTML Report

注意 Created with JaCoCo 0.8.5.201910111838在底部。

对于 CodeCov,您需要添加 codecov.yml ;见 ignoring paths (CodeCov 配置不关心 JaCoCo 配置)。

关于android - Jacoco 报告不排除指定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61712439/

相关文章:

android - 如何从 android 中的自定义列表适配器获取控件 ID?

java - glTexImage2D 中的 ByteBuffer.wrap() 导致过多的垃圾收集

android - 尝试构建 ionic Android 应用程序时出现 Dex 错误

java - 使用 Travis CI 和 Gradle 自动构建计数部署 jar 文件

unit-testing - Gradle测试任务-Gradle 1.6以及Gradle 2.3或更高版本

android:如何获取给定 TextView 的背景颜色的 ARGB 值

android - 添加子菜单时,Android应用程序崩溃

android - 如何使用Gradle将其他参数传递给transformClassesWithDexForDebug?

jacoco - 由于没有类文件,因此无法对项目覆盖率进行 JaCoCo 分析

java - 如何使用 JaCoCo 忽略内部/嵌套类?