Knowledge Base > Cover CLI 3.0 > Generating coverage reports
Generating an HTML report highlighting new coverage
Highlighting new coverage
To visualize the new coverage gained by writing tests with Cover CLI 3.0, dcover
has a subcommand called coverage-diff
that generates a JaCoCo HTML report with the new coverage explicitly highlighted. For example, after writing tests a java-demo
example the report might look as follows:
The new coverage is shown in blue in coverage bars and as percentage increases, and for each coverage counter in the right portion of the table (cyclomatic complexity, lines, methods and classes) a column called Increased
is added with the total number of newly covered elements.
Using JaCoCo
JaCoCo Java Code Coverage is a widely used code coverage report generator. For example, to use it in a Maven project 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>
Running mvn test
will now result in creating a coverage report in binary format called jacoco.exec
in the target
directory of your Maven project. The report will also be interpreted in a readable code coverage report in HTML format in target/site/jacoco
directory.
Generating an HTML report with new coverage
To generate the HTML report, use dcover
with subcommand coverage-diff
.
For example, running the following command from the root of the java-demo
project:
dcover coverage-diff target/jacocoOld.exec target/jacocoNew.exec
will generate the HTML report for a coverage report target/jacocoNew.exec
while highlighting the increase in coverage compared to a coverage report target/jacocoOld.exec
, in target/site/jacoco
.
Additional optional arguments may also be provided:
–classpath or -cp
Example:
-cp "my-project/target/classes"
Provide the path to class files of the project you want to generate the HTML report for. Multiple paths can be specified using :
as separators for Linux/macOS and ;
for Windows. All packages, classes and methods found in the listed directories and jar files will show in the generated report and at least all files of the project itself must be on the path. For example, if a jar with dependencies is provided then all packages and classes from the dependencies will be appear in the coverage report.
Default: target/classes
–source-dir or -s
Example:
-s "my-project/src/main/java"
Provide the relative path for the directory with source files. The source files are part of the HTML report and the coverage is marked using green and red lines. Note that at the moment, the newly covered lines are also shown in green, i.e., not distinguished from the old coverage.
Default: src/main/java
–report-output-dir or -d
Example:
-d "my-project/target/site/jacoco"
Provide the relative path for the directory where the HTML report should be saved.
Default: target/site/jacoco
–working-directory
Example:
--working-directory "my-project"
Set the working directory for running dcover
, which should be the root directory of the project or module. If provided, all paths specified in other options should be either absolute or relative to the working directory. This is also used for setting the report header.
Default: .