Jacoco报告没有排除指定的文件。

7

我正在使用Jacoco 0.8.5和Gradle 6.4,我有一个Android项目,我正在尝试设置我的代码覆盖率。这是我的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"]))
    }
}

我希望从覆盖范围中删除一些文件,例如Activities或Adapter,这些文件已经设置在androidExcludes中。但是目前报告没有考虑我的排除项,正如您可以从CodeCov的以下报告中看到的那样,我仍然有被排除的文件(ViewHolder或Adapter)。

enter image description here

2个回答

1
如果在 app/src/test(或 app/src/androidTest 用于集成测试)中没有测试,JaCoCo 可能不会生成覆盖率报告。对于 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 文件;参见 忽略路径(CodeCov 配置不关心 JaCoCo 配置)。

你好,感谢你花费这么多时间回复我,非常感激 :) 关于jacoco报告,它已经可以正常工作了,我能够使用当前的设置生成报告。我的主要问题是排除特定类的覆盖范围。从Jacoco中无法做到这一点,就像你的报告所显示的那样。我尝试添加一个codecov.yml文件,并指定忽略路径,但结果仍然相同:我想要排除的类仍然计入代码覆盖率。 - Guimareshh
我怀疑你是否在生成 JaCoCo 报告时,测试任务出现了故障。而且很可能你的 codecov.yml 配置也无法正常工作;否则 CodeCov GitHub 应用程序从哪里获取配置呢? - Martin Zeitler

-1

你有尝试过配置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/**


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接