JaCoCo
TeamCity supports JaCoCo, a Java Code Coverage tool allowing you to measure a wide set of coverage metrics and code complexity.
JaCoCo is available for the following build runners: Ant, IntelliJ IDEA Project, Gradle, and Maven.
tip
To ensure the coverage data is collected properly, make sure your tests run in (one or more) separate JVMs.
Ant and Intellij Idea Project runners: this is the default setting for TestNG, for Junit test task, set
fork=true
.Maven runner: set
forkCount
to a value higher than 0.Gradle runner: this is the default setting for Gradle tests.
TeamCity supports the Java agent coverage mode allowing you to collect coverage without modifying build scripts or binaries. No additional build steps needed — just choose JaCoCo coverage in a build step which runs tests:
In the Code Coverage section, select JaCoCo as a coverage tool in the Choose coverage runner drop-down menu.
Set up the coverage options — refer to the description of the available options below.
note
Note that this runner does not currently support automatic JaCoCo coverage configured for Android projects.
To provide JaCoCo coverage in Android projects, the build script should generate the.exec
files and import them as described in this section.
Option | Description | Example |
---|---|---|
Classfile directories or jars | Newline-delimited set of path patterns in the form of
|
|
Classes to instrument | Newline-delimited set of classname patterns in the form of |
|
tip
By default, in TeamCity the
jacoco.sources
property is set to.
, which means that TeamCity will scan whole checkout directory including all subdirectories for your sources.
Check that your classfiles are compiled with debug information (including the source file info) to see with highlighted source code in the report.
The code coverage results can be viewed on the Overview tab of the Build Results page; detailed report is displayed on the dedicated Code Coverage tab.
TeamCity can parse JaCoCo coverage data and generate a report using a service message of the following format:
##teamcity[jacocoReport dataPath='<path to jacoco.exec file>']
TeamCity will use the default JaCoCo version to parse the imported data. Note that if the imported coverage was generated by a different minor version (the second digit in X.X.X), TeamCity won't be able to process its classfiles and collect the coverage. To prevent this, make sure that the default version selected in Administration | Tools | JaCoCo Coverage corresponds to the tool version of the coverage you want to import.
Alternatively, you can specify the required tool version directly in the service message body: ##teamcity[jacocoReport dataPath='coverage_data/jacoco.exec' includes='*' classpath='+:build/classes/java/main/**' toolVersion='X.X.X']
. Make sure that the specified version is installed on the TeamCity server.
Attribute | Description | Default value | Example |
---|---|---|---|
dataPath | Space-delimited set of paths relative to the checkout directory to read the jacoco data file |
| |
includes | Space-delimited set of classname include patterns |
|
|
excludes | Space-delimited set of classname exclude patterns |
| |
sources | Space-delimited set of paths relative to the checkout directory to read sources from. Does not need to be listed by default. |
|
|
classpath | Space-delimited set of path patterns in the form of |
|
|
reportDir | Path to the directory to store temporary files. The report will be generated as coverage | A random directory under Agent's temp directory | jacocoReport |
tip
Use
\
(backslash) to escape space characters in paths.
An example of a complete service message:
##teamcity[jacocoReport dataPath='jacoco.exec' includes='com.package.core.*' classpath='classes/lib/some.jar' reportDir='temp/jacocoReport']
Thanks for your feedback!