Software Testing
Course Instructor
Sobia Usman
Structural Testing (White box)
• We look inside the system and evaluate what it consists of and how is
it implemented.
• In white box testing the internal structures of the program are
analyzed and test cases are devised that can test these structures.
2
Basic Code Structures and Flow Graph
Notation
• Sequence
• Sequence depicts programming instructions that do not have
branching or any control information. So we lump together several
sequential instructions in one node of the graph.
3
Basic Code Structures and Flow Graph
Notation
• If
• Second structural form is the If statement. In the following graph, the first node
at the left depicts the if statement and the two nodes next to the first node
correspond to the successful case (if condition is true) and unsuccessful case (if
condition is false) consecutively. The control comes to the same instruction from
either of these intermediate instructions.
4
Basic Code Structures and Flow Graph
Notation
• Case
• In Case statement, control can take either of several branches (as
opposed to only two in If statement.) First node represents the switch
statement (C/C++) and nodes in middle correspond to all different
cases. Program can take one branch and result into the same
instruction.
5
Basic Code Structures and Flow Graph
Notation
• While
• A while loop structure consists of a loop guard instruction through
which the iteration in the loop is controlled. The control keeps
iterating in the loop as long as the loop guard condition is true. It
branches to the last instruction when it becomes false.
6
Example – Bubble Sort
7
Path
• A sequence of edges in the flow graph from input to output
8
Paths
• Following are possible paths from starting to the end of this code.
• Path1: 1-6
• Path2: 1-2-3-4-5-1-6
• Path3: 1-2-4-5-1-6
• Path4: 1-2-4-2-3-4-5-1-6
9
White Box Testing - Coverage
• Statement Coverage
• Branch Coverage
• Path Coverage
10
Coverage
• Statement Coverage: In this scheme, statements of the code are
tested for a successful test that checks all the statements lying on the
path of a successful scenario.
• Branch Coverage: In this scheme, all the possible branches of decision
structures are tested. Therefore, sequences of statements following a
decision are tested.
• Path Coverage: In path coverage, all possible paths of a program from
input instruction to the output instruction are tested. An exhaustive
list of test cases is generated and tested against the code.
11
• statement coverage
• test cases
Input: a = 2, b = 2, d = 3
expected result: a = 3; b = 2; c = 3; d = 3
12
• branch coverage
• test cases
Input: a = 2; b = 2; c = 3; d = 4
expected result: a = 3; b = 2; c = 4; d = 4
input: a = 1, b = 2; c = 3; d = 4
expected result: a = 2; b = 2; c = 3; d = 4
13
• path coverage
• test cases
Input: a = 2; b = 2; c = 3; d = 4
expected result: a = 3; b = 2; c = 4; d = 4
input: a = 1, b = 2; c = 3; d = 4
expected result: a = 2; b = 2; c = 3; d = 4
14
Paths in a
Program
Containing
Loops
• Now we shall apply the path
coverage scheme on a piece
of code that contains a loop
statement and see how many
test cases can possibly be
developed.
15
Paths
• The following is an analysis of the mentioned code and the flow diagram. It determines the number
of paths against different iterations of the loop.
• N = 0: If the control does not enter into the loop then only one path will be traversed. It is 1-5.
• N=1: Two different paths can possibly be traversed (depending on condition).
• 1-2-4-1-5
• 1-3-4-1-5
• N=2: Four possible paths can be traversed.
• 1-2-4-1-2-4-1-5
• 1-2-4-1-3-4-1-5
• 1-3-4-1-2-4-1-5
• 1-3-4-1-3-4-1-5
• Generalizing
N
the relation between loop variable N and the number of possible paths, for the value
of N, 2 paths are possible
• Thus if N = 20 it means more then 1 million paths are possible.
16
A simple graph with many paths
17
Cyclomatic Complexity
• Quantitative measure of the logical complexity of a program.
• Number of independent paths in the basis set of a program.
• Upper bound for the number of tests that must be conducted to
ensure that all statements and branches have been executed at least
once.
• Cyclomatic Complexity, V (G), for a flow graph G is defined as:
V (G) = E - N + 2
• Where E is the number of edges and N is the number of nodes in the
flow graph G.
• Cyclomatic complexity provides us with an upper bound for the
number of independent paths that comprise the basis set.
18
Cyclomatic Complexity
∙ Number of edges = 8
∙ Number of nodes = 6
∙ C (G) = 8-6+2 = 4
Paths to be tested
∙ Path1: 1-6
∙ Path2: 1-2-3-4-5-1-6
∙ Path3: 1-2-4-5-1-6
∙ Path4: 1-2-4-2-3-4-5-1-6
19