03 Testing Fundamentals: Coverage Self-Study Guide
December 2025 (986 Words, 6 Minutes)
Code Coverage Self-Study Guide
Welcome
This guide helps you master code coverage concepts and tools through structured self-study. The materials are designed for two 90-minute sessions, but you can work at your own pace.
What you’ll learn:
- What code coverage measures and why it matters
- The difference between Statement (C0) and Branch (C1) coverage
- How to read and interpret coverage reports from pytest-cov
- How to systematically design tests to improve coverage
- The limitations of coverage (why 100% doesn’t mean bug-free)
Learning Path Overview
Note on time estimates: The durations listed below are for orientation only. There’s no need to skip an exercise just because the suggested time has passed—take the time you need to understand the material. Likewise, if you finish faster, feel free to move on without waiting.
Session 1: Theory & Analysis (~90 min)
| Step | Exercise | Duration | Focus |
|---|---|---|---|
| 1 | Read this guide | 10 min | Orientation |
| 2 | Coverage Concepts Quiz | 25 min | Test understanding of C0, C1, subsumption |
| 3 | CFG Tracing Exercise | 35 min | Trace test cases through Control Flow Graphs |
| 4 | Coverage Detective Part 1 | 20 min | Analyze coverage reports, identify gaps |
Session 2: Practice & Application (~90 min)
| Step | Exercise | Duration | Focus |
|---|---|---|---|
| 5 | Coverage Detective Part 2 | 25 min | Design tests to cover missing branches |
| 6 | GitHub Classroom Assignment | 65 min | Hands-on: improve coverage on real code |
How to Master Each Exercise
Before attempting each exercise, read the specified lecture sections. Click the links to jump directly to the relevant material.
Coverage Concepts Quiz
Read first:
From Chapter 03 (TDD and CI): TDD and CI:
- Part 2: The Coverage Problem - Why we need coverage
- Part 3: Setting Up Coverage Reporting - pytest-cov basics
- Part 4: Integrating Coverage into CI - Thresholds and automation
From Chapter 03 (Testing Theory and Coverage): Testing Theory & Coverage:
- Section 3: The Testing Theory - Program, Model, Test Suite, Coverage Criterion
- Section 4: Code Coverage (C0 and C1) - Statement vs Branch coverage, Subsumption
Focus areas: Definitions, subsumption hierarchy, coverage limitations
CFG Tracing Exercise
Read first:
From Chapter 03 (Testing Theory and Coverage): Testing Theory & Coverage:
- Section 4: Code Coverage (C0 and C1) - CFG examples, node/edge counting
- Section 5: Systematic Test Design for Branch Coverage - Deriving tests from CFGs
Focus areas: Reading CFGs, tracing paths, calculating C0 and C1
Coverage Detective Exercise
Read first:
From Chapter 03 (TDD and CI): TDD and CI:
- Part 3: Setting Up Coverage Reporting - Understanding
--cov-report=term-missing
From Chapter 03 (Testing Theory and Coverage): Testing Theory & Coverage:
- Section 5: Systematic Test Design - Designing tests for specific branches
Focus areas: Interpreting “Missing” lines, mapping lines to conditions, designing targeted tests
GitHub Classroom Assignment
Assignment link: Accept the Coverage Improvement Exercise
Read first:
From Chapter 03 (TDD and CI): TDD and CI:
- Part 4: Integrating Coverage into CI -
--cov-fail-underand GitHub Actions
Prerequisites for this exercise:
- Python 3.12+ installed
- uv package manager installed
- Git configured and GitHub access
Focus areas: Running coverage locally, achieving threshold, CI feedback loop
Key Concepts Summary
Statement Coverage (C0)
The percentage of code statements executed by your tests.
def classify(x):
if x > 0: # Statement 1
return "pos" # Statement 2
else:
return "neg" # Statement 3
Testing only classify(5) → C0 = 2/3 = 67%
Branch Coverage (C1)
The percentage of decision outcomes (True/False branches) executed.
Testing only classify(5) → C1 = 1/2 = 50% (only True branch taken)
Why C1 Subsumes C0
100% Branch Coverage guarantees 100% Statement Coverage because covering all branches requires executing all statements within those branches.
But the reverse is NOT true! High C0 doesn’t guarantee high C1.
Common Pitfalls
1. Chasing 100% Coverage
100% coverage does NOT mean bug-free code:
def test_bad():
result = calculate_discount(100, True, "VIP")
assert result is not None # Weak assertion!
2. Ignoring Branch Coverage
You can have 100% C0 while missing entire branches:
def process(data, validate=True):
if validate:
check_data(data) # Always tested with validate=True
return transform(data)
3. Coverage Without Assertions
Running code isn’t testing it - you need meaningful assertions.
How to Get Feedback
During Quizzes and Exercises
- Collapsible answers - Click “Show Answer” after attempting
- Detailed explanations - Understand WHY answers are correct
- Scoring guides - Self-assess your understanding
In GitHub Classroom
- Immediate CI feedback - Push and see coverage results
- Green check = 85%+ coverage achieved
- Red X = Add more tests and push again
Quick Reference
pytest-cov Commands
# Basic coverage
uv run pytest --cov=src
# Show missing lines
uv run pytest --cov=src --cov-report=term-missing
# Fail if below threshold
uv run pytest --cov=src --cov-fail-under=80
# Branch coverage mode
uv run pytest --cov=src --cov-branch
Coverage Report Columns
| Column | Meaning |
|---|---|
| Stmts | Total executable statements |
| Miss | Statements NOT executed |
| Cover | Percentage executed |
| Missing | Line numbers not covered |
Key Formulas
Statement Coverage (C0):
\[C0 = \frac{\text{Executed Statements}}{\text{Total Statements}} \times 100\%\]Branch Coverage (C1):
\[C1 = \frac{\text{Executed Branches}}{\text{Total Branches}} \times 100\%\]Subsumption:
\(100\% \text{ C1} \Rightarrow 100\% \text{ C0}\) (but NOT vice versa)
Estimated Total Time
| Component | Time |
|---|---|
| This guide | 10 min |
| Coverage Concepts Quiz | 25 min |
| CFG Tracing Exercise | 35 min |
| Coverage Detective Exercise | 45 min |
| GitHub Classroom Assignment | 65 min |
| Total | ~3 hours |
Ready to Start?
- Read the lecture sections linked above for your first exercise
- Attempt the exercise without looking at answers
- Check your understanding using the collapsible solutions
- Move to the next exercise when ready
Begin with the Coverage Concepts Quiz after reading the recommended lecture sections.
Good luck!