簡體   English   中英

Jacoco 報告不排除指定文件

[英]Jacoco report isn't excluding specified files

我正在使用 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"]))
    }
}

我想從覆蓋范圍中刪除一些在 androidExcludes 中設置的文件,例如活動或適配器。 但是目前該報告沒有考慮到我的排除項,正如您在 CodeCov 的以下報告中看到的那樣,我仍然有排除文件(ViewHolder 或適配器)

在此處輸入圖像描述

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 報告

注意底部的Created with JaCoCo 0.8.5.201910111838


對於 CodeCov,您需要添加一個codecov.yml 看到忽略路徑
(CodeCov 配置不關心 JaCoCo 配置)。

您是否嘗試過配置 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/**

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM