【问题标题】:Jacoco report isn't excluding specified filesJacoco 报告不排除指定文件
【发布时间】:2020-05-10 13:02:32
【问题描述】:

我正在使用 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 中设置的文件,例如活动或适配器。但目前该报告没有考虑我的排除项,正如您在 CodeCov 的以下报告中看到的那样,我仍然有排除文件(ViewHolder 或适配器)

【问题讨论】:

    标签: android gradle jacoco


    【解决方案1】:

    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 的输出如下所示:

    注意底部的Created with JaCoCo 0.8.5.201910111838


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

    【讨论】:

    • 您好,感谢您花这么多时间回复此回复,非常感谢 :) 关于 jacoco 报告,它已经运行良好,我可以使用当前设置生成报告。我的主要问题是从覆盖范围中排除特定类。正如您的报告中显示的那样,它不适用于 Jacoco。我尝试通过指定忽略路径来添加 codecov.yml,但结果仍然相同:我想要排除的类仍然计入代码覆盖率
    • 我怀疑你在哪里生成 JaCoCo 报告,测试任务中断。很可能您的codecov.yml 配置也不起作用; CodeCov GitHub 应用程序还应该从哪里获取它的配置?
    【解决方案2】:

    您是否尝试过专门配置 gradle jacoco 插件的排除项:

    test {
        jacoco {
            enabled = true
            destinationFile = file("$buildDir/jacoco/${name}.exec")
            includes = []
            excludes = []
            excludeClassLoaders = []
            includeNoLocationClasses = false
            sessionId = "<auto-generated value>"
            dumpOnExit = true
            classDumpDir = null
            output = JacocoTaskExtension.Output.FILE
            address = "localhost"
            port = 6300
            jmx = false
        }
    }
    

    来源:https://docs.gradle.org/current/userguide/jacoco_plugin.html

    对我来说,这很好用。有了这个你可以排除完整的包**/my/package/**

    【讨论】:

      猜你喜欢
      • 2019-04-10
      • 2013-01-18
      • 1970-01-01
      • 2015-08-24
      • 2023-03-28
      • 2019-09-07
      • 2019-01-04
      • 2018-09-22
      • 2015-01-17
      相关资源
      最近更新 更多