I am working on a grails application. We use cobertura to generate the code coverage reports. Now I want to modify the grails project such that the build should fail if the code coverage is less than say, 90%. How can I achieve this in grails?
I don't think the code-coverage plugin supports this directly, but it's easy enough to do by hooking into the powerful grails build events infrastructure. By placing this in your scripts/_Events.groovy
, the build will fail if coverage is below a certain threshold:
eventStatusFinal = { message ->
if (message ==~ /.*Cobertura Code Coverage Complete.*/) {
def report = new XmlSlurper().parse(new File("target/test-reports/cobertura/coverage.xml"))
if (Float.parseFloat(report.'@line-rate'.text()) < 0.90) {
throw new RuntimeException("coverage too low!")
}
}
}
This requires you to turn on the XML report generation with this in grails-app/conf/BuildConfig.groovy
:
coverage {
xml = true
}
Adjust the attribute (line-rate
, branch-rate
) and value as needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With