This blog post describes how we use the Jacobo Maven plug-in to create code coverage reports for unit and integration tests.
Our construction requirements are as follows:
When running tests, our build must create code coverage reports for unit and integration tests. Code coverage reports must be created in a separate directory. In other words, the code coverage report for unit tests must be created in a different directory than the code coverage report for integration tests. Let's start.
Configuring the JaCoCo Maven plug-in
We use the Jacobo Maven plug-in for two purposes:
- It gives us access to the Jacobo runtime agent, which records execution coverage data.
- It creates code coverage reports based on execution data recorded by the JaCo Co runtime agent.
- We can configure the Jacobo Maven plug-in as follows:
Add the Jacobo Maven plug-in to the plug-in section of our POM file.
- Configure code coverage reports for unit tests.
- Configure code coverage reports for integration testing. These steps are described in more detail below.
Add the Jacobo Maven plug-in to the POM file
We can add the Jacobo Maven plug-in to our POM file by adding the following plug-in declaration to its "plug in" section:
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.6.3.201306030806</version> </plugin>
Configuring code coverage reports for unit tests
We can configure code coverage reports for unit tests by adding two executions to the plug-in declaration. These are carried out as follows:
- The first execution creates a property that points to the Jacobo runtime agent. Make sure the execution data is written to the file target / coverage reports / JaCoCo ut.exec. Set the name of the property to surfireargline. When you run a unit test, the value of this property is passed as a VM parameter.
- After running the unit test, the second execution creates a code coverage report for the unit test. Make sure to read the execution data from the file target / coverage reports / jacoco ut.exec and write the code coverage report to the directory target / site / jacoco ut.
The relevant parts of our plug-in configuration are as follows:
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.6.3.201306030806</version> <executions> <!-- Prepares the property pointing to the JaCoCo runtime agent which is passed as VM argument when Maven the Surefire plugin is executed. --> <execution> <id>pre-unit-test</id> <goals> <goal>prepare-agent</goal> </goals> <configuration> <!-- Sets the path to the file which contains the execution data. --> <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile> <!-- Sets the name of the property containing the settings for JaCoCo runtime agent. --> <propertyName>surefireArgLine</propertyName> </configuration> </execution> <!-- Ensures that the code coverage report for unit tests is created after unit tests have been run. --> <execution> <id>post-unit-test</id> <phase>test</phase> <goals> <goal>report</goal> </goals> <configuration> <!-- Sets the path to the file which contains the execution data. --> <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile> <!-- Sets the output directory for the code coverage report. --> <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory> </configuration> </execution> </executions> </plugin>
Let's find out how to configure code coverage reports for integration tests.
Configure code coverage reports for integration tests
We can configure code coverage reports for integration tests by adding two executions to the plug-in declaration. These are carried out as follows:
- The first execution creates a property that points to the Jacobo runtime agent. Make sure to write the execution data to the file target / coverage reports / JaCoCo-it.exec. Set the name of this property to failsafeArgLine. When running our integration test, the value of this property is passed as a VM parameter.
- Create an execution that creates a code coverage report for the integration test after it runs. Make sure to read the execution data from the file target / coverage reports / jacoco it.exec and write the code coverage report to the directory target / site / jacoco it.
The relevant parts of our plug-in configuration are as follows:
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.6.3.201306030806</version> <executions> <!-- The Executions required by unit tests are omitted. --> <!-- Prepares the property pointing to the JaCoCo runtime agent which is passed as VM argument when Maven the Failsafe plugin is executed. --> <execution> <id>pre-integration-test</id> <phase>pre-integration-test</phase> <goals> <goal>prepare-agent</goal> </goals> <configuration> <!-- Sets the path to the file which contains the execution data. --> <destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile> <!-- Sets the name of the property containing the settings for JaCoCo runtime agent. --> <propertyName>failsafeArgLine</propertyName> </configuration> </execution> <!-- Ensures that the code coverage report for integration tests after integration tests have been run. --> <execution> <id>post-integration-test</id> <phase>post-integration-test</phase> <goals> <goal>report</goal> </goals> <configuration> <!-- Sets the path to the file which contains the execution data. --> <dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile> <!-- Sets the output directory for the code coverage report. --> <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory> </configuration> </execution> </executions> </plugin>
Now we have the JaCoCo Maven plug-in configured. The next step is to configure the Maven Surefire plug-in. Let's figure out how to do that.
Configuring the Maven Surefire plug-in
We use the Maven Surefire plug-in to run unit tests for the sample application. Because we are going to create code coverage reports for unit tests, we have to make sure that the Jacobo agent is running when we run unit tests. We can guarantee this surefire argLine property as the value argLine configuration parameter through the added value.
The configuration of the Maven Surefire plug-in is as follows (highlighting the required changes):
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.15</version> <configuration> <!-- Sets the VM argument line used when unit tests are run. --> <argLine>${surefireArgLine}</argLine> <!-- Skips unit tests if the value of skip.unit.tests property is true --> <skipTests>${skip.unit.tests}</skipTests> <!-- Excludes integration tests when unit tests are run. --> <excludes> <exclude>**/IT*.java</exclude> </excludes> </configuration> </plugin>
We're almost done. The rest is to configure the Maven Failsafe plug-in. Let's figure out how to do that.
Configure Maven fail safe plug in
The integration test of our sample application is run by the Maven Failsafe plug-in. Because we are creating code coverage reports for integration tests, we have to make sure that the Jacobo agent is running when we run integration tests. We can do this by adding the value of the failsafeArgLine attribute to the value of the argLine configuration parameter.
The configuration of the Maven Failsafe plug-in is as follows (highlighting the required changes):
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.15</version> <executions> <!-- Ensures that both integration-test and verify goals of the Failsafe Maven plugin are executed. --> <execution> <id>integration-tests</id> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> <configuration> <!-- Sets the VM argument line used when integration tests are run. --> <argLine>${failsafeArgLine}</argLine> <!-- Skips integration tests if the value of skip.integration.tests property is true --> <skipTests>${skip.integration.tests}</skipTests> </configuration> </execution> </executions> </plugin>
Create code coverage report
Now we have successfully completed the required configuration. Let's see how to create code coverage reports for unit tests and integration tests.
The sample application for this blog post has three build profiles, which are described below:
-
It is used during the development of configuration file, which is the default configuration file we build. When this profile is active, only unit tests are run.
-
The integration test profile is used to run integration tests.
-
All test profiles are used to run unit tests and integration tests for. We can create different code coverage reports by running the following command at the command prompt:
-
Command mvn clean test to run the unit test and create a code coverage report for the unit test for the directory target / site / jacoco ut.
-
Command MVN clean verify - P integration test to run integration tests and create code coverage reports for integration tests for the directory target / site / jacoco it.
-
Command MVN clean verify - P all tests to run unit tests and integration tests, and create code coverage reports for unit tests and integration tests.
Selected technical articles
- java one line code printing heart
- Chinese version of Linux performance monitoring software netdata
- Interface test code coverage (jacoco) scheme sharing
- Performance testing framework
- How to test the performance of Linux command line interface happily
- Diagram HTTP brain map
- Automatically turn a swagger document into test code
- Five line code to build static blog
- Research on the testing framework of linear interface based on java
- Practice in JUnit for Selenium testing
Selected non-technical articles
- Why choose software testing as a career path?
- Programming thinking for all
- Seven steps to become an excellent automation test engineer
- Important reasons for manual testing
- 7 skills to become an automated test
- Automatic and manual testing, keep balance!
- Automated test life cycle
- How to introduce automated testing in DevOps