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.
Gradle runner: this is the default setting for Gradle tests.
Enabling JaCoco coverage
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.
Set up the coverage options - refer to the description of the available options below.
Option
Description
Example
Classfile directories or jars
Newline-delimited set of path patterns in the form of +|-:[path] to scan for classfiles to be analyzed. Libraries and test classfiles don't have to be listed unless their coverage is wanted.
+:target/main/java/**
Classes to instrument
Newline-delimited set of classname patterns in the form of +|-:[path]. Allows filtering out unwanted classes listed in "Classfile directories or jars" field. Useful in case test classes are compiled.
+:com.package.core.* -:com.package.*Test*
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.
Importing JaCoCo coverage data to TeamCity
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>']
Attribute name
Description
Default value
Example
dataPath
Space-delimited set of paths relative to the checkout directory to read the jacoco data file
Space-delimited set of paths relative to the checkout directory to read sources from. Does not need to be listed by default.
.
src
classpath
Space-delimited set of path patterns in the form of +|-:[path] to scan for classfiles to be analyzed. Libraries and test classfiles do not need to be listed unless their coverage is wanted.
+:**/*
+:target/main/java/**
reportDir
Path to the directory to store temporary files. The report will be generated as coverage.zip under this directory. Check that there is no existing directory with the same name.