Skip to content

Instantly share code, notes, and snippets.

@mcassiano
Last active February 26, 2018 13:18
Show Gist options
  • Save mcassiano/eb2a3f97d759ad03926e4e08d86ff0a0 to your computer and use it in GitHub Desktop.
Save mcassiano/eb2a3f97d759ad03926e4e08d86ff0a0 to your computer and use it in GitHub Desktop.
Jacoco aggregated test coverage report (multiple modules)
apply plugin: 'jacoco'
jacoco {
toolVersion = '0.8.0'
}
def exclude = [
// list of classes you want to exclude from test coverage
]
task jacocoReport(type: JacocoReport) {
group "Reporting"
description "Generate Jacoco coverage reports."
reports {
xml.enabled = false
html.enabled = false
}
// if you use kotlin, add its source set here too!
sourceDirectories = files(["$project.projectDir/src/main/java"])
classDirectories = (file("$project.buildDir/intermediates/classes/debug").exists()
? fileTree(dir: "$project.buildDir/intermediates/classes/debug", excludes: exclude)
: fileTree(dir: "$project.buildDir/classes", excludes: exclude))
// scans for both unit and instrumented test coverage results
executionData = fileTree(dir: "$project.buildDir", includes: ["jacoco/*.exec", "outputs/code-coverage/connected/*.ec"])
}
apply plugin: 'jacoco'
jacoco {
toolVersion = '0.8.0'
}
buildscript {
// ...
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
// ...
subprojects {
// for sub modules, configure their test reports
apply from: rootProject.file("gradle/quality.gradle")
}
// ...
// root report
task jacocoRootReport(type: JacocoReport) {
group "Reporting"
description "Generate Jacoco coverage reports."
reports {
xml.enabled = false
html.enabled = true
}
// collecting all source sets, compiled classes and coverage binaries from sub projects
def sourceSets = subprojects.collect { project -> project.jacocoReport.sourceDirectories }.flatten()
def classDirs = subprojects.collect { project -> project.jacocoReport.classDirectories }.flatten()
def coverageBinaries = subprojects.collect { project -> project.jacocoReport.executionData }.flatten()
sourceDirectories = files(sourceSets)
classDirectories = files(classDirs)
executionData = files(coverageBinaries)
}
@mcassiano
Copy link
Author

mcassiano commented Feb 22, 2018

You also need to:

  • Apply the Jacoco plugin to each sub project
  • Run all connected and unit tests before running the root task (so the classes are generated and all test coverage files are available)

For the last requirement you can also depend on those tasks, but for me it does not work because I don't run instrumented tests on my CI server (Firebase Test Lab to the rescue)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment