Branch and Path Coverage
You'll learn: How branch coverage measures whether both true and false outcomes of decisions are tested, and how path coverage tracks every unique route through your code.
Understanding Branch Coverage
Branch coverage focuses on decision points in your code—places where execution can go two or more ways. It measures whether your tests exercise both outcomes of each decision.
Think of a fork in a road: branch coverage ensures your tests travel down the left path and the right path at least once. Each if statement creates two branches: the "yes" branch and the "no" branch.
For example, an if (age >= 18) statement creates two branches. Full branch coverage requires:
- At least one test where
age >= 18is true - At least one test where
age >= 18is false
This is stronger than line coverage because even if a line executes, you might only hit one branch outcome.
Understanding Path Coverage
Path coverage goes further by tracking every unique combination of branches—every possible route from start to finish.
Imagine a building with two hallways, each containing two doors. Path coverage ensures you've walked through every combination: left hall + left door, left hall + right door, right hall + left door, right hall + right door.
With just two if statements, you already have four possible paths. Path coverage grows exponentially, making 100% path coverage impractical for most real code—but it reveals the theoretical maximum testing thoroughness.
The Practical Difference
Branch coverage asks: "Did I test both outcomes of each decision?"
Path coverage asks: "Did I test every possible journey through all decisions combined?"
Branch coverage is achievable and valuable. Path coverage is often prohibitively expensive but helps you understand code complexity.
Key Takeaway: Branch coverage ensures all decision outcomes are tested (practical goal); path coverage tracks all unique execution routes (theoretical ideal, rarely achievable).