Code Coverage Metrics
You'll learn how to measure testing thoroughness using line, branch, and path coverage.
What Coverage Metrics Tell You
Code coverage metrics quantify what percentage of your code is actually executed during your test runs. They help answer: "Are my tests exercising enough of the codebase?" Coverage is not a guarantee of quality, but it reveals untested regions that might harbor bugs.
Three Core Coverage Types
Line Coverage
The simplest metric: what percentage of code lines were executed? If your program has 100 lines and tests run 80 of them, you have 80% line coverage. Think of it as checking which roads on a map you've driven down.
Branch Coverage
Measures whether both the true and false paths of every decision point (if statements, loops) were tested. Even if you execute a line containing if (condition), you need tests where condition is both true and false to achieve full branch coverage. This catches logic you might miss with line coverage alone.
Path Coverage
The most thorough: measures whether every possible route through the code was tested. With multiple branches, the number of paths explodes exponentially. If you have three if statements, there are 2³ = 8 possible paths. Path coverage is often impractical for complex code but represents the ideal.
The Hierarchy
Path coverage ⊇ Branch coverage ⊇ Line coverage. Achieving 100% path coverage automatically gives you 100% branch and line coverage, but not vice versa. Most teams target high branch coverage (80-90%) as a practical balance.
Key Takeaway: Code coverage metrics—line, branch, and path—measure testing completeness, with each level providing progressively deeper confidence that your tests explore different code execution scenarios.