Professional Documents
Culture Documents
1
Organization of this
Lecture
Introduction to Testing.
White-box testing:
statement coverage
path coverage
branch testing
condition coverage
Cyclomatic complexity
Summary
2
White-Box Testing
3
Statement Coverage
In a programming language, a statement is nothing
but the line of code or instruction for the computer
to understand and act accordingly. A statement
becomes an executable statement when it gets
compiled and converted into the object code and
performs the action when the program is in a
running mode.
Hence “Statement Coverage”, as the name itself
suggests, it is the method of validating whether each
and every line of the code is executed at least once.
INPUT A & B
C=A+B
IF C>100
PRINT “ITS DONE”
For Statement Coverage – we would only need one test case
to check all the lines of the code.
That means:
With this, we can see that each and every line of the
code is executed at least once.
Here are the Conclusions that are derived so far:
Branch Coverage ensures more coverage than
Statement coverage.
Branch coverage is more powerful than Statement
coverage.
100% Branch coverage itself means 100% statement
coverage.
But 100 % statement coverage does not guarantee
100% branch coverage.
Path Coverage
INPUT A & B
C=A+B
IF C>100
PRINT “ITS DONE”
END IF
IF A>50
PRINT “ITS PENDING”
END IF
there are 2 decision statements, so for
each decision statement, we would need
two branches to test. One for true and the
other for the false condition. So for 2
decision statements, we would require 2
test cases to test the true side and 2 test
cases to test the false side, which makes
a total of 4 test cases.
In order to have the full coverage, we would need following
test cases:
17
Statement Coverage
18
Example
19
Branch Coverage
20
Branch Coverage
21
Example
22
Example
23
Condition Coverage
24
Example
25
Condition testing
stronger testing than branch
testing:
Branch testing
stronger than statement coverage
testing.
26
Condition coverage
27
Path Coverage
28
Linearly independent paths
Defined in terms of
control flow graph (CFG) of a
program.
29
Path coverage-based
testing
To understand the path coverage-
based testing:
we need to learn how to draw
control flow graph of a program.
30
Control flow graph (CFG)
31
How to draw Control flow
graph?
32
Example
33
Example Control Flow
Graph
1
2
3 4
5
6
34
How to draw Control flow
graph?
Sequence: 1
1 a=5;
2
2 b=a*b-1;
35
How to draw Control flow
graph?
Selection:
1
1 if(a>b) then 2 3
2 c=3; 4
3 else c=5;
4 c=c*c;
36
How to draw Control flow
graph?
1
Iteration:
1 while(a>b){ 2
2 b=b*a; 3
3 b=b-1;} 4
4 c=b+d;
37
Path
38
Independent path
39
Independent path
It is straight forward:
to identify linearly independent
paths of simple programs.
For complicated programs:
it is not so easy to determine the
number of independent paths.
40
McCabe's cyclomatic
metric
An upper bound:
for the number of linearly
independent paths of a program
Provides a practical way of
determining:
the maximum number of linearly
independent paths in a program.
41
McCabe's cyclomatic
metric
Given a control flow graph G,
cyclomatic complexity V(G):
V(G)= E-N+2
N is the number of nodes in G
E is the number of edges in G
42
Example Control Flow
Graph
1
2
3 4
5
6
43
Example
Cyclomatic complexity =
7-6+2 = 3.
44
Cyclomatic complexity
46
Example Control Flow
Graph
1
2
3 4
5
6
47
Example
48
Cyclomatic complexity
49
Cyclomatic complexity
50
Cyclomatic complexity
51
Cyclomatic complexity
52
Cyclomatic complexity
54
Path testing
56
Derivation of Test Cases
57
Example
58
Example Control Flow
Diagram
1
2
3 4
5
6
59
Derivation of Test Cases
60
An interesting application
of cyclomatic complexity
61
Cyclomatic complexity
62
Cyclomatic complexity
64
Data Flow-Based Testing
65
Data Flow-Based Testing
67
Definition-use chain (DU
chain)
[X,S,S1],
S and S1 are statement numbers,
X in DEF(S)
X in USES(S1), and
the definition of X in the statement S is
live at statement S1.
68
Data Flow-Based Testing
69
Data Flow-Based Testing
70
Data Flow-Based Testing
1 X(){
2 B1; /* Defines variable a */
3 While(C1) {
4 if (C2)
5 if(C4) B4; /*Uses variable a */
6 else B5;
7 else if (C3) B2;
8 else B3; }
9 B6 }
71
Data Flow-Based Testing
[a,1,5]: a DU chain.
Assume:
DEF(X) = {B1, B2, B3, B4, B5}
USED(X) = {B2, B3, B4, B5, B6}
There are 25 DU chains.
However only 5 paths are needed to cover
these chains.
72
Mutation Testing
73
Mutation Testing
74
Mutation Testing
A mutated program:
tested against the full test suite of the
program.
If there exists at least one test case in the
test suite for which:
a mutant gives an incorrect result,
then the mutant is said to be dead.
75
Mutation Testing
If a mutant remains alive:
even after all test cases have been exhausted,
the test suite is enhanced to kill the mutant.
The process of generation and killing of mutants:
can be automated by predefining a set of
primitive changes that can be applied to the
program.
76
Mutation Testing
77
Mutation Testing
78
Cause and Effect Graphs
80
Cause and Effect Graphs
81
Steps to create cause-
effect graph
Study the functional requirements.
Mark and number all causes and
effects.
Numbered causes and effects:
become nodes of the graph.
82
Steps to create cause-
effect graph
Draw causes on the LHS
Draw effects on the RHS
Draw logical relationship between
causes and effects
as edges in the graph.
Extra nodes can be added
to simplify the graph
83
Drawing Cause-Effect
Graphs
A B
If A then B
A
B
C
If (A and B)then C
84
Drawing Cause-Effect
Graphs
A
B
C
If (A or B)then C
A
B
C
If (not(A and B))then C
85
Drawing Cause-Effect
Graphs
A
B
C
If (not (A or B))then C
A B
If (not A) then B
86
Cause effect graph-
Example
A water level monitoring
system
used by an agency involved in
flood control.
Input: level(a,b)
a is the height of water in dam in
meters
b is the rainfall in the last 24
hours in cms
87
Cause effect graph-
Example
Processing
The function calculates whether the level
is safe, too high, or too low.
Output
message on screen
level=safe
level=high
invalid syntax
88
Cause effect graph-
Example
89
Cause effect graph-
Example
90
Cause effect graph-
Example
91
Cause effect graph-
Example
Three effects
level = safe
level = high
invalid syntax
92
Cause effect graph-
Example
1 10 E
3
2 11
3 E
1
4
E
5 2
93
Cause effect graph-
Decision table
Test 1 Test 2 Test 3 Test 4 Test 5
Cause 1 I I I S I
Cause 2 I I I X S
Cause 3 I S S X X
Cause 4 S I S X X
Cause 5 S S I X X
Effect 1 P P A A A
Effect 2 A A P A A
Effect 3 A A A P P
94
Cause effect graph-
Example
95
Cause effect graph-
Example
96
Cause effect graph-
Example
97
Cause effect graph-
Example
98
Cause effect graph
99