Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jacoco report isn't excluding specified files

I'm using Jacoco 0.8.5 and Gradle 6.4, I have an Android project which I'm trying to setup my code coverage. Here is how my jacoco.gradle file:

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"]))
    }
}

I want to remove from the coverage some files, set in androidExcludes, for example Activities or Adapter. But currently the report doesn't take into account my excludes, as you can see in the following report from CodeCov I still have excluded files (ViewHolder or Adapter)

enter image description here

like image 861
Guimareshh Avatar asked May 10 '20 13:05

Guimareshh


People also ask

How do I exclude a package from JaCoCo?

Excluding With Custom Annotation Starting from JaCoCo 0.8. 2, we can exclude classes and methods by annotating them with a custom annotation with the following properties: The name of the annotation should include Generated. The retention policy of annotation should be runtime or class.

How do I exclude a folder from JaCoCo code coverage?

You can exclude classes in the Jacoco report by setting an exclude path but the values should be the path of compiled classes relative to the directory target/classes/. The best solution would be to generate the classes in a specific package.

Why does JaCoCo not show coverage?

Why does the coverage report not show line coverage figures? JaCoCo is based on class files analysis. To calculate line coverage class files must contain line number attributes. For this your code must be compiled with debug information.

What files do we need to create JaCoCo report?

Supported formats are HTML, XML and CSV. Defaults to all formats if no values are given. Default value is: HTML,XML,CSV . A list of class files to include in the report.


1 Answers

JaCoCo probably won't generate coverage reports, while there are no tests in app/src/test (or app/src/androidTest for integration tests). For JUnit 5 it also needs these dependencies:

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 fixes the tests. The output from :jacocoTestReportDebug looks alike this then:

JaCoCo HTML Report

Notice the Created with JaCoCo 0.8.5.201910111838 at the bottom.


And for CodeCov, you'd need to add a codecov.yml; see ignoring paths
(the CodeCov configuration does not care about the JaCoCo configuration).

like image 180
Martin Zeitler Avatar answered Oct 03 '22 02:10

Martin Zeitler