Home

03 Testing Fundamentals: Coverage Self-Study Guide

self-study coverage testing c0 c1 pytest-cov ci-cd chapter-03

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:


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:

From Chapter 03 (Testing Theory and Coverage): Testing Theory & Coverage:

Focus areas: Definitions, subsumption hierarchy, coverage limitations


CFG Tracing Exercise

Read first:

From Chapter 03 (Testing Theory and Coverage): Testing Theory & Coverage:

Focus areas: Reading CFGs, tracing paths, calculating C0 and C1


Coverage Detective Exercise

Read first:

From Chapter 03 (TDD and CI): TDD and CI:

From Chapter 03 (Testing Theory and Coverage): Testing Theory & Coverage:

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:

Prerequisites for this exercise:

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

In GitHub Classroom


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?

  1. Read the lecture sections linked above for your first exercise
  2. Attempt the exercise without looking at answers
  3. Check your understanding using the collapsible solutions
  4. Move to the next exercise when ready

Begin with the Coverage Concepts Quiz after reading the recommended lecture sections.

Good luck!

© 2026 Dominik Mueller   •  Powered by Soopr   •  Theme  Moonwalk