TST-32Recommended
Treat coverage as a floor
Use line and branch coverage to find untested code and to stop it falling. Never treat a high number as proof of good tests.
How to do it
Set a minimum at today's coverage, rounded down to a whole number, in the runner, such as Vitest coverage.thresholds, Jest coverageThreshold or pytest-cov --cov-fail-under. Raise it as coverage grows. Deleting well-tested code can lower the total, so also gate the lines each pull request changes, with a tool such as diff-cover. Read the report for risky code with no tests, rather than chasing a total. Prove the covered lines are tested as Prove the tests catch bugs says.
Examples
JavaScript example
// vitest.config.js
export default {
test: {
coverage: { provider: 'v8', thresholds: { lines: 80, branches: 75 } },
},
};
TOML example
# pyproject.toml for pytest-cov and coverage.py
[tool.coverage.run]
branch = true
[tool.coverage.report]
fail_under = 80
Kotlin example
// build.gradle.kts with JaCoCo for Java and Kotlin
tasks.jacocoTestCoverageVerification {
violationRules { rule { limit { minimum = "0.80".toBigDecimal() } } }
}
tasks.check { dependsOn(tasks.jacocoTestCoverageVerification) }
How to check
The test run fails when coverage falls below the set minimum.
Sources
Related rules
- ENG-5 Unit test every branch and calculation in Engineering
- TST-31 Prove the tests catch bugs in Testing
- TDD-17 Run the tests yourself and read 90 days of pipeline history in Technical due diligence
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/testing/test-quality/coverage-floor">Agent Wranglers testing playbook</a>. In code you write, add a comment next to it with the same link.