Why coverage measures test completeness in PyTest - Automation Benefits in Action
Start learning this pattern below
Jump into concepts and practice - no test required
import subprocess import re def square(x): return x * x def test_square_one(): assert square(3) == 9 # Run coverage for initial test result1 = subprocess.run(['coverage', 'run', '-m', 'pytest', '-q', '--tb=line'], capture_output=True, text=True) result_report1 = subprocess.run(['coverage', 'report'], capture_output=True, text=True) # Extract coverage percent for the file match1 = re.search(r'square.py\s+\d+\s+\d+\s+(\d+)%', result_report1.stdout) coverage1 = int(match1.group(1)) if match1 else 0 # Add second test to cover all lines def test_square_zero(): assert square(0) == 0 # Run coverage again with both tests result2 = subprocess.run(['coverage', 'run', '-m', 'pytest', '-q', '--tb=line'], capture_output=True, text=True) result_report2 = subprocess.run(['coverage', 'report'], capture_output=True, text=True) match2 = re.search(r'square.py\s+\d+\s+\d+\s+(\d+)%', result_report2.stdout) coverage2 = int(match2.group(1)) if match2 else 0 # Assertions assert coverage1 < 100, f"Expected coverage less than 100%, got {coverage1}%" assert coverage2 == 100, f"Expected coverage 100%, got {coverage2}%"
This script defines a simple function square and two pytest test functions.
First, it runs pytest with coverage enabled with only one test, then captures the coverage report and extracts the coverage percentage.
Then it adds a second test to cover all code paths and runs coverage again.
Finally, it asserts that the first coverage is less than 100% and the second coverage is exactly 100%, showing how coverage measures test completeness.
Using subprocess allows running coverage commands programmatically.
Regex extracts coverage percent from the report output.
This approach teaches how coverage helps identify missing tests and improve completeness.
Now add data-driven testing with 3 different inputs to the square function
Practice
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]
- Confusing coverage with number of tests
- Thinking coverage measures test speed
- Believing coverage counts errors found
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]
- Using '--coverage' instead of '--cov'
- Mixing coverage report flags with coverage run flags
- Typing '--cover' which is invalid
Name Stmts Miss Cover my_module.py 10 2 80%
What does the 'Miss' number mean?
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]
- Thinking 'Miss' counts failed tests
- Confusing 'Miss' with skipped tests
- Assuming 'Miss' means code errors
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]
- Assuming plugin missing causes 0% without errors
- Thinking fast tests mean low coverage
- Believing code without functions can't be covered
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]
- Writing tests blindly without coverage info
- Ignoring coverage to focus on speed
- Removing tests that cover code
