Coverage shows how much of your code is tested. It helps find parts not checked by tests.
Why coverage measures test completeness 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
pytest --cov=your_package tests/
This command runs tests and measures coverage for 'your_package'.
Replace 'your_package' with the folder or module you want to check.
Examples
PyTest
pytest --cov=myapp tests/
PyTest
pytest --cov=module_name test_module.py
PyTest
pytest --cov=src --cov-report=term-missing tests/
Sample Program
This simple code has two functions: add and subtract. The test only checks add.
Running coverage will show subtract is not tested.
PyTest
def add(a, b): return a + b def subtract(a, b): return a - b def test_add(): assert add(2, 3) == 5 # Note: No test for subtract function
Important Notes
Coverage does not guarantee your tests find all bugs, only that code runs during tests.
High coverage is good, but tests must also check correct behavior.
Use coverage reports to improve tests by adding missing cases.
Summary
Coverage measures how much code your tests run.
It helps find untested parts of your program.
Use coverage to improve test completeness and quality.
Practice
1. What does test coverage measure in pytest?
easy
Solution
Step 1: Understand the meaning of coverage
Coverage shows which parts of the code are run when tests execute.Step 2: Compare options to coverage definition
Only How much of the code is executed by tests matches this meaning, others describe different test aspects.Final Answer:
How much of the code is executed by tests -> Option AQuick Check:
Coverage = executed code percentage [OK]
Hint: Coverage = code run by tests, not test count [OK]
Common Mistakes:
- Confusing coverage with number of tests
- Thinking coverage measures test speed
- Believing coverage counts errors found
2. Which pytest command correctly runs tests with coverage measurement?
easy
Solution
Step 1: Recall pytest coverage plugin syntax
The correct flag to measure coverage is '--cov'.Step 2: Check options for correctness
Only pytest --cov uses the exact correct flag '--cov'. Others are invalid or incomplete.Final Answer:
pytest --cov -> Option DQuick Check:
Use --cov to enable coverage [OK]
Hint: Use '--cov' flag to measure coverage in pytest [OK]
Common Mistakes:
- Using '--coverage' instead of '--cov'
- Mixing coverage report flags with coverage run flags
- Typing '--cover' which is invalid
3. Given this pytest coverage output:
What does the 'Miss' number mean?
Name Stmts Miss Cover my_module.py 10 2 80%
What does the 'Miss' number mean?
medium
Solution
Step 1: Understand coverage report columns
'Miss' shows how many lines of code were not run by tests.Step 2: Match 'Miss' meaning to options
Number of lines not executed by tests correctly describes 'Miss' as unexecuted lines; others describe unrelated test results.Final Answer:
Number of lines not executed by tests -> Option AQuick Check:
Miss = untested lines count [OK]
Hint: 'Miss' means lines tests did not run [OK]
Common Mistakes:
- Thinking 'Miss' counts failed tests
- Confusing 'Miss' with skipped tests
- Assuming 'Miss' means code errors
4. You run pytest with coverage but get 0% coverage report. What is the most likely cause?
medium
Solution
Step 1: Analyze 0% coverage meaning
0% coverage means no code lines were run during tests.Step 2: Evaluate causes
If the coverage plugin is not installed, pytest will not measure coverage and may silently produce 0% coverage report or no coverage data.Final Answer:
Coverage plugin is not installed -> Option CQuick Check:
Missing plugin causes no coverage data [OK]
Hint: 0% coverage often means coverage plugin missing [OK]
Common Mistakes:
- Assuming plugin missing causes 0% without errors
- Thinking fast tests mean low coverage
- Believing code without functions can't be covered
5. You want to improve test completeness using coverage. Which approach is best?
hard
Solution
Step 1: Understand coverage report use
Coverage shows which code lines lack tests, guiding where to add tests.Step 2: Evaluate options for improving completeness
Only Add tests targeting uncovered code lines shown by coverage report uses coverage data to add tests for uncovered code, improving completeness.Final Answer:
Add tests targeting uncovered code lines shown by coverage report -> Option BQuick Check:
Use coverage to find and test missing code [OK]
Hint: Add tests where coverage report shows gaps [OK]
Common Mistakes:
- Writing tests blindly without coverage info
- Ignoring coverage to focus on speed
- Removing tests that cover code
