Share

September 25, 2008

Using Maven to test your code coverage

For a while i had mixed feelings about code coverage test and that has not changed but the number of lines in my projects has grown and we are under stress to deliver more features by the day I started to worry a bit about our test coverage of the code...

I have always used the cobertura-maven-plugin to generate reports of the code coverage but that is about it, ya cook look graphs that tell you how you are doing but never used it for more than that... This weekend is stated looking at my coverage and started to add checks to each project to make sure we can't build if we fail below a set % of code coverage.

That was good and bad... i played with a lot of the different settings that you can check and ended up using only total line rate (see blow) where the system make sure we run over % of the total lines in the project and if we don't hit the build fails...

A few things to note:
  • I was suppressed to learn that cobertura does not work well with static or enum classes and calls.
  • You should exclude any auto generated code from your coverage check
  • A new fill called cobertura.ser will show up on desk.. make sure you add it to the .cvsingore or source control ignore file since if that file is checked in it will break everything
  • Check out Java: Measure Test Coverage with Cobertura
  • You can use "-Dmaven.test.skip" when haltOnFailure is set to true

Anyway here is a sample of how i use the plugin

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>cobertura-maven-plugin</artifactId>
 <executions>
  <execution>
   <goals>
    <goal>clean</goal>
    <goal>check</goal>
   </goals>
  </execution>
 </executions>
 <configuration>
  <check>
   <haltOnFailure>true</haltOnFailure>
   <totalLineRate>65</totalLineRate>
  </check>
  <instrumentation>
   <excludes>
    <exclude>com/company/autogen/**/*.class</exclude>
   </excludes>
  </instrumentation>
 </configuration>
</plugin>