Maven + Emma code coverage example

摘要: Emma is a free Java code coverage tool. In this tutorial, we will show you how to use Maven to generate the Emma code coverage report for your project, and also how to integrate the Emma report into the Maven project site.

Emma is a free Java code coverage tool. In this tutorial, we will show you how to use Maven to generate the Emma code coverage report for your project, and also how to integrate the Emma report into the Maven project site.

1. Generate Emma Code Coverage Report

Do nothing, just type the following Maven command mvn emma:emma to run the maven-emma-plugin.

c:\project> mvn emma:emma
//...
Tests run: 16, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.53 sec
EMMA: locking coverage output file [C:\mkyong_projects\TestNG\coverage.ec] ...
EMMA: runtime coverage data merged into [C:\mkyong_projects\TestNG\coverage.ec] {in 78 ms}
Results :
Tests run: 16, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] <<< emma-maven-plugin:1.0-alpha-3:emma (default-cli) @ TestNG <<<
[INFO]
[INFO] --- emma-maven-plugin:1.0-alpha-3:emma (default-cli) @ TestNG ---
processing input files ...
2 file(s) read and merged in 1 ms
writing [xml] report to [C:\mkyong_projects\TestNG\target\site\emma\coverage.xml] ...
writing [html] report to [C:\mkyong_projects\TestNG\target\site\emma\index.html] ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.004s
[INFO] Finished at: Fri Jan 10 23:32:05 SGT 2014
[INFO] Final Memory: 25M/307M
[INFO] ------------------------------------------------------------------------
C:\mkyong_projects\TestNG>

Maven will compile, run unit test and Emma plugin to generate the code coverage report at ${project}/target/site/index.html.

Figure : Sample of Emma code coverage report, ${project}/target/site/index.html.

Figure : Detail page.

2. Maven Site + Emma Report

To integrate Emma reports into the Maven project site, add the following to the reporting section.

pom.xml
  //...
  <reporting>
    <plugins>
	<!-- Normally, dependency report takes time, skip it -->
      <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 emma plugin to project site 
      <plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>emma-maven-plugin</artifactId>
	<version>1.0-alpha-3</version>
	<inherited>true</inherited>
      </plugin>
    </plugins>
  </reporting>

Creating Maven project site

c:\project> mvn site
//...
Tests run: 16, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.45 sec
EMMA: locking coverage output file [C:\mkyong_projects\TestNG\coverage.ec] ...
EMMA: runtime coverage data merged into [C:\mkyong_projects\TestNG\coverage.ec] {in 38 ms}
Results :
Tests run: 16, Failures: 0, Errors: 0, Skipped: 0
//... Generating other reports
[INFO] Generating "Project Summary" report    
[INFO] Generating "Dependencies" report    
[INFO] Generating "EMMA Test Coverage" report   
processing input files ...
2 file(s) read and merged in 2 ms
writing [xml] report to [C:\mkyong_projects\TestNG\target\site\emma\coverage.xml] ...
writing [html] report to [C:\mkyong_projects\TestNG\target\site\emma\index.html] ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.488s
[INFO] Finished at: Fri Jan 10 23:43:58 SGT 2014
[INFO] Final Memory: 40M/1024M
[INFO] ------------------------------------------------------------------------

Output – Project site, ${project}/site/index.html

3. Integrate into the Maven Build

You also can include the “emma-maven-plugin” in the build section.

pom.xml
  //...
  <build>
    <plugins>
	<plugin>
		<groupId>org.codehaus.mojo</groupId>
		<artifactId>emma-maven-plugin</artifactId>
		<version>1.0-alpha-3</version>
		<inherited>true</inherited>
		<executions>
		  <execution>
			<phase>process-classes</phase>
			<goals>
				<goal>instrument</goal>
			</goals>
		  </execution>
		</executions>
	 </plugin>
    </plugins>
  </build>

Just beware of the class X appears to be instrumented already error.

References

  1. Maven – Creating a site
  2. Emma code coverage tool
  3. Emma Maven Plugin
  4. Java Code Coverage Tools
  5. Maven + Cobertuna Code Coverage Example

上一篇: MongoDB Aggregate and Group example
下一篇: Maven site build is very slow dependency report
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

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

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

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