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?
Run the JUnit. View the results. The results of the run are available in the Code Coverage Results view. If you do not see this view, select Window > Show View > Other > Code Coverage > Code Coverage Results.
Basically, it is a tool used to maintain all project-related documents of all source code including JUnit and project source code. It also helps us to display the coverage level of method and class implementation.
For the code coverage to increase , one would need to run the tests with the coverage enabled and then view the report generated locally to see the areas covered by jacoco during its coverage parse, then from these one would see the methods (per class) that needs to be covered from the view of the jacoco agent.
JaCoCo
offers that feature.
Define JaCoCo plugin using configuration rules COVEREDRATIO
forLINE
and BRANCH
:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>CLASS</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
</limits>
<excludes>
<exclude>com.xyz.ClassToExclude</exclude>
</excludes>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
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%
This example requires an overall instruction coverage of 80% and no class must be missed:
<rules> <rule implementation="org.jacoco.maven.RuleConfiguration"> <element>BUNDLE</element> <limits> <limit implementation="org.jacoco.report.check.Limit"> <counter>INSTRUCTION</counter> <value>COVEREDRATIO</value> <minimum>0.80</minimum> </limit> <limit implementation="org.jacoco.report.check.Limit"> <counter>CLASS</counter> <value>MISSEDCOUNT</value> <maximum>0</maximum> </limit> </limits> </rule> </rules>
If the coverage isn't as expected, it fails with the following message:
[WARNING] Rule violated for class com.sampleapp.SpringConfiguration: lines covered ratio is 0.00, but expected minimum is 0.80
[WARNING] Rule violated for class com.sampleapp.Launcher: lines covered ratio is 0.33, but expected minimum is 0.80
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
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.
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