Coverage thresholds help ensure your tests check enough of your code. They warn you if tests miss too much code.
Coverage thresholds in PyTest
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
PyTest
[run] branch = True [report] fail_under = 90
PyTest
[run] branch = True [report] fail_under = 75 show_missing = True
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.
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.
Practice
1. What does setting a coverage threshold in pytest ensure?
easy
Solution
Step 1: Understand coverage threshold purpose
Coverage thresholds set a minimum coverage percentage to maintain test quality.Step 2: Effect of coverage below threshold
If coverage is below the threshold, pytest fails the test run to alert missing tests.Final Answer:
The test run fails if coverage is below a set percentage -> Option CQuick Check:
Coverage threshold = fail if below limit [OK]
Hint: Coverage threshold means minimum coverage to pass tests [OK]
Common Mistakes:
- Thinking threshold speeds up tests
- Believing threshold skips tests
- Assuming threshold hides reports
2. Which is the correct pytest command to set a coverage fail threshold at 80%?
easy
Solution
Step 1: Recall pytest coverage threshold syntax
The correct option uses --cov-fail-under to set minimum coverage.Step 2: Match the correct command
pytest --cov-fail-under=80 matches the exact pytest CLI option for coverage fail threshold.Final Answer:
pytest --cov-fail-under=80 -> Option AQuick Check:
Correct CLI option = --cov-fail-under [OK]
Hint: Use --cov-fail-under to set coverage threshold [OK]
Common Mistakes:
- Mixing option order or names
- Using non-existent flags
- Confusing coverage threshold with other options
3. Given this pytest command:
and coverage report shows 85%, what will happen?
pytest --cov=myapp --cov-fail-under=90and coverage report shows 85%, what will happen?
medium
Solution
Step 1: Understand the coverage threshold set
The command sets a fail threshold at 90% coverage.Step 2: Compare actual coverage with threshold
Actual coverage is 85%, which is below 90%, so pytest fails the test run.Final Answer:
Tests fail due to coverage below 90% -> Option BQuick Check:
Coverage 85% < 90% threshold = fail [OK]
Hint: Coverage below threshold causes test failure [OK]
Common Mistakes:
- Assuming tests pass with warning
- Thinking coverage check is skipped
- Believing report is not generated
4. You set
--cov-fail-under=75 but pytest does not fail even when coverage is 70%. What is the likely cause?medium
Solution
Step 1: Check coverage plugin status
If coverage plugin is missing or disabled, threshold has no effect.Step 2: Understand threshold behavior
Threshold works only if coverage plugin runs and measures coverage.Final Answer:
Coverage plugin is not installed or enabled -> Option DQuick Check:
Missing plugin = threshold ignored [OK]
Hint: Ensure coverage plugin is active for thresholds to work [OK]
Common Mistakes:
- Thinking threshold values have minimum limits
- Believing coverage is ignored by default
- Using wrong command option names
5. You want to enforce different coverage thresholds for branches and statements: 80% for branches and 90% for statements. Which pytest configuration snippet achieves this?
hard
Solution
Step 1: Enable branch coverage in config
Set branch = True under [run] to measure branch coverage.Step 2: Set fail thresholds correctly
Under [report], fail_under sets statement threshold, fail_under_branch sets branch threshold.Final Answer:
[run]\nbranch = True\n[report]\nfail_under = 90\nfail_under_branch = 80 -> Option AQuick Check:
Branch coverage enabled + correct thresholds = [run]\nbranch = True\n[report]\nfail_under = 90\nfail_under_branch = 80 [OK]
Hint: Use [run] branch=True and [report] fail_under_branch for branches [OK]
Common Mistakes:
- Swapping statement and branch thresholds
- Using wrong config section names
- Missing branch = True setting
