web-dev-qa-db-ja.com

Jacocoレポートは指定されたファイルを除外していません

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またはAdapter)を持っています

enter image description here

6
Guimareshh

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#2 テストを修正します。 :jacocoTestReportDebugからの出力は、次のようになります。

JaCoCo HTML Report

下部にあるCreated with JaCoCo 0.8.5.201910111838に注目してください。


そして、CodeCovの場合、codecov.ymlを追加する必要があります。参照 パスを無視
(CodeCov構成はJaCoCo構成を気にしません)。

1
Martin Zeitler