Basic path testing is a white-box testing technique where you aim to validate all possible
execution paths in a program or a function to ensure that all statements are exercised at least
once. This technique is based on the control flow graph of a program, which represents all
possible paths through the code.
Here’s a quick guide on how to perform basic path testing, followed by a simple example.
Steps for Basic Path Testing
1. Create a Control Flow Graph (CFG):
o Represent each statement or block of statements as a node in the graph.
o Draw edges to show the flow of control from one statement to another based on
conditions (e.g., if-else branches, loops).
2. Identify Independent Paths:
o An independent path is one that introduces at least one new edge not covered by
other paths.
o A program with n decision points has up to n+1 independent paths.
3. Derive Test Cases for Each Path:
o Create test cases that will execute each independent path to ensure complete
coverage.
4. Execute Test Cases and Verify Results:
o Run the tests to verify that the program behaves as expected for all paths.
Example of Basic Path Testing
Let's consider a simple function that determines if a number is positive, negative, or zero.
def check_number(num):
if num > 0:
return "Positive"
elif num < 0:
return "Negative"
else:
return "Zero"
Step 1: Control Flow Graph (CFG)
Node 1: Start
Node 2: if num > 0
Node 3: return "Positive"
Node 4: elif num < 0
Node 5: return "Negative"
Node 6: return "Zero"
Step 2: Identify Independent Paths
The function has three possible paths:
1. Path 1: 1 → 2 → 3 (num > 0)
2. Path 2: 1 → 2 → 4 → 5 (num < 0)
3. Path 3: 1 → 2 → 4 → 6 (num == 0)
Step 3: Derive Test Cases
Path 1: Test with num = 5 (should return "Positive")
Path 2: Test with num = -3 (should return "Negative")
Path 3: Test with num = 0 (should return "Zero")
Step 4: Execute and Verify
Run each test case to verify that the function produces the expected output for all independent
paths:
python
Copy code
assert check_number(5) == "Positive"
assert check_number(-3) == "Negative"
assert check_number(0) == "Zero"
To represent the control flow graph (CFG) for the check_number function, we’ll break down
each condition and return statement as nodes and show the flow based on the if-elif-else
branches.
Control Flow Graph for check_number
plaintext
Copy code
+-------+
| Start |
+-------+
|
v
+----------------+
1 -> | if num > 0 |
+----------------+
/ \
yes no
/ \
+----------------+ +----------------+
| return "Positive" | | elif num < 0 |
+----------------+ +----------------+
| |
yes no
| |
+----------------+ +----------------+
| return "Negative" | | return "Zero" |
+----------------+ +----------------+
Explanation
Node 1: Start of the function.
Node 2: Condition if num > 0.
o If True (num is greater than 0), we go to Node 3.
o If False, we move to the elif condition.
Node 3: return "Positive" if num > 0 is true.
Node 4: Condition elif num < 0.
o If True (num is less than 0), we go to Node 5.
o If False, we move to the else block.
Node 5: return "Negative" if num < 0 is true.
Node 6: return "Zero" if none of the above conditions are true (i.e., num == 0).
This CFG helps us visualize the paths and validate test cases to cover each path in the
check_number function.