Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fail a maven build, if JUnit coverage falls below certain threshold

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?

like image 480
rathna Avatar asked Jan 27 '17 10:01

rathna


People also ask

How do I check my JUnit code coverage?

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.

What is code coverage JUnit?

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.

How do I increase STS code coverage?

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.


1 Answers

JaCoCo offers that feature.

JaCoCo with Configuration rules

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>

Various options

The supported counter options are:

  • LINE
  • BRANCH
  • INSTRUCTION
  • COMPLEXITY
  • METHOD
  • CLASS

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>

Message on failure

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] ------------------------------------------------------------------------

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:

  • http://choudhury.com/blog/2014/02/25/enforcing-minimum-code-coverage
  • http://www.eclemma.org/jacoco/trunk/doc/maven.html
  • http://www.eclemma.org/jacoco/trunk/doc/check-mojo.html
like image 197
alexbt Avatar answered Sep 21 '22 00:09

alexbt