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.
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.
//... <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.
//... <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
- Maven – Creating a site
- Emma code coverage tool
- Emma Maven Plugin
- Java Code Coverage Tools
- Maven + Cobertuna Code Coverage Example
From:一号门
Previous:MongoDB Aggregate and Group example
COMMENTS