Maven + Cobertura code coverage example

摘要: 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.

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.

Figure : Detail page.

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

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

上一篇: Spring Security Hello World Annotation Example
下一篇: Ant and TestNG Task example
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号