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



  1. you can NOT use maven.test.skip when haltOnFailure is set to true?

    i can't seem to figure out a way to install my local snapshots without editing the pom every time i want to skip the tests and the cobertura check.

    - disgruntled rangers fan

  2. Jeff,

    check out the following blog posting on how to fix that issue.