03 Quiz: Coverage Concepts
Instructions
This quiz tests your understanding of code coverage concepts from Lectures 6 and 7.
Preparation: Read First
Before attempting this quiz, study the following lecture sections:
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
Why Take This Quiz?
For Your Learning:
- Self-Assessment: Identify areas where you have strong understanding and topics that need review
- Exam Preparation: The quiz format and difficulty mirror what you can expect in the final exam
- Immediate Feedback: Get instant feedback on your answers to reinforce correct understanding
- Active Recall: Testing yourself is one of the most effective learning techniques
Guidelines:
- Each question has 4 options with exactly one correct answer
- Try to answer without referring to the lecture materials first
- Sign in with GitHub to save your progress across sessions
- Or continue as guest (progress won’t be saved)
Scoring Guide
- 18-20 correct: Excellent! Strong grasp of coverage concepts
- 15-17 correct: Good understanding! Review the areas you missed
- 12-14 correct: Fair understanding. Revisit the lecture materials
- 9-11 correct: Review needed. Focus on fundamentals
- Below 9: Please review Lectures 6 and 7 carefully
Statement coverage (C0) measures:
Branch coverage (C1) measures:
A coverage criterion is:
When we say “C1 subsumes C0,” we mean:
A test suite is “adequate” according to a coverage criterion when:
Consider this code:
def check(value):
if value > 0:
return "positive"
else:
return "negative"
Why does achieving 100% C1 automatically give 100% C0?
Given this code and test:
def categorize(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
else:
return "F"
# Test:
def test_high_score():
assert categorize(95) == "A"
What is the statement coverage (C0)?
Using the same categorize code with these tests:
def test_scores():
assert categorize(95) == "A"
assert categorize(85) == "B"
What is the branch coverage (C1)?
Given this pytest-cov output:
Name Stmts Miss Cover Missing
-----------------------------------------------
src/utils.py 24 6 75% 18-20, 35-37
-----------------------------------------------
What does “Missing: 18-20, 35-37” tell you?
Given this code (with line numbers):
1: def validate(value):
2: if value is None:
3: return "Error"
4: if value < 0:
5: return "Negative"
6: if value == 0:
7: return "Zero"
8: return "Positive"
And this coverage report: Missing: 3, 7
Which conditions were NEVER True during testing?
A test suite achieves 100% statement coverage. This means:
Consider this test:
def calculate_total(items, discount_percent):
total = sum(items)
if discount_percent > 0:
discount = total * (discount_percent / 100)
total = total - discount
return total
def test_calculate_total():
result = calculate_total([10, 20, 30], 10)
assert result > 0 # What's wrong?
What’s the problem with this test?
Which of the following bugs would 100% statement coverage likely MISS?
In which situation might 70% coverage be perfectly acceptable?
Which statement best describes the relationship between coverage and correctness?
Given this code (with line numbers):
1: def classify_age(age):
2: if age < 0:
3: return "Invalid"
4: elif age < 18:
5: return "Minor"
6: elif age < 65:
7: return "Adult"
8: else:
9: return "Senior"
Your coverage report shows Line 9 is missing. Which test would cover it?
Given this code and coverage report:
1: def calculate_shipping(weight, express=False):
2: if weight <= 0:
3: raise ValueError("Invalid")
4:
5: base_cost = weight * 2.0
6:
7: if express:
8: return base_cost * 1.5
9: return base_cost
Coverage report: Missing lines 3, 8
To cover BOTH missing lines, you need tests with which inputs?
Given this pytest-cov output with branch coverage enabled:
Name Stmts Miss Branch BrPart Cover
----------------------------------------------------
src/validator.py 20 2 10 3 82%
----------------------------------------------------
What does “BrPart: 3” indicate?
Your team is setting up a CI pipeline for a financial transaction system. What coverage threshold is most appropriate?
Why integrate coverage checks into a CI/CD pipeline?