Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging Integration and Unit test reports with JaCoCo

I am generating failsafe and surefire reports with maven with the JaCoCo plugin, but I can only manage to get them in separate reports. I would like to have a overall coverage view (merge between both Unit Tests and Integration Tests) as well.

After what I think was a thorough google search I could only found a way to do this with Sonar. Is there any simpler way to do this?

Related question: Maven separate Unit Test and Integration Tests

like image 733
raf Avatar asked Oct 26 '15 15:10

raf


People also ask

Does JaCoCo include integration tests?

Execution data for integration tests Thankfully a big piece of the puzzle is already solved for us simply by including the java and jacoco plugins. With this plugin combo Gradle automatically enriches any task of type Test to also produce Jacoco code coverage execution data.

What is JaCoCo merge?

merge: This goal helps in merging a set of execution data files (*. exec) into a single file. Since every module has an individual *. exec file generated by jacoco. This merge goal merges those files specified in includes property into a single aggregate file to the given location in destFile .

What is JaCoCo aggregate?

Description: Creates a structured code coverage report (HTML, XML, and CSV) from multiple projects within reactor. The report is created from all modules this project depends on, and optionally this project itself. From those projects class and source files as well as JaCoCo execution data files will be collected.

Does code coverage include integration tests?

As a Developer or Tester, you can absolutely include Integration Testing in Code Coverage, if you have integrated code scripts.


2 Answers

I recently implemented this: after some headaches and a lot of testing, I have a configuration that works beautifully.

<plugin>     <groupId>org.jacoco</groupId>     <artifactId>jacoco-maven-plugin</artifactId>     <version>${jacoco.version}</version>     <executions>         <execution>             <id>pre-unit-test</id>             <goals>                 <goal>prepare-agent</goal>             </goals>             <configuration>                 <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>                 <propertyName>surefireArgLine</propertyName>             </configuration>         </execution>         <execution>             <id>pre-integration-test</id>             <goals>                 <goal>prepare-agent-integration</goal>             </goals>             <configuration>                 <destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>                 <propertyName>testArgLine</propertyName>             </configuration>         </execution>         <execution>             <id>post-integration-test</id>             <phase>post-integration-test</phase>             <goals>                 <goal>report</goal>             </goals>             <configuration>                 <dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>                 <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>             </configuration>         </execution>         <execution>             <id>post-unit-test</id>             <phase>prepare-package</phase>             <goals>                 <goal>report</goal>             </goals>             <configuration>                 <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>                 <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>             </configuration>         </execution>         <execution>             <id>merge-results</id>             <phase>verify</phase>             <goals>                 <goal>merge</goal>             </goals>             <configuration>                 <fileSets>                     <fileSet>                         <directory>${project.build.directory}/coverage-reports</directory>                         <includes>                             <include>*.exec</include>                         </includes>                     </fileSet>                 </fileSets>                 <destFile>${project.build.directory}/coverage-reports/aggregate.exec</destFile>             </configuration>         </execution>         <execution>             <id>post-merge-report</id>             <phase>verify</phase>             <goals>                 <goal>report</goal>             </goals>             <configuration>                 <dataFile>${project.build.directory}/coverage-reports/aggregate.exec</dataFile>                 <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>             </configuration>         </execution>     </executions> </plugin> <plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-surefire-plugin</artifactId>     <version>2.18.1</version>     <configuration>         <argLine>${surefireArgLine}</argLine>         <skipTests>${skip.unit.tests}</skipTests>         <includes>             <include>**/*UT.java</include>             <include>**/*MT.java</include>             <include>**/*Test.java</include>         </includes>         <skipTests>${skipUTMTs}</skipTests>     </configuration> </plugin> <plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-failsafe-plugin</artifactId>     <version>2.12.4</version>     <configuration>         <skipTests>${skipTests}</skipTests>         <skipITs>${skipITs}</skipITs>         <argLine>${testArgLine}</argLine>         <excludes>             <exclude>**/*UT*.java</exclude>         </excludes>     </configuration>     <executions>         <execution>             <goals>                 <goal>integration-test</goal>                 <goal>verify</goal>             </goals>         </execution>     </executions> </plugin> 

As you can see, there are 6 distinct Jacoco executions to run the tests, merge the files and create an aggregate report. On top of the Jacoco config, you also need to configure Surefire and Failsafe to take an argument from Jacoco (Surefire runs the unit tests and Failsafe runs the integration tests).

All of the configuration that I used should be there, what you do with it is your design architecture to make it fit within your desired requirements. Personally, I recommend a look into what I exclude and include within surefire and failsafe if you are having issues with files not being read.

like image 152
Chad Van De Hey Avatar answered Oct 13 '22 22:10

Chad Van De Hey


Not the answer you're looking for, but still...

In most cases, you should not merge coverage from unit and integration tests.

The value of unit tests is that they improve the design of your application and ensure that corner cases of your code is working correctly. You should try to have a high branch coverage of your unit tests.

The value of your integration tests is that they ensure that the main use cases of your application are working correctly and that the whole stack is integrated correctly. You should try to have a high functional coverage for your integration tests. (But it is fairly hard to measure functional coverage with a tool).

If you need integration tests to improve your branch coverage, that's a strong indication that you should review the design of your code. And if you already have a high branch coverage without integration tests, adding them should not modify significantly your metrics.

Feel free to down vote this answer as it is a bit of topic and fairly opinionated...

like image 22
Guillaume Avatar answered Oct 13 '22 22:10

Guillaume