Skip to content

Instantly share code, notes, and snippets.

@tristanlins
Created March 2, 2019 13:15
Show Gist options
  • Save tristanlins/9ac55dc7e6692b8c98dc0242724b12af to your computer and use it in GitHub Desktop.
Save tristanlins/9ac55dc7e6692b8c98dc0242724b12af 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
}
}
@leogmarzo
Copy link

It helped me! Thanks!

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