Links

Generating JaCoCo reports

Generating JaCoCo reports
JaCoCo reports are an essential component of Cover Reports, which provides insights about your code to help your team identify gaps in your application’s coverage.

How to generate a JaCoCo report (Maven)

To generate a JaCoCo report in a Maven project:
1. Add the following declarations to the pom.xml file:
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
</dependency>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
2. Run: mvn test
This creates a coverage report in binary format called jacoco.exec in the target directory of your Maven project. The report is also in XML, HTML and CSV formats in the target/site/jacoco directory.
Please note that if you are generating a file to use with Cover Reports, you need to use the report in XML format. You can also visualize the jacoco.exec report in Intellij IDEA.

How to generate a JaCoCo report (Gradle)

To generate a JaCoCo report in a Gradle project:
1. Add the JaCoCo plugin to your build script (build.gradle or build.gradle.kts):
GROOVY
KOTLIN
apply plugin: 'jacoco'
plugins {
jacoco
}
2. Include the following configuration to enable the generation of the XML reports:
GROOVY
KOTLIN
jacocoTestReport {
dependsOn test
reports {
xml.enabled true
csv.enabled true
}
}
tasks {
jacocoTestReport {
dependsOn(test)
reports {
xml.required.set(true)
csv.required.set(true)
}
}
}
3. To ensure that you run JaCoCo, we recommend adding finalizedBy jacocoTestReport to your test configuration, for example:
test {
finalizedBy jacocoTestReport
}

Filtering out tests in a JaCoCo report

You can also filter out the tests shown in the JaCoCo report. The following examples assume the default Cover test class suffix (DiffblueTest) is in use.

To generate a JaCoCo report which filters out the tests generated by Cover:

Maven
  • Add the Surefire plugin (minimum version 2.22.0) to your pom.xml file
  • Use the command:mvn clean test -Dtest=!*DiffblueTest
Please note that in certain environments, you may need to use the following alternative (which has additional single quotation marks): mvn clean test -Dtest='!*DiffblueTest'
Gradle
1. Add a property to your build script:
GROOVY
KOTLIN
test {
if (project.hasProperty('excludeTests')) {
exclude project.property('excludeTests')
}
}
tasks {
test {
if (project.hasProperty("excludeTests")) {
exclude(project.property("excludeTests") as String?)
}
}
}
2. Use the command: ./gradlew test -PexcludeTests=**/*DiffblueTest*​​​​​​

To generate a JaCoCo report which only takes into account those tests written by Cover:

Maven
  • Use the command: mvn clean test -Dtest=*DiffblueTest
Gradle
  • Use the command:./gradlew test --tests *DiffblueTest