vendredi 9 septembre 2016

Generate Gradle Jacoco Report in Jenkins environment

I was trying to setup a Jacoco Unit test report after building an Android project in Jenkins. The project is created with Android Studios, but the unit tests are under src/test/java rather than src/androidTes/java because we use Robolectric rather than what comes with Android Studio.

Here is a section of my build.gradle file:

jacoco {
    toolVersion = "0.7.5.201505241946"
    reportsDir = file("$buildDir/reports/jacoco")
}

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

task jacocoTestReport(type: JacocoReport) {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled = true
        html.enabled = true
        html.destination "${buildDir}/jacoco/coverage"
        xml.destination "${buildDir}/jacoco/coverage"
    }
    classDirectories = fileTree(
            dir: './build/intermediates/classes/debug',
            excludes: ['**/R*.class',
                       '**/*$InjectAdapter.class',
                       '**/*$ModuleAdapter.class',
                       '**/*$ViewInjector*.class'
            ])
    sourceDirectories = files(coverageSourceDirs)
    executionData = files("$buildDir/jacoco/testDebugUnitTest.exec")
    // Bit hacky but fixes http://ift.tt/1o7btMn.
    // We iterate through the compiled .class tree and rename $$ to $.
    doFirst {
        new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
            if (file.name.contains('$$')) {
                file.renameTo(file.path.replace('$$', '$'))
            }
        }
    }
}

jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.enabled true
        html.destination "${buildDir}/jacoco/coverage"
        xml.destination "${buildDir}/jacoco/coverage"
    }
}

And the Jenkins build is executing these two commands:

gradle clean build
gradle jacocoTestReport

But what it turns out is that it will generate Jacoco report, but with 0 percent coverage. From Gradle's own report I can see the tests are actually run and all passed. My initial thought was that Jacoco did not recognize the unit tests and thus reported zero percent coverage. What did I do wrong here?

Aucun commentaire:

Enregistrer un commentaire