Skip to main content

Test Coverage Gate

A test coverage gate is a CI rule that fails the build when code coverage falls below a set threshold, preventing new code from merging without tests.

Understanding Test Coverage Gates

What Is a Coverage Gate and Why Use One

A coverage gate is a contract: every pull request must keep coverage at or above an agreed level, or it cannot merge. Without it, coverage erodes quietly under deadline pressure until the suite offers little safety. With it, the pipeline fails and the author writes tests or refactors.

Types of Coverage

Type What it measures
Line Share of lines run by at least one test
Branch Share of if and else paths exercised
Function Share of functions called

Line coverage is the most common gate. Branch coverage is stricter and catches untested else paths.

flowchart TD A[Run tests with coverage] --> B[Coverage report] B --> C{Meets threshold?} C -->|Yes| D[Pipeline passes] C -->|No| E[Pipeline fails, PR blocked] classDef run fill:#326CE5,color:#FFFFFF classDef good fill:#10B981,color:#FFFFFF classDef bad fill:#EF4444,color:#FFFFFF class A,B,C run class D good class E bad

Gate in the Test Runner

The simplest gate lives in the tool's own config, so it also fails locally.

JSON
{
"jest": {
"coverageThreshold": {
"global": { "lines": 80, "branches": 70, "functions": 85 },
"./src/payments/": { "lines": 95 }
}
}
}

Gate as a Separate CI Job

YAML
coverage-gate:
runs-on: ubuntu-latest
needs: unit-test
steps:
- uses: actions/download-artifact@v4
with:
name: coverage-report
path: coverage/
- name: Check thresholds
run: |
LINES=$(jq '.total.lines.pct' coverage/coverage-summary.json)
BRANCHES=$(jq '.total.branches.pct' coverage/coverage-summary.json)
echo "Lines: $LINES Branches: $BRANCHES"
if (( $(echo "$LINES < 80" | bc -l) )) || (( $(echo "$BRANCHES < 70" | bc -l) )); then
echo "::error::Coverage below threshold"
exit 1
fi

Diff Coverage for New Code

Total coverage can hide untested new code in a well-tested legacy codebase. Diff or patch coverage checks only the lines changed in the pull request, for example with Codecov patch coverage or the diff-cover tool.

Bash
python -m pytest --cov=src --cov-report=xml
diff-cover coverage.xml --compare-branch=origin/main --fail-under=80

Troubleshooting

Symptom Check What to look for
Coverage dropped suddenly git diff and report Uncovered new code
Gate passes, quality low Branch coverage Switch to stricter metric
Coverage gaming Test assertions Tests that run code without asserting
Tip

Use per-directory thresholds. Require 95% on payments code and a lower bar on utilities, so effort goes where risk is highest.

Remember

Coverage measures executed code, not test quality. A test with no assertions still gives 100%.

Security

Prioritise coverage of authentication, authorisation, and input validation. Untested branches in these paths are where security bugs hide.

Common Mistake

Setting the threshold far above current coverage overnight, so every PR fails. Start at the current level and raise it a few points per quarter.

Frequently Asked Questions

What coverage threshold should a team enforce?

Most teams enforce around 70 to 80% on lines or branches rather than 100%. Chasing 100% encourages low-value tests, while a floor prevents large untested chunks of new logic from merging.

How do teams game a coverage gate?

By calling code without asserting results, which raises the number but catches nothing. Reviewing assertions and using diff coverage on changed lines reduces this.

Should the gate use total coverage or diff coverage?

Both help. A total-coverage floor prevents decay, while diff coverage ensures the code in each pull request is actually tested even when the legacy baseline is high.

Found a mistake, an outdated step or a broken link?