Jacoco报告未排除指定的文件

问题描述 投票:6回答:2

我正在使用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或Adapter)

enter image description here

android gradle jacoco
2个回答
2
投票

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配置无关)。


0
投票

您是否尝试专门配置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/**

© www.soinside.com 2019 - 2024. All rights reserved.