Code Coverage Metrics
You'll learn how to measure how thoroughly your tests exercise your code using line, branch, and path coverage.
What Are Code Coverage Metrics?
Code coverage metrics answer the question: "How much of my code is actually executed during testing?" Think of it like a quality inspector walking through a factory—coverage tells you which assembly lines the inspector visited and which were skipped.
Coverage isn't about whether tests pass, but whether they run specific parts of your code at all.
Three Main Coverage Types
Line Coverage (Statement Coverage)
Measures what percentage of executable lines ran during tests. If you have 100 lines and tests execute 80, you have 80% line coverage.
Limitation: A line might run, but not all its logic paths get tested.
Branch Coverage (Decision Coverage)
Tracks whether both true and false outcomes of every conditional were executed. An if statement creates two branches—branch coverage ensures tests exercise both paths.
Example: Testing if (x > 0) with only positive numbers gives 50% branch coverage—you've missed the false branch.
Path Coverage
The most thorough metric—measures whether every possible execution path through the code was taken. With nested conditionals, this grows exponentially.
Example: Two if statements create 4 possible paths (TT, TF, FT, FF). Full path coverage means testing all four combinations.
How to Measure
Most languages have coverage tools (Python's coverage.py, Java's JaCoCo, JavaScript's Istanbul) that instrument your code, track execution during test runs, and generate reports showing which lines/branches were hit.
The Reality Check
High coverage (80-90% line coverage is common) suggests thoroughness, but 100% coverage doesn't guarantee bug-free code—it only shows what ran, not that it ran correctly.
Key Takeaway: Code coverage metrics—line, branch, and path—measure test completeness by tracking which parts of your code execute during testing, with each metric offering increasingly thorough insight into untested logic.