Monday, January 2, 2012

Java - Generating code coverage reports using Cobertura with Maven

If you have a requirement or a plan to generate test code coverage reports for your project using Cobertura and you are using Maven as your build tool, copy the following onto your pom.xml in the build/plugins section.

<plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>cobertura-maven-plugin</artifactId>
      <version>2.5.1</version>
       <configuration>
      <instrumentation>
        <includes>
          <include>com/company/**/*Manage*.class</include>
          <include>com/company/**/*Tool*.class</include>
        </includes>
      </instrumentation>
      <aggregate>true</aggregate>
      <outputDirectory>C:/Project/TestCoverageReports</outputDirectory>
    </configuration>
    <executions>
      <execution>
          <phase>package</phase>
          <goals>
              <goal>cobertura</goal>
          </goals>
      </execution>
    </executions>
</plugin>

Make sure that your maven doesnt skip tests - i.e., -Dmaven.test.skip is set to false.

Note that the test coverage report will be generated in the configured outputDirectory. If you are working on a project that has a parent POM which invokes multiple child POMs, add this to the parent POM, the aggregate option will generate one code coverage report for all the set of modules invoked from inside the parent without any changes to the child POMs. You can always refer to the Maven Cobertura Plugin page for more information and options.

No comments:

Post a Comment