Clover can be used to generate code coverage statistics from practically any kind of test - unit, integration, functional, regression ... - both automatic and manual. The only thing that has to be done is to instrument source code and run it with proper options.
The most frequent Clover usage is to run unit test with code coverage - typically the maven-surefire-plugin is used for this purpose - and thus Clover-for-Maven was designed to cooperate with Surefire plugin "out of the box".
In this short tutorial you will learn how to configure Clover with the Maven Failsafe Plugin, which is used for integration tests.
maven-surefire-plugin | maven-failsafe-plugin | |
---|---|---|
Main purpose | unit tests | integration tests |
Bound to build phase | test | pre-integration-test integration-test post-integration-test verify |
Build fails in phase | test | verify |
Default wildcard pattern | **/Test*.java **/*Test.java **/*TestCase.java | **/IT*.java **/*IT.java **/*ITCase.java |
Default output directory | ${basedir}/target/surefire-reports | ${basedir}/target/failsafe-reports |
In order to have code coverage statistics from integration tests and excluding unit tests, you have to do the following:
<dependencies> <!-- Test framework which will be used by Failsafe plugin. Version number is mandatory --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>com.atlassian.maven.plugins</groupId> <artifactId>maven-clover2-plugin</artifactId> <configuration> <!-- Use custom report descriptor --> <reportDescriptor>clover-report.xml</reportDescriptor> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <!-- Disable unit tests --> <skip>true</skip> </configuration> </plugin> </plugins> </build>
<project name="Clover Report" default="current"> <!-- Initialize Clover --> <clover-setup initString="${cloverdb}"/> <target name="historical"> <!-- Empty as we're not interested in historical reports right now --> </target> <target name="current"> <clover-report> <current outfile="${output}" title="${title}"> <format type="html"/> <!-- Declare naming convention in order to have test classes listed on the "Tests" tab in HTML report --> <testsources dir="src/test"> <!-- Use Maven-Failsafe-Plugin naming convention --> <include name="**/IT*.java"/> <include name="**/*IT.java"/> <include name="**/*ITCase.java"/> <!-- Use Maven-Surefire-Pugin naming convention. NOTE: Although we don't run unit tests, we still want to have them on "Tests" tab instead of "Classes" --> <include name="**/Test*.java"/> <include name="**/*Test.java"/> <include name="**/*TestCase.java"/> </testsources> <!-- Tell Clover to get test results from failsafe. They will be listed on "Results" tab --> <testresults dir="target/failsafe-reports" includes="TEST-*.xml"/> </current> </clover-report> </target> </project>
mvn clean clover2:setup verify clover2:aggregate clover2:clover
In order to have combined coverage statistics from unit and integration tests, you have to do the following:
<properties> <!-- A common location in which a surefire report from 'test' and failsafe report from 'integration-test' phase will be stored. See also the clover-report.xml file which refers to this location --> <surefire.and.failsafe.report.dir>target/test-report</surefire.and.failsafe.report.dir> </properties> <dependencies> <!-- Test framework which will be used by Failsafe plugin. Version number is mandatory --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>com.atlassian.maven.plugins</groupId> <artifactId>maven-clover2-plugin</artifactId> <configuration> <!-- Use custom report descriptor --> <reportDescriptor>clover-report.xml</reportDescriptor> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> <configuration> <reportsDirectory>${surefire.and.failsafe.report.dir}</reportsDirectory> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <reportsDirectory>${surefire.and.failsafe.report.dir}</reportsDirectory> </configuration> </plugin> </plugins> </build>
<project name="Clover Report" default="current"> <!-- Initialize Clover --> <clover-setup initString="${cloverdb}"/> <target name="historical"> <!-- Empty as we're not interested in historical reports right now --> </target> <target name="current"> <clover-report> <current outfile="${output}" title="${title}"> <format type="html"/> <!-- Declare naming convention in order to have test classes listed on the "Test" tab in HTML report --> <testsources dir="src/test"> <!-- Use Maven-Failsafe-Plugin naming convention --> <include name="**/IT*.java"/> <include name="**/*IT.java"/> <include name="**/*ITCase.java"/> <!-- Use Maven-Surefire-Pugin naming convention --> <include name="**/Test*.java"/> <include name="**/*Test.java"/> <include name="**/*TestCase.java"/> </testsources> <!-- Tell Clover to get test results directory as defined in pom.xml. They will be listed on "Results" tab --> <testresults dir="target/test-report" includes="TEST-*.xml"/> </current> </clover-report> </target> </project>
mvn clean clover2:setup verify clover2:aggregate clover2:clover
Test Optimization feature is available for Surefire plugin. You you have to use:
Test Optimization feature for maven-failsafe-plugin is not available yet.
A sample project shows usage of Surefire and Failsafe plugins together.
Checkout code from Bitbucket: https://bitbucket.org/atlassian/maven-clover2-plugin
Go to: src/it/surefire-and-failsafe-plugins
Run mvn command with goals as specified in goals.txt file in this project. Use Maven 2.x or higher and Java5 or higher.
See also: