Gradle Jacoco Plugin Reporting Zero Coverage

7.6k Views Asked by At

I'm getting zero code coverage reported on a select group of classes, when running Gradle's Jacoco plugin. I have confirmed all unit tests, which tests these classes, have successfully ran.

What is very interesting, is that EclEmma, in Eclipse, generates correct code coverage results. I have confirmed both tools are using the same version of Jacoco.

I'm trying to figure out what the difference between the two tools are? Do i need additonal configuration of the Gradle Jacoco plugin.

Edit: My Gradle Jacoco output is showing "Execution data for class com/.... does not match"

Update: I opened the test.exec file Jacoco generates, in Eclipse. It shows the classes with missing coverage having 80% of their probes executed.

4

There are 4 best solutions below

3
On

This probably means that the jacoco plugin isn't configured correctly in gradle. Here you can find a checklist of common errors with Jacoco and gradle (Thanks to Taeho Kim's clear answer): https://stackoverflow.com/a/23965581/2166900

Also, here is the configuration that I used in my last Android project and that worked for me:

apply plugin: 'jacoco'

jacoco {
    toolVersion = "0.7.2.+"
}

def coverageSourceDirs = [
        'src/main/java'
]

task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug") {
    group = "Reporting"

    description = "Generate Jacoco coverage reports"

    classDirectories = fileTree(
            dir: 'build/intermediates/classes/debug',
            excludes: ['**/R.class',
                       '**/R$*.class',
                       '**/*$ViewInjector*.*',
                       '**/BuildConfig.*',
                       '**/Manifest*.*']
    )

    additionalSourceDirs = files(coverageSourceDirs)
    sourceDirectories = files(coverageSourceDirs)
    executionData = files('build/jacoco/testDebug.exec')

    reports {
        xml.enabled = false
        html.enabled = true
    }
}
0
On

Also check that the unit tests actually run. If you have jar hell with Junit5, it might silently fail. Add --info and check for error messages.

0
On

I followed this medium guide:

https://medium.com/codex/software-engineering-done-right-2358ae0d6dd4

and into test/jacoco I added include param with my package like this

test{
    ...
    jacoco {
        destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
        includes= ["it.funds.*"]
        classDumpDir = file("$buildDir/jacoco/classpathdumps")
    }
    ...
}
0
On

I had the same problem when switching to gradle wrapper, but kept running jacoco with gradle. Running jacoco with gradle wrapper solved the problem.

This stackoverflow accepted answer helped me figure out what I missed.