Skip to content

Jacoco Test Coverage

Samuel Villaescusa Vinader edited this page Jul 17, 2020 · 2 revisions

Description

Jacoco is a framework to manage code-coverage for Java and Kotlin languages. By using this with Codecov we can show how the code-coverage is affected on each commit (codecov provides useful bots that add comments to Pull requests like this one)

To use Jacoco, you just have to config it like this:

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'connectedAndroidTest']) {
    reports {
        xml.enabled = true
        html.enabled = true
    }

    def fileFilter = ['**/R.class',
                      '**/R$*.class',
                      '**/*$ViewBinder*.*',
                      '**/*$InjectAdapter*.*',
                      '**/*Injector*.*',
                      '**/BuildConfig.*',
                      '**/Manifest*.*',
                      '**/*Test*.*',
                      '**/*Module*.*',
                      '**/*Failure*.*',
                      '**/*Application*.*',
                      '**/CiMattersApplication*.*',
                      'android/**/*.*'
    ]
    def debugTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/debug", excludes: fileFilter)
    def mainSrc = "${project.projectDir}/src/main/java"

    sourceDirectories.setFrom files([mainSrc])
    classDirectories.setFrom files([debugTree])
    executionData.setFrom files(fileTree(dir: "$project.buildDir", includes: [
            "jacoco/testDebugUnitTest.exec",
            "outputs/code_coverage/debugAndroidTest/connected/**/*.ec"
    ]))
}

Then, you just need to execute ./gradlew jacocoTestReport and this will generate code-coverage report that can be uploaded to Codecov or checked to know test health.