Two white box and black box techniques appear in almost every Software Testing course: cyclomatic complexity (how many independent paths does this code have?) and boundary value analysis (which inputs are most likely to break it?). Students usually learn them as separate theory answers. They make far more sense when you apply both to the same small function, write the tests, and run them. That is what this post does.
Disclosure: GTU Hero is my project (link at the end). The function below is a toy example I wrote for practice. Its grading thresholds are made up for the exercise and are not GTU's actual grading rules.
The function under test
A grading function for a 70 mark paper:
def grade(marks: int) -> str:
if marks < 0 or marks > 70: # decision 1 (two conditions)
raise ValueError("marks must be 0 to 70")
if marks >= 56: # decision 2
return "AA"
elif marks >= 42: # decision 3
return "BB"
elif marks >= 23: # decision 4
return "PASS"
return "FAIL"
Small, but it has a compound condition, a chain of elifs and an exception. That is enough to show every idea.
Part 1: cyclomatic complexity
McCabe's cyclomatic complexity V(G) counts the number of linearly independent paths through the control flow graph. There are three standard ways to compute it, and a good exam answer shows at least two that agree.
Method 1: predicate nodes. V(G) = P + 1, where P is the number of simple conditions. Split the compound condition, because marks < 0 or marks > 70 is really two decisions:
marks < 0 1
marks > 70 2
marks >= 56 3
marks >= 42 4
marks >= 23 5
V(G) = 5 + 1 = 6
Method 2: edges and nodes. Draw the flow graph with one node per condition and one per outcome:
N1: marks < 0 ? -> N7 (true), N2 (false)
N2: marks > 70 ? -> N7 (true), N3 (false)
N3: marks >= 56 ? -> N8 (true), N4 (false)
N4: marks >= 42 ? -> N9 (true), N5 (false)
N5: marks >= 23 ? -> N10 (true), N6 (false)
N6: return "FAIL" -> N11
N7: raise ValueError -> N11
N8: return "AA" -> N11
N9: return "BB" -> N11
N10: return "PASS" -> N11
N11: exit
Count: 11 nodes. Edges: N1 to N5 have 2 each (10 edges), and N6 to N10 have 1 each (5 edges), so E = 15.
V(G) = E - N + 2 = 15 - 11 + 2 = 6
Method 3: regions. For a planar flow graph, V(G) equals the number of regions including the outer one. Drawn out, this graph has 6.
All three agree: V(G) = 6. So the basis path set has 6 paths, and a test suite that covers each basis path needs at least 6 cases.
Basis paths and one test each
Path 1: N1 true -> error test: marks = -1
Path 2: N1 false, N2 true -> error test: marks = 71
Path 3: N1 f, N2 f, N3 true -> "AA" test: marks = 60
Path 4: N1 f, N2 f, N3 f, N4 true -> "BB" test: marks = 50
Path 5: ... N4 f, N5 true -> "PASS" test: marks = 30
Path 6: ... N5 false -> "FAIL" test: marks = 10
Notice why splitting the or matters: if you treated it as one decision you would get V(G) = 5 and might never test the marks > 70 side separately.
Part 2: boundary value analysis
Basis path testing proves every path runs. It does not prove the thresholds are in the right place. A classic bug is writing > where the spec says >=, and path tests with values like 60 or 50 never notice. Boundary value analysis (BVA) targets exactly that: test at each boundary and just outside it.
The valid input range is 0 to 70, with internal boundaries at 23, 42 and 56. For each boundary take the value just below, the value itself, and for the range limits, the value just outside:
-1 (just below min, invalid)
0 (min) 1 (min + 1)
22 (just below PASS) 23 (PASS starts)
41 (just below BB) 42 (BB starts)
55 (just below AA) 56 (AA starts)
69 (max - 1) 70 (max)
71 (just above max, invalid)
The runnable test
cases = [(-1, "error"), (0, "FAIL"), (1, "FAIL"), (22, "FAIL"), (23, "PASS"),
(41, "PASS"), (42, "BB"), (55, "BB"), (56, "AA"), (69, "AA"),
(70, "AA"), (71, "error")]
for m, want in cases:
try:
got = grade(m)
except ValueError:
got = "error"
print(f"{m:>3} expected {want:<5} got {got:<5} {'ok' if got == want else 'FAIL'}")
Output from my run:
-1 expected error got error ok
0 expected FAIL got FAIL ok
1 expected FAIL got FAIL ok
22 expected FAIL got FAIL ok
23 expected PASS got PASS ok
41 expected PASS got PASS ok
42 expected BB got BB ok
55 expected BB got BB ok
56 expected AA got AA ok
69 expected AA got AA ok
70 expected AA got AA ok
71 expected error got error ok
Now break it on purpose: change marks >= 42 to marks > 42 and rerun. The case 42 fails immediately, while all six basis path tests above would still pass. That one experiment explains, better than any definition, why both techniques exist.
How to write this in an exam
- Write the code or pseudo code with numbered statements.
- Draw the flow graph and label nodes.
- Compute V(G) by at least two methods and state that they match.
- List the basis paths and one test case per path in a table (input, expected output).
- If the question also asks for black box testing, add the BVA table with the boundary reasoning.
Practice
Take a function that classifies a triangle as equilateral, isosceles or scalene from three sides. Compute V(G), list the basis paths, then do BVA on side lengths in the range 1 to 100. If you get V(G) between 4 and 7 depending on how you write the conditions, that is normal: say how you counted. When you are ready for real questions, search the GTU software testing question papers for "cyclomatic" and "boundary value" and attempt them with a timer.
Top comments (0)