Test Intelligence with Code Coverage
Collect code coverage while Test Intelligence skips tests
Normally, Test Intelligence and code coverage pull in opposite directions: Test Intelligence speeds up builds by running only the tests affected by your changes, while coverage numbers assume every test ran. Running both naively makes coverage collapse on every pull request, because the tests that were skipped look like tests that covered nothing.
Test Intelligence with Code Coverage resolves that. When you enable both, Harness records coverage for each test individually as your tests run, and reuses the stored coverage for tests that were skipped. You get the speed of Test Intelligence and coverage numbers that reflect your whole suite.
Harness also uses this data when deciding what to skip: a test is only skipped when coverage for it has already been recorded. If a test has no coverage on file, it runs, even if Test Intelligence would otherwise have skipped it.
What you will learn from this topic
How Test Intelligence and code coverage work together without collapsing coverage on pull requests
How to configure coverage in your build for Java and Python
How to run tests through the Harness test executor (
hcli htx)How to set the coverage output location and upload per-test results
Which build tools, test frameworks, and coverage tools support per-test coverage attribution
Before you begin
You need:
A Test step with Intelligence Mode enabled. Go to Test Intelligence™ step to set one up.
A coverage tool already configured in your build. Harness does not add one for you. Go to Step 1.
A test command that runs through the Harness test executor,
hcli htx. This is what wires test selection and coverage collection together, and it is required. Go to Step 2.At least one previous run on the branch, so Test Intelligence has a baseline. Go to Trigger test selection for how baselines are established.
Supported languages
Java and other JVM languages
JUnit 5 / JUnit Platform
JaCoCo
Python
pytest
coverage.py / pytest-cov
Other languages supported by Test Intelligence do not yet produce per-test coverage. For those, use standard code coverage as described in the Onboarding Guide.
Step 1: Configure coverage in your build
Harness collects coverage using the coverage tool your build already runs. If your build has no coverage tool configured, the step still succeeds and your tests still run, but no coverage is produced.
JaCoCo must be configured in your build. Harness does not bundle a copy of JaCoCo and will not add one.
For Maven, add the jacoco-maven-plugin to your pom.xml. For Gradle, apply the jacoco plugin in your build.gradle.
If JaCoCo is missing, the step log shows:
Your underlying build command does not change. You still run mvn test or ./gradlew test, but through the test executor as shown in Step 2.
No build configuration is required. Harness configures coverage collection for you, and installs coverage and pytest-cov into the step environment if they are not already present.
Your underlying test command does not change. You still run pytest, but through the test executor as shown in Step 2.
By default, only source files that your tests import appear in the results. To report files that no test touched as 0% rather than omitting them, set TI_COVERAGE_SOURCE to your source directory:
Use a colon to list more than one directory, for example src:lib.
Step 2: Run your tests through the test executor
Per-test coverage works out of the box only when your tests run through the Harness test executor, hcli htx. Put hcli htx -- in front of the command you already run:
Everything after -- is your own command, passed through untouched. Keep your existing flags, profiles, and arguments exactly as they are.
To split tests across parallel steps, pass -p with the number of workers, for example hcli htx -p 16 -- pytest. Go to Parallelism for how to size this.
Step 3: Set the coverage output location
Set TI_COVERAGE_OUTPUT_PATH on the Test step to a directory inside your workspace. This is where Harness writes the coverage results, and it is the path you upload in the next step.
Step 4: Upload the coverage results
Coverage is not uploaded automatically. Add an upload command so the results reach the Harness UI.
Harness writes one results file per step, named with a _tiRunner.json suffix, into the directory from Step 3. Point hcli cov upload at that directory:
If you run the Test step with parallelism, each parallel step writes its own results file. Upload them all, and Harness combines them into a single coverage number.
Step 5: Add a quality gate (optional)
Coverage collected this way works with quality gates like any other coverage data:
Go to Onboarding Guide for the full set of threshold options.
Verify your setup
Confirm per-test coverage is working end to end:
Run the pipeline twice on the same branch. The first run establishes the Test Intelligence baseline and runs every test.
On the second run, confirm the Tests tab shows tests being skipped.
Go to the Coverage tab and confirm coverage is reported for the full codebase, not only the tests that ran.
Go to Understanding Results for how to read the coverage numbers.
Coverage opt-out
To run Test Intelligence without collecting coverage, set HTX_DISABLE_COVERAGE to any value on the step:
Tests still run and Test Intelligence still selects tests, but no coverage is collected and none is uploaded. Turning coverage off also removes the coverage check from test selection, so Test Intelligence goes back to skipping tests based on code changes alone.
Supported configurations
Coverage is always collected when a supported coverage tool is present. The table below describes when Harness can attribute coverage to individual tests. Where a configuration is unsupported for per-test attribution, coverage is still collected for the run as a whole: you keep your coverage numbers, but skipped tests cannot be credited with their stored coverage.
Maven with Surefire 3.x
Supported
Gradle 7.3 and later
Supported
Maven with Failsafe
Expected to work, not yet verified
Bazel, Ant, sbt
Not supported
JUnit 5 (Jupiter)
Supported
JUnit 4 through the Vintage engine
Expected to work, not yet verified
Spock
Expected to work, not yet verified
JUnit 4 run without JUnit Platform
Whole-run coverage only
TestNG
Whole-run coverage only
JUnit 5 features such as @Nested, @ParameterizedTest, and @Suite are all supported.
JaCoCo
Supported
Cobertura, Clover, OpenClover
Not supported
For Python, coverage.py 7.0 or later is required. Harness installs a compatible version if one is not present.
Things to know
A few behaviors are worth keeping in mind:
Coverage is tracked per test file. A test file is the smallest unit Harness attributes coverage to, for both Java and Python. Individual test methods within a file are not tracked separately.
Tests run sequentially within each test process. Harness runs tests in order inside each process so it can tell which test covered which lines. Running steps in parallel is unaffected, and so is Test Intelligence parallelism.
If you force parallel execution inside a test process, for example with JUnit's
@Execution(CONCURRENT)or by settingjunit.jupiter.execution.parallel.enabledyourself, Harness respects your setting and falls back to whole-run coverage.Gradle daemons started before the step began are not instrumented. If coverage is missing on a Gradle build, run with
--no-daemon.
Troubleshooting
Next steps
Go to Understanding Results to interpret coverage metrics and set up PR comments.
Go to Advanced Settings to exclude files from coverage and roll the setup out across an organization.
Go to Test Intelligence™ overview to learn how test selection works.
Last updated
Was this helpful?