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.
Gate in the Test Runner
The simplest gate lives in the tool's own config, so it also fails locally.
{ "jest": { "coverageThreshold": { "global": { "lines": 80, "branches": 70, "functions": 85 }, "./src/payments/": { "lines": 95 } } }}Gate as a Separate CI Job
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 fiDiff 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.
python -m pytest --cov=src --cov-report=xmldiff-cover coverage.xml --compare-branch=origin/main --fail-under=80Troubleshooting
| 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 |
TipUse per-directory thresholds. Require 95% on payments code and a lower bar on utilities, so effort goes where risk is highest.
RememberCoverage measures executed code, not test quality. A test with no assertions still gives 100%.
SecurityPrioritise coverage of authentication, authorisation, and input validation. Untested branches in these paths are where security bugs hide.
Common MistakeSetting 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.