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?
Asked
Active
Viewed 384 times
1 Answers
4
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.

ataylor
- 64,891
- 24
- 161
- 189
-
Thanks for the response. Will test it out today and let you know. – Npa Aug 23 '12 at 14:23
-
Do we need to call this eventStatusFinal closure from anywhere. I have cobertura plugin installed in my project. It is generating code coverage reports for me. Not sure how the 'message' will be sent to this closure? – Npa Sep 10 '12 at 13:55
-
No, the grails build system calls it when the StatusFinal event happens. The code-coverage plugin fires the event when it's done writing its report. – ataylor Sep 10 '12 at 14:36
-
Oh...I have the code-coverage plugin, but i was running it manually, using "test-app -coverage"...so, i should first figure out on running the corbetura and generating reports every time i build it automatically, instead of running it manually and then worry about using this closure to fail the build if coverage is less then some %age. – Npa Sep 10 '12 at 15:15
-
I have about 3 _Events files in my project...they are: (1)/Project/.Link_to_grails_plugins/Code-coverage-1.2.4/scripts/_Events..... (2)/Project/.Link_to_grails_plugins/tomcat-1.3.9/scripts/_Events..... (3)/Project/.Link_to_grails_plugins/webtest-3.0.1/scripts/_Events..... I think i should place the above closure in the first location..Am i right? – Npa Sep 10 '12 at 18:18
-
This should go in the `scripts` directory at the root of your project, alongside `src` and `grails-app`. The file `_Events.groovy` won't exist, you'll have to create it. – ataylor Sep 10 '12 at 18:54
-
thanks again...One thing that I noticed here is that when i run test-app -coverage -xml, it would run all the test cases, generate civerage reports and fail if the coverage is less than 90% if i add the above closure. But when I say, run-app, the build passes fine. I am not sure what happens behind the hood, when i say run-app, but ASAIK, test cases are not run at all...My task is to achieve an automated build that runs all the junits and also checks if the coverage is less than 90%. Is there a way that I can achieve this by just saying run -app command? – Npa Sep 11 '12 at 14:04