CFG (Control Flow Graph)
What is a Control Flow Graph?
A Control Flow Graph (CFG) is a graphical representation of all the paths that might be traversed through a program during its execution, using nodes and edges.
Key Elements of a CFG
- Nodes: Represent statements or blocks of sequential code.
- Edges: Represent the flow of control between nodes (e.g., following an if/else branch or a loop).
- Decision Points: Nodes with more than one outgoing edge, representing branching logic.
Why CFGs Matter for Testing
CFGs are the foundation for structure-based testing techniques like statement, branch, and path coverage, and are used to calculate cyclomatic complexity and design basis path test cases.
Drawing a CFG From Real Code, Step by Step
A control flow graph becomes much easier once you build one by hand. Take this small function that checks an exam score:
def grade(score):
if score < 0: # node 1 (decision)
return "invalid" # node 2
result = "pass" # node 3
if score < 40: # node 4 (decision)
result = "fail" # node 5
return result # node 6
Follow these steps to turn it into a graph:
- Number the statements. Give each statement or straight-line block a node number, as in the comments above.
- Add a single exit node. Both return statements lead to the end of the function, so add node E to represent the exit.
- Draw an edge for every possible move. Node 1 has two edges (to 2 when the score is negative, to 3 otherwise). Node 4 also has two edges (to 5 or straight to 6).
- Join the loose ends. Node 5 flows to 6, and both 2 and 6 flow to E.
The finished graph has 7 nodes (1, 2, 3, 4, 5, 6, E) and 8 edges: 1→2, 1→3, 3→4, 4→5, 4→6, 5→6, 2→E and 6→E.
Calculating Cyclomatic Complexity From the Graph
Cyclomatic complexity tells you how many independent paths exist, which is also the minimum number of test cases needed for basis path testing. There are three equivalent ways to compute it:
- Edges and nodes: V(G) = E − N + 2P, where P is the number of connected components (1 for a single function). Here 8 − 7 + 2 = 3.
- Decision points: number of binary decisions + 1. The function has two
ifstatements, so 2 + 1 = 3. - Regions: count the enclosed areas of the drawn graph plus the outer area. This also gives 3.
If two methods give different answers, recheck your edges. A missing edge is the most common reason.
Turning Basis Paths Into Test Cases
| Path | Route through the graph | Input | Expected output |
|---|---|---|---|
| P1 | 1 → 2 → E | score = -5 | "invalid" |
| P2 | 1 → 3 → 4 → 6 → E | score = 70 | "pass" |
| P3 | 1 → 3 → 4 → 5 → 6 → E | score = 20 | "fail" |
These three tests together execute every statement and every branch outcome. Adding a boundary test such as score = 40 is still worthwhile, because path testing proves routes are exercised, not that the comparison operator is right.
Where Testers Go Wrong With CFGs
- Treating every line as its own node. Consecutive statements with no branch can be merged into one block. Merging removes one node and one edge together, so the complexity stays the same.
- Forgetting the back edge of a loop. A
whileorforloop needs an edge from the end of the body back to the condition. - Ignoring compound conditions.
if a and bcontains two decisions when you need condition-level testing; split it into two nodes in that case. - Trying to test every path in loops. Loops create an unlimited number of paths. Basis paths keep the set finite and practical.
Takeaway: number the nodes, draw every edge, compute V(G) two ways, then write one test per basis path.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.