Path Coverage
What is Path Coverage?
Path coverage is the most thorough white box testing technique, requiring that every possible execution path through the code — every unique combination of branches — be tested at least once.
Why It's Challenging
The number of possible paths can grow exponentially with the number of decision points and loops, making 100% path coverage impractical for most real-world applications. Basis path testing is often used as a practical, mathematically grounded compromise.
Coverage Hierarchy
- Statement coverage is the weakest criterion.
- Branch coverage is stronger, subsuming statement coverage.
- Path coverage is the strongest, subsuming both statement and branch coverage.
Worked Example: Listing Every Path
Here is a simple shipping fee function with two independent decisions.
def shipping_fee(weight, express):
fee = 50
if weight > 5: # decision A
fee += 30
if express: # decision B
fee += 100
return fee
Each decision can go true or false, and the two decisions follow one after the other, so there are 2 × 2 = 4 complete paths from entry to exit.
| Path | A | B | Test input | Expected fee |
|---|---|---|---|---|
| 1 | True | True | weight 8, express True | 180 |
| 2 | True | False | weight 8, express False | 80 |
| 3 | False | True | weight 2, express True | 150 |
| 4 | False | False | weight 2, express False | 50 |
Statement, Branch and Path Coverage on the Same Code
- Statement coverage: test 1 alone runs every line.
- Branch coverage: tests 1 and 4 together take each decision both ways.
- Path coverage: all four tests are needed.
Why would the extra two tests matter? Imagine a later change makes the express charge depend on the weight surcharge already being added. A bug that appears only when A is true and B is false would slip through branch coverage but would be caught by path 2. Path coverage checks how decisions interact, not just each decision alone.
Why Paths Multiply So Quickly
With k independent if statements in a row, the number of paths is 2 to the power k. Three decisions give 8 paths, ten give 1,024, and twenty give over a million. Loops make it worse: a loop that can run 0 to 50 times creates a separate path for every possible count. That is why full path coverage is usually limited to small, critical functions.
Infeasible paths
Some paths look possible on a diagram but can never happen. If one condition is x > 10 and a later one is x < 5, with x unchanged in between, the path where both are true is impossible. Mark such paths as infeasible and record why, rather than trying to force a test.
Practical Ways to Apply Path Coverage
- Pick functions where combinations of decisions matter: pricing rules, eligibility checks, safety logic.
- Draw the control flow graph and list every path, or use a tool that does it for you.
- For loops, test zero iterations, one iteration, a typical number and the maximum, instead of every count.
- Remove infeasible paths, then write one test per remaining path with an exact expected result.
- Where the path count is still too large, fall back to basis path testing, which needs only as many tests as the cyclomatic complexity.
Common mistake: claiming path coverage because every branch was tested. Branch coverage and path coverage are equal only when there is a single decision.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.