Coverage thresholds help ensure your tests check enough of your code. They warn you if tests miss too much code.
0
0
Coverage thresholds in PyTest
Introduction
When you want to make sure your tests cover at least 80% of your code.
When you want to prevent new code from lowering your test coverage.
When you want to fail a test run if coverage is below a set limit.
When you want to track coverage progress over time.
When you want to enforce quality rules in a team project.
Syntax
PyTest
[run] branch = True [report] fail_under = 80
This example is from a pytest.ini file configuring coverage.
fail_under sets the minimum coverage percent required.
Examples
Set coverage threshold to 90%. Test run fails if coverage is below 90%.
PyTest
[run] branch = True [report] fail_under = 90
Set threshold to 75% and show which lines are missing coverage.
PyTest
[run] branch = True [report] fail_under = 75 show_missing = True
Set threshold to 80% and skip files fully covered in the report.
PyTest
[run] branch = True [report] fail_under = 80 skip_covered = True
Sample Program
This config sets a coverage threshold of 80%. When you run pytest with coverage, it checks if coverage is at least 80%. If not, the test run fails.
PyTest
[run] branch = True [report] fail_under = 80 # Run pytest with coverage: # pytest --cov=my_module # If coverage is below 80%, pytest will fail the test run.
OutputSuccess
Important Notes
Coverage thresholds help keep your tests effective over time.
You can set different thresholds for lines, branches, or functions.
Use pytest --cov-report=term-missing to see which lines lack tests.
Summary
Coverage thresholds set a minimum test coverage percentage.
If coverage is below the threshold, the test run fails.
This helps maintain good test quality and catch missing tests early.