Basis Path Testing
What is Basis Path Testing?
Basis path testing is a white box technique that uses the cyclomatic complexity of a program to identify a set of linearly independent execution paths, ensuring each path is tested at least once.
Steps in Basis Path Testing
- Draw the Control Flow Graph (CFG) of the code.
- Calculate the cyclomatic complexity to determine the number of independent paths.
- Identify the basis set of linearly independent paths.
- Design test cases that exercise each path in the basis set.
Benefit
Basis path testing provides a mathematically grounded way to ensure adequate path coverage with the minimum necessary number of test cases.
Worked Example: A Ticket Price Function
Let us apply all four steps to a small piece of code that decides a cinema ticket price.
def ticket_price(age, is_student):
if age < 5: # node 1
return 0 # node 2
price = 200 # node 3 (with the next decision)
if is_student:
price = 120 # node 4
return price # node 5
# node 6 = single exit
Step 1: Draw the control flow graph
Nodes 1 to 5 come from the code, and node 6 is a shared exit. The edges are: 1→2 (under five), 1→3 (five or older), 3→4 (student), 3→5 (not a student), 4→5, 2→6 and 5→6. That makes 7 edges and 6 nodes.
Step 2: Calculate cyclomatic complexity
V(G) = 7 − 6 + 2 = 3. Counting decisions gives the same answer: two if statements plus one. So the basis set has three paths.
Step 3: Identify the independent paths
A reliable way is the baseline method. Choose one normal path first, then flip one decision at a time so each new path adds at least one edge not used before.
- Path 1 (baseline): 1 → 3 → 5 → 6, an adult who is not a student.
- Path 2: 1 → 3 → 4 → 5 → 6, flipping the student decision.
- Path 3: 1 → 2 → 6, flipping the age decision.
Step 4: Design the test cases
| Test | Path | age | is_student | Expected |
|---|---|---|---|---|
| TC1 | 1 | 30 | False | 200 |
| TC2 | 2 | 19 | True | 120 |
| TC3 | 3 | 3 | False | 0 |
Three tests cover every statement and every branch here. Notice that a toddler who is also a student would follow the same path as TC3, so basis path testing does not ask for it.
How It Compares With Other Coverage Goals
Basis path testing sits between branch coverage and full path coverage. A complete basis set always achieves branch coverage, and therefore statement coverage too. It does not test every combination of decisions, so bugs that appear only with one specific combination may still slip through. For critical logic, add extra tests based on boundary values, for example ages 4 and 5 in this function.
Tips, Traps and a Short Checklist
- Infeasible paths: if a chosen path cannot be executed, pick a different combination of edges that still keeps the set independent.
- Loops: treat the loop condition as one decision; one path skips the loop and another enters it.
- Compound conditions: split
if a and binto separate nodes if you want each condition tested on its own. - Tool support: static analysis tools can calculate complexity, but choosing meaningful inputs and expected results is still the tester's job.
Checklist: graph drawn and checked, V(G) calculated two ways, exactly V(G) independent paths listed, one test per path with a concrete expected result, and boundary tests added where values matter.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.