Knowledge Base > DCover CLI > 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 Diffblue Cover Reports, you need to use the report in XML format.

How to generate a JaCoCo report (Gradle)

To generate a JaCoCo report in a Gradle project:

1. Add the JaCoCo plugin to your build.gradle file:

apply plugin: 'jacoco'

2. Include the following configuration to enable the generation of the xml reports:

jacocoTestReport {
   dependsOn test
   reports {
       xml.enabled true
       csv.enabled 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 DCover test class suffix (DiffblueTest) is in use.

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

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 build.gradle:

test {
    if (project.hasProperty('excludeTests')) {
        exclude project.property('excludeTests')
    }
}

2. Use the command: ./gradlew test -PexcludeTests=**/*DiffblueTest*​​​​​​

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

Maven

  • Use the command: mvn clean test -Dtest=*DiffblueTest

Gradle

  • Use the command:./gradlew test --tests *DiffblueTest