Code coverage with Maven and Cobertura

Cobertura‘s usage is covered in its page so that you can install the plugin and run it separately from the build lifecycle. This how-to integrates it with maven3 to run as part of the site lifecycle.

This configuration has a few lines so it’s probably a good idea to group them inside a profile which executes by default so we check coverage all the time. In order to check code coverage and report it, cobertura needs to instrument the code, test and generate reports. All this can be done with cobertura:cobertura goal. You might want to clean up everything before starting, so you get cobertura:clean goal too. The point is when to execute what, so you should checking at maven’s lifecycle reference for the site phase pre-site looked like a good place for clean up and site for checking coverage and reporting.

<profile>
<id>auditoria-codigo</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>${plugin.cobertura.version}</version>
<configuration>
<check>
<haltOnFailure>false</haltOnFailure>
</check>
</configuration>
<executions>
<execution>
<id>clean</id>
<phase>pre-site</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
<execution>
<id>check-coverage</id>
<phase>site</phase>
<goals>
<goal>cobertura</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>${plugin.cobertura.version}</version>
</plugin>
</plugins>
</reporting>
</profile>

By default cobertura checks for a minimum coverage of 50% or fires an error. So, in order for this error to do not stop the site generation you need to set haltOnFailure to false.

Advertisements

One response to “Code coverage with Maven and Cobertura

  1. Interesante, gracias por el tip, no había escuchado antes de cobertura

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s