Skip to main content

Code Coverage

Code coverage shows what percentage of your code is exercised by tests. Harness displays coverage metrics in the Coverage tab of your pipeline execution, helping you identify untested code paths.

What You'll See

MetricDescription
Total CoveragePercentage of all lines covered by tests
Patch CoveragePercentage of changed lines covered (PRs only)
Per-File BreakdownCoverage percentage for each source file

For pull requests, the diff view shows line-by-line coverage:

  • Green - Line is covered
  • Red - Line is not covered
  • Yellow - Partially covered (some branches)

Supported Formats

FormatFile ExtensionLanguages
LCOV.info, .lcovPython, Node.js, C/C++
JaCoCo XML.xmlJava, Kotlin, Scala
Go Coverage.outGo

Pipeline Examples

- run:
script: |-
# Install dependencies
pip install pytest pytest-cov

# Run tests with coverage
pytest tests/ \
--junitxml=test-results.xml \
--cov=src \
--cov-report=lcov:lcov.info \
-v

# Upload test results
hcli test-reports upload test-results.xml

# Analyze coverage locally (optional)
hcli cov analyze --file lcov.info

# Upload coverage to Harness
hcli cov upload \
--file=lcov.info \
--provider=github \
--owner="$HARNESS_ORG_ID" \
--identifier="my-repo"
env:
CI_ENABLE_QUARANTINED_TEST_SKIP: "true"

Analyze Coverage Locally

Before uploading, you can analyze coverage locally:

hcli cov analyze --file lcov.info

This outputs coverage statistics without uploading to Harness, useful for debugging or local development.

Quality Gates

Enforce minimum coverage thresholds to fail pipelines that don't meet your standards. Use hcli cov wait-until to block the pipeline until coverage meets the specified thresholds:

hcli cov wait-until \
--gt=70 \
--patch-gt=80 \
--timeout=1m \
--endpoint="$HARNESS_TI_SERVICE_ENDPOINT/coverage"

This command:

  1. Waits for coverage data to be available
  2. Checks if total coverage exceeds the --gt threshold (70%)
  3. Checks if patch coverage exceeds the --patch-gt threshold (80%)
  4. Fails the pipeline if thresholds are not met

Example output when the quality gate fails:

Coverage: 84.53%
Lines: 579/685 covered
Patch Coverage: 78.57%
Patch Lines: 11/14 covered
Quality Gate FAILED:
- patch coverage 78.57% is not > 80.00%
Error: quality gate failed: 1 threshold(s) not met

Quality Gate Parameters

ParameterDescription
--gtMinimum total coverage percentage required
--patch-gtMinimum patch coverage percentage required (for PRs)
--timeoutHow long to wait for coverage data (e.g., 1m, 30s)
--endpointCoverage service endpoint

CLI Reference

cov upload

hcli cov upload \
--file=<coverage-file> \
--provider=<provider> \
--owner=<owner> \
--identifier=<repo-name>
ParameterRequiredDescription
--fileYesPath to coverage file
--providerYesGit provider: github, gitlab, bitbucket, or Harness
--ownerYesOrganization or owner identifier
--identifierYesRepository name

cov analyze

hcli cov analyze --file <coverage-file>
ParameterRequiredDescription
--fileYesPath to coverage file

cov wait-until

hcli cov wait-until \
--gt=<total-threshold> \
--patch-gt=<patch-threshold> \
--timeout=<duration> \
--endpoint=<endpoint>
ParameterRequiredDescription
--gtNoMinimum total coverage percentage (e.g., 70)
--patch-gtNoMinimum patch coverage percentage (e.g., 80)
--timeoutNoWait timeout (e.g., 1m, 30s). Default: 30s
--endpointYesCoverage service endpoint ($HARNESS_TI_SERVICE_ENDPOINT/coverage)

Troubleshooting

IssueSolution
Coverage shows 0%Verify the coverage file path matches your tool's output
Patch coverage not showingEnsure you're running on a pull request build
File not foundConfirm coverage file was generated before the upload step
Invalid formatVerify the file format matches supported types (LCOV, JaCoCo, Go)

Next Steps