Lecture 5 Unit Testing Part 2
Lecture 5 Unit Testing Part 2
Let G(V,E) be a directed graph. We say that two nodes are strongly
connected in G iff :
• there is a path going from u to v,
• and a path going from v to u.
No
Yes
i=i+1
Yes
No
return True
Stop
Program Graph Notations
• Program Graph is a directed graph that displays same information as
the program flowchart but is more convenient to use in Path Testing.
• A circle in a graph represents a node, which stands for a sequence of
one or more procedural statements.
• A node containing a simple conditional expression is referred to as a
predicate node.
• Each compound condition in a conditional expression containing one or more
Boolean operators (e.g., and, or) is represented by a separate predicate node.
• A predicate node has two edges leading out from it (True and False).
Program Graph Notations
• An edge, or a link, is an arrow representing flow of control in a
specific direction
• An edge must start and terminate at a node.
• An edge does not intersect or cross over another edge (i.e., the graph is
planar).
• Areas bounded by a set of edges and nodes are called regions.
• When counting regions, include the area outside the graph as a
region, too.
Program Graph Notations
Deriving a program graph from a given program (source code or a
flowchart) is an easy process. It is illustrated here with two basic
structured programming constructs:
If-Then-Else (branching construct)
1
1. If <condition>
2. Then
2 4
3. <then statement>
3 5
4. Else
5. <else statement>
6
6. End if
7
7. <next statement>
Program Graph Notations
Pre-test loop
1. While <condition>
2. <repeated body>
3. End While
4. <next statement>
Program Graph Notations
Post-test loop
1. Do
1
2. <repeated body>
3. While <condition>
2
4. <next statement>
3
4
Program Graph: Example
1
2
1) Start, n is initialized def is_prime(n):
2) i = 2
4 3 6 3) if n % i == 0 for i in range(2, n):
4) then if (n % i == 0):
7 5) return False return False
6) else
5 7) i = i + 1 return True
9 8 8) if i < n
9) then (3)
10 10) else
11) return True
12) End
11
12
DD-Paths
The best-known form of white-box testing is based on a construct
known as a decision-to-decision path (DD-path) (Miller, 1977).
10
D
11
12 E
5
9 8
Region 2
10
11
12
Cyclomatic Complexity Example (DD-Path
graph)
• V(G) = 6 – 5 + 2 = 3
• Number of regions = 3
Region 1 A • Number of predicate points = 2,
therefore V(G) = 2 + 1 = 3.
Region 3
C B
Region 2
D
E
Deriving the Basis Set and Test Cases
1) Using the design or source code as a foundation, draw a
corresponding program graph.
2) Determine the cyclomatic complexity of the resultant program
graph.
3) Determine a basis set of linearly independent paths.
4) Prepare test cases that will force execution of each path in the
basis set.
Cyclomatic Complexity
• Programs with high cyclomatic complexity require more testing.
• Organizations that use the cyclomatic complexity metric usually set
the maximum acceptable complexity (V(G) = 10 is a common choice).
• There are two possibilities if a unit has a higher complexity: either
simplify the unit or plan to do more testing.
• Every loop in a program (unit) corresponds to strongly connected
component in the program graph.
• Therefore, one way to simplify a unit with high cyclomatic complexity
is to test loops in the program graph separately (as standalone units),
and then generate test cases for the condensation graph of the
program graph.