Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

ITECH7409: Software Testing

Lecture 5. Unit Testing. Part 2


This lecture
In this lecture:
• Notions and results from the Graph Theory
• Program Graphs
• DD-Paths Graphs
• Graph-Based Metrics
• McCabe’s Basis Path Testing
Mathematical Detour: Graph Theory for Testers
Basic Definitions
• A graph is composed of a (finite, non-empty) set V of nodes(or
vertices) and set E of edges.
• Every edge has two endpoints in the set of nodes and is said to
connect or join the two endpoints.
• Frequently, graphs are depicted as sets of points (nodes) connected
by line segments (edges).
• Formally, an edge is defined as a set (unordered pair) of two nodes }
in the case of undirected graph. In this case the edge is depicted as an
undirected line segment.
• In a directed graph, an edge is defined as an ordered pair of two
nodes and is depicted as an arrow going from to .
Examples of Undirected Graphs
Examples of Directed Graphs (Digraphs)
Degree of a Node
The degree of a node in a graph is the number of edges that have that
node as an endpoint. We write deg(n) for the degree of node n.

For example, in the graph depicted below, deg(1) = 3, deg(3) = 5.


Indegree and Outdegree of a Node in a Directed Graph
The indegree of a node in a directed graph is the number of distinct
edges that have that node as a terminal node. We write indeg(n) for
the indegree of node n.
The outdegree of a node in a directed graph is the number of distinct
edges that have that node as a start node. We write outdeg(n) for the
outdegree of node n.
For example, in the graph depicted below, indeg(2) = 1, outdeg(2) = 2.
Paths
A path is a sequence of edges such that for any adjacent pair of edges
in the sequence, the terminal node of the first edge is the initial node
of the second edge.
For example, in the graph depicted below the sequence of edges (1,2),
(2,4), (4,5), (5,6) is a path. Whereas (1,2), (2,3), (3,6) is not a path
because (3,6) is not an edge.
Planar Graphs
Graphs need not be represented pictorially. Moreover, if you have such
a representation, it is not unique. For example, the following two
pictures represent the same graph.
Planar Graphs
A graph (digraph) is called a planar graph if it can be drawn on the
plane so that its edges do not intersect. For example, as we have seen
on the previous slide the following graph is planar.
Planar Graphs
Not every graph is planar. For example, it can be proved that the
following graph is not planar (we don’t prove this fact).
Planar Graphs
Planar graphs satisfy the following Euler’s formula:

Where, V is the number of nodes (vertices), E is the number of edges


and F is the number of faces (regions) in a planar graph. For example, in
the graph depicted below we have 4 – 6 + 4 = 2 (note that we count the
outer region as well).
Condensation Graph of a Directed Graph
The following notion is very important in software testing since it helps
in simplifying situations that otherwise are computationally untenable.

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.

Exercise 1 Check that strong connectedness is an equivalence relation.


Condensation Graph of a Directed Graph
Definition A maximal set of strongly connected nodes in a directed
graph is called a strong component.
For example, in the following graph has three strong components:
S1={A,B,C}, S2={D,E,F,G} and S3={H}.
Condensation Graph of a Directed Graph
Definition Given a graph G = (V, E), its condensation graph is formed by
replacing each strong component by a condensing node.
For example, the graph on the previous slide has the following
condensation graph:
Program Flowcharts & Program Graphs
Program logic and paths are frequently illustrated using a flowchart.
Flowchart symbols:
• Start : represents the start of program. Usually drawn as an ellipse.
• Arrows : represent flow of control in a program. Usually means going from
one command to another commands.
• Rectangles : Used to show computation or a specific process.
• Parallelogram : Used for getting input from user or sending output to user.
• Rhombus : Used for conditional flow control where program must decide
which way to go, therefore representing if, while, switch, for, do while
statements.
• End : represents the end of program. usually drawn as an ellipse but
sometimes rounded squares are also used.
Flowchart: Example
Start
def is_prime(n):
for i in range(2, n):
i=2 if (n % i == 0):
n >1 is entered
return False
return True
n % i == 0

No
Yes

i=i+1

Yes

return False i<n

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).

Definition A chain in a directed graph is a sequence of nodes in which:


• The initial node has an indegree ≥ 0.
• Every internal node has indegree = outdegree = 1.
• The final node has an outdegree ≥ 0.
For example, in the graph on the previous slide the sequence 3,6,7,8 is
a chain.

Definition A DD-Paths is a chain in a program graph.


DD-Paths
Definition Given a program written in an imperative language, its DD-
Paths Graph is the directed graph in which nodes are DD-Paths of its
program graph, and edges represent control flow between successor
DD-Paths.
Note, that one can build different DD-Path Graphs for a given program
graph since DD-Paths can be chosen in many ways. However, they all
will be somehow similar (we discuss this fact later in the lecture).
DD-Paths Graphs enable very precise description of the extent to which
set of test cases covers the corresponding source code.
DD-Path Graph: Example
1 Nodes in DD-Path
the
Program
2
Graph
4 3 6
1,2,3 A A
6,7,8 B
7 9 C
10,11 D
5
9 8 4,5,12 E C B

10
D

11

12 E

Program Graph DD-Path Graph


Program Graph-Based Coverage Metrics
Several widely accepted test/code coverage metrics are used. See the
table below (E.F. Miller):
Metric Description of Coverage
C0 Every statement
C1 Every DD-path
C2 C1 coverage + loop coverage
Cd C1 coverage + Every dependent pair of DD-Paths
Cik Every program path that contains up to k repetitions of a loop
Cstat Statistically significant fraction of paths
C∞ All possible execution paths

Most quality organizations expect the C1 metric as the minimum


acceptable level of test coverage.
Program Graph-Based Coverage Metrics
Let’s discuss some of the metrics:
• C0 and C1 are self-explanatory.
• C2 – simple way of loop testing is to test two outcomes: one is to
traverse the loop (once), the other is to skip it. Note, that this
approach won’t test the is_prime function properly.
• Cik – all path from source node to sink node that contain k iterations
of each loop are tested. To make it feasible, usually k = 2.
• Cstat – cannot be defined in a universally reasonable way; usually refers
to a comfort level of the customer.
• C∞ - this could make sense for small programs with no loops.
McCabe’s Basis Path Testing
• Basis Path Testing is a white-box testing technique proposed by Tom
McCabe. It uses program graph notation.
• Enables the test case designer to derive a logical complexity measure
of a procedural design.
• Uses this measure as a guide for defining a basis set of execution
paths.
• Test cases derived to exercise the basis set are guaranteed to execute
every statement in the program at least one time during testing.
McCabe’s Basis Path Testing
There can be several different paths through the program from the
start node until the end node. We may choose some of them as test
cases.
To avoid redundancy, each new test case (path) should introduce at
least one new set of processing statements or a new condition.
Minimum number of such paths is given by cyclomatic complexity of
the program graph, or DD-Path graph. Cyclomatic complexities of both
graphs are equal.
Cyclomatic Complexity
Definition
The cyclomatic number of a connected graph G is given by
, where
• E is the number of edges in G.
• V is the number of nodes in G.
Note, that if the graph is planar, then from the Euler’s formula it follows
that V(G) = number of regions.
Usually, a program graph in a structured programming language is
planar if no break (or similar) statements are used.
If G is a program graph, then one can also use the formula V(G) = the
number of predicate nodes + 1.
Cyclomatic Complexity Example (program graph)
1
• V(G) = 13 – 12 + 2 = 3
Region 1 2 • Number of regions = 3
4 3 6
• Number of predicate points = 2,
therefore V(G) = 2 + 1 = 3.
Region 3 7

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.

You might also like