Skip to content

Instantly share code, notes, and snippets.

@jdsingh
Forked from tristanlins/build.gradle
Created February 18, 2021 20:40
Show Gist options
  • Save jdsingh/997947fab503fe4387609cc6e0155152 to your computer and use it in GitHub Desktop.
Save jdsingh/997947fab503fe4387609cc6e0155152 to your computer and use it in GitHub Desktop.
Gradle Jacoco Plugin and Gitlab Coverage
plugins {
id 'jacoco'
}
jacocoTestReport {
reports {
xml.enabled true
}
}
task coverageReport() {
dependsOn jacocoTestReport
def reportFile = project.file("build/reports/jacoco/test/jacocoTestReport.xml")
inputs.file(reportFile)
doLast {
def slurper = new XmlSlurper()
slurper.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false)
slurper.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
def xml = slurper.parse(reportFile)
def counter = xml.counter.find {
node -> node.@type == 'BRANCH'
}
def missed = counter.@missed.toDouble()
def covered = counter.@covered.toDouble()
def total = missed + covered
def percentage = covered / total * 100
printf "Missed %.0f branches%n", missed
printf "Covered %.0f branches%n", covered
printf "Total %.0f%%%n", percentage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment