Maven + Cobertura code coverage example
By:Roy.LiuLast updated:2019-08-18
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.
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
- Cobertura code coverage tool
- Cobertura Maven Plugin
- Maven - Creating a site
- Java Code Coverage Tools
- Stackoverflow : What is the proper way to use Cobertura with Maven
- Maven + Emma code coverage Example
From:一号门
COMMENTS