📜 ⬆️ ⬇️

Code Coverage for Maven Projects (code coverage)

The structure of Java projects using Maven assumes a certain number of Unit tests.
But tests alone are few, I want to have more complete information on how much our tests cover the code.

For these purposes there are a number of libraries from which I use Emma ( off site ).


Pom.xml configuration


')
First, we add Emma instrumentation to our classes after the compile phase (process-classes phase)
 <plugin>
     <groupId> org.sonatype.maven.plugin </ groupId>
     <artifactId> emma-maven-plugin </ artifactId>
     <version> 1.2 </ version>
     <configuration>
         <filters>
             <filter> + org. * </ filter>
             <filter> -org.some.package. * </ filter>
         </ filters>
     </ configuration>
     <executions>
         <execution>
             <phase> process-classes </ phase>
             <goals>
                 <goal> instrument </ goal>
             </ goals>
         </ execution>
     </ executions>
 </ plugin>

As you can see from the example, emma-maven-plugin supports filtering. More about plugin parameters

By default, instrumented classes will be created in the $ {project.build.directory} / generated-classes / emma / classes directory.

This directory should be noted as classesDirectory for the maven-surefire-plugin:
 <plugin>
     <groupId> org.apache.maven.plugins </ groupId>
     <artifactId> maven-surefire-plugin </ artifactId>
     <configuration>
         <classesDirectory> $ {project.build.directory} / generated-classes / emma / classes </ classesDirectory>
     </ configuration>
 </ plugin>


And finally, add the creation of a report on the test phase
 <plugin>
     <groupId> org.sonatype.maven.plugin </ groupId>
     <artifactId> emma4it-maven-plugin </ artifactId>
     <version> 1.3 </ version>
     <executions>
         <execution>
             <id> report </ id>
             <phase> test </ phase>
             <goals>
                 <goal> report </ goal>
             </ goals>
             <configuration>
                 <sourceSets>
                     <sourceSet>
                         <directory> $ {project.build.sourceDirectory} </ directory>
                     </ sourceSet>
                 </ sourceSets>
             </ configuration>
         </ execution>
     </ executions>
 </ plugin>

As a result, we get the target / site / emma directory with HTML, TXT and XML reports.

Hudson



Install the Emma plugin (http://wiki.hudson-ci.org/display/HUDSON/Emma+Plugin) and in the project configuration, check the Record Emma coverage report checkbox.

This plugin uses the coverage.xml created by the emma4it plugin.

As a result, we have graphs, tables of percentage coverage, etc.

Sample graphics (bad):

Source: https://habr.com/ru/post/105046/


All Articles