I'm getting the Unit test coverage percentage metric from sonar rest api.
How can I fail the build if it falls below a defined value?
I'm getting the Unit test coverage percentage metric from sonar rest api.
How can I fail the build if it falls below a defined value?
JaCoCo
offers that feature.JaCoCo with Configuration rules
Define JaCoCo plugin using configuration rules
COVEREDRATIO
forLINE
andBRANCH
:Various options
The supported
counter
options are:I believe
INSTRUCTION
would allow you to make a general check (verify that the whole project has at least 0.80 of coverage for instance).Example with INSTRUCTION - overall instruction coverage of 80%
Message on failure
If the coverage isn't as expected, it fails with the following message:
Exclusions
In the example above, I set
<exclude>com.xyz.ClassToExclude</exclude>
. I think you'll find you need to add many exclusions. Projects usually contain many classes which aren't testable/tested (Spring Configuration, Java beans...). You may be able to use regular expression too.sources:
If you are using 0.8.2 or similar versions of jacoco-maven-plugin, please make sure data file is defined, and use the following configuration for the plugin.
This code ensures overall coverage of 17%.
Please verify this using
command.