Maven + Cobertura code coverage example

cobertura-code-coverage-report-1

Cobertura is a free Java code coverage tool – calculates the percentage of code accessed by unit tests. In this tutorial, we will show you how to use Maven to generate the Cobertura code coverage report for your project.

1. Cobertura Code Coverage Report

Do nothing, just type the following Maven command to download and run the maven-cobertura-plugin automatically.


c:\project> mvn cobertura:cobertura

//...
Results :

Tests run: 16, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] <<< cobertura-maven-plugin:2.6:cobertura (default-cli) @ TestNG <<<
[INFO]
[INFO] --- cobertura-maven-plugin:2.6:cobertura (default-cli) @ TestNG ---
[INFO] Cobertura 2.0.3 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Report time: 82ms

//Mkyong : Not sure what caused this error, but Cobertura still works well.
[ERROR] net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler 
loadCoverageData

INFO: Cobertura: Loaded information on 5 classes.

[INFO] Cobertura Report generation was successful.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.309s
[INFO] Finished at: Mon Jan 13 21:45:56 SGT 2014
[INFO] Final Memory: 25M/307M
[INFO] ------------------------------------------------------------------------

Maven will generate the Cobertura code coverage report at ${project}/target/site/cobertura/index.html.

More Examples
Please refer to this Cobertura Maven Plugin for more examples.

Figure : Sample of Cobertura code coverage report, index page, look like a JavaDoc.

cobertura-code-coverage-report

Figure : Detail page.

cobertura-code-coverage-report-1

2. Maven Site + Cobertura Report

To integrate Cobertura report into the Maven site, add the following to the reporting section.

pom.xml

//...
<reporting>
  <plugins>
	<!-- Normally, we take off the dependency report, saves time. -->
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-project-info-reports-plugin</artifactId>
		<version>2.7</version>
		<configuration>
			<dependencyLocationsEnabled>false</dependencyLocationsEnabled>
		</configuration>
	</plugin>

	// integrate maven-cobertura-plugin to project site 
	<plugin>
		<groupId>org.codehaus.mojo</groupId>
		<artifactId>cobertura-maven-plugin</artifactId>
		<version>2.6</version>
		<configuration>
			<formats>
				<format>html</format>
				<format>xml</format>
			</formats>
		</configuration>
	</plugin>

   </plugins>
</reporting>

Creating Maven project site


mvn site

Output - ${project}/site/index.html

maven-cobertura

References

  1. Cobertura code coverage tool
  2. Cobertura Maven Plugin
  3. Maven - Creating a site
  4. Java Code Coverage Tools
  5. Stackoverflow : What is the proper way to use Cobertura with Maven
  6. Maven + Emma code coverage Example

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Vasandharaj Muniyandy
Guest
Vasandharaj Muniyandy

Dear mKong,

Please change the URL of the Cobertura Maven Plugin to below URL

http://www.mojohaus.org/cobertura-maven-plugin/

Regards,
Vasandharaj M.

Manas Satpathi
Guest
Manas Satpathi

Hi, This is very useful, however I get some error while trying to use this plugin. Can you please help?

[Cobertura] Publishing Cobertura coverage report…
[Cobertura] No coverage results were found using the pattern ‘**/target/site/cobertura/coverage.xml’ relative to ‘C:Program Files (x86)Jenkinsworkspacegame-of-life-default’. Did you enter a pattern relative to the correct directory? Did you generate the XML report(s) for Cobertura?
Build step ‘Publish Cobertura Coverage Report’ changed build result to FAILURE
Finished: FAILURE

Rajeev Rathor
Guest
Rajeev Rathor

very much helpful, easy to understand blog as usually.

Vishal
Guest
Vishal

Hi, Thanks for explaining Cobertura report generation in details here. I am able to generate report for all modules available in my war separately. What I am looking for is consolidated report for all modules through single html or xml file in my local workspace. I tried with using aggrgrate tag available with plug in as below

org.codehaus.mojo
cobertura-maven-plugin
2.6

html
xml

true

Could you please help me in this?

Thanks.

D Deepak
Guest
D Deepak

Can you give me the brief idea about Cobertura and fitnesse integration? I mean how Cobertura can generate fitnesse report as well?