Foundations of Software Testing: Test Adequacy Measurement and Enhancement: Control and Data Flow
Foundations of Software Testing: Test Adequacy Measurement and Enhancement: Control and Data Flow
Learning Objectives
Test adequacy
What is adequacy?
Measurement of adequacy
Example
Program sumProduct must meet the following requirements:
R1
R2.1
R2.2
Example (contd.)
Suppose now that the test adequacy criterion C is specified as:
C : A test set T for program (P, R) is considered adequate if,
for each r in R there is a test case in T that tests the
correctness of P with respect to r.
T={t: <x=2, y=3> is inadequate with respect to C for
program sumProduct.
The lone test case t in T tests R1 and R2.1, but not R2.2.
Aditya P. Mathur 2007
Coverage
Measuring adequacy of T:
T covers Ce if, for each e' in Ce, there is a test case in T that
tests e'. T is adequate wrt C if it covers all elements i.
T is inadequate with respect to C if it covers k<n elements of Ce.
k/n is the coverage of T wrt C.
Example
Consider criterion:
A test T for (P, R) is adequate if , for each requirement r in R,
there is at least one test case in T that tests the correctness of P
with respect to r.
The coverage domain is Ce={R1, R2.1, R2.2}.
T covers R1 and R2.1 but not R2.2.
Hence, T is inadequate with respect to C.
The coverage of T wrt C is 2/3=0.66.
10
Another Example
Consider the following criterion:
A test T for program (P, R) is considered adequate if each path
in P is traversed at least once.
Assume that P has exactly two paths, p1 and p2, corresponding
to condition x<y and condition x y, respectively.
For the given adequacy criterion C we obtain the coverage
domain Ce to be the set {p1, p2}.
11
12
13
Example
sumProduct1
There is only one path, p1, which traverses all the statements.
Using the path-based coverage criterion C, Ce={ p1}.
T={t: <x=2, y=3>}is adequate w.r.t. C but does not reveal the
error.
14
Example (contd.)
sumProduct2
15
Lesson
An adequate test set might not reveal even the most obvious error
in a program.
This does not diminish in any way the need for the measurement
of test adequacy, as increasing coverage might reveal an error!
16
Test enhancement
17
Test Enhancement
While a test set adequate wrt some criterion does not guarantee an
error-free program, an inadequate test set often implies deficiency.
Identifying deficiency helps in enhancing inadequate test set.
Enhancement in is likely to test the program in new ways
testing untested portions or testing the features in a sequence
different from the one used previously.
Testing the program differently than before may discover any
uncovered errors.
18
19
20
21
22
23
24
f
t
f
t
product*x
25
f
t
y
f
t
product*x
26
f
t
y
f
t
product*x
27
28
Demonstrating feasibility
Feasibility can be demonstrated by executing the program on a
test case that shows that the element under consideration is
covered.
Infeasibility cannot be demonstrated by execution on a finite
number of test cases. Determining infeasibility is difficult.
An attempt to enhance a test set by executing tests aimed at
covering an infeasible element will fail.
29
30
9]
31
32
33
Example
A program to meet the following requirements is to be developed.
34
Example (contd.)
35
Example (contd.)
Consider this
program written to
meet the above
requirements.
36
Example (contd.)
37
Example (contd.)
38
Example (contd.)
Consider the following to test if program meets its requirements.
T={<request=1, x=2, y=3>, <request=2, x=4>, <request=3>}
For the first requests, the program correctly outputs 8 and 24.
The program exits when executed against the last request.
This program behavior is correct, hence one might conclude
that the program is correct.
But this conclusion is incorrect.
39
Example (contd.)
Let us now evaluate T against the path coverage criterion.
The coverage domain consists of all paths that traverse each
of the three loops zero and once in the same or different
executions of the program.
We consider one uncovered path that exposes error.
40
Example (contd.)
Consider the path p that: begins execution at line 1; reaches the
outermost while at line 10; then the first if at line 12; followed by
the statements that compute the factorial starting at line 20; and
then the code to compute the exponential starting at line 13.
p is traversed when the program is launched and the first
request is to compute the factorial of a number, followed by a
request to compute the exponential.
The sequence of requests in T does not exercise p.
T is inadequate with respect to the path coverage criterion.
Aditya P. Mathur 2007
41
Example (contd.)
The following test covers p:
T={<request=2, x=4>;<request=1, x=2, y=3>;<request=3>}
When the values in T are input, the program correctly outputs 24 as
the factorial of 4, but incorrectly outputs 192 as the value of 23 .
This happens because T traverses the path which makes the
computation of the exponentiation begin without initializing
product.
In fact the code at line 14 begins with the value of product set to 24.
42
Example (contd.)
43
Multiple executions
44
45
Statement Coverage
46
Block Coverage
The block coverage of T wrt Program P is
Bc/(Be -Bi), where
Bc is the number of blocks covered;
Bi is the number of unreachable blocks; and
Be is the total number of blocks in the program.
T is considered adequate with respect to the block coverage
criterion if the block coverage of T is 1.
47
(b)
Statements covered:
t1: 2, 3, 4, 5, 6, 7, 10
t2: 2, 3, 4, 5, 9, 10.
Sc=6, Si=1, Se=7. The statement coverage
for T is 6/(7-1)=1.
Hence, we conclude that T1 is adequate for P
with respect to the statement coverage
criterion. Note: 7b is unreachable.
48
Be={1, 2, 3, 4, 5}
Blocks covered:
t1: Blocks 1, 2, 5
t2, t3: same coverage as of t1.
50
51
52
Conditions as Decisions
Conditions serve as a decision in context of if, while, and switch
statements.
53
Outcomes of a Decision
54
Undefined Condition
55
Decision Coverage
A decision is considered covered if all outcomes of the decision
have been taken.
For example, the expression in an if or a while statement has
evaluated to true in some execution of the program under test and
to false in the same or another execution.
56
57
58
59
60
Decision Coverage
61
Condition Coverage
62
63
64
65
Condition Coverage
The condition coverage of T wrt P is
Cc/(Ce -Ci), where
Cc is the number of simple conditions covered;
Ci is the number of infeasible simple conditions; and
Ce is the number of simple conditions in the program.
T is considered adequate wrt the condition coverage criterion if the
condition coverage of T wrt P is 1.
66
67
Error
Aditya P. Mathur 2007
68
T and F
69
70
Condition/Decision Coverage
When a decision is composed of a compound condition, decision
coverage does not imply that each simple condition within a
compound condition has taken both values true and false.
Condition coverage ensures that each component simple condition
within a condition has taken both values true and false.
However, condition coverage does not require each decision to
have taken both outcomes.
Condition/Decision coverage, also known as branch condition
coverage, requires coverage of conditions and decisions.
Aditya P. Mathur 2007
71
F or F
F or T
72
73
74
75
76
T or T
F or T
T or F
F or F
77
ki
i1
Aditya P. Mathur 2007
78
79
Error! the computation of S for line 3 in the table has been left out.
80
T and T -> f4
t1: T T; T F; F F
t2: F F; F T; T T
81
82
83
LCSAJ coverage
84
85
86
87
LCSAJs:
(1,10,7)
(7,10,7)
(1,7,11)
(11,12,exit)
(7,7,11)
Aditya P. Mathur 2007
88
89
90
MC/DC coverage
91
92
93
false
true
t5
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
Test trace-back
When enhancing a test set to satisfy a given coverage criterion, it is desirable to
ask the following question: What portions of the requirements are tested when
the program under test is executed against the newly added test case? The task
of relating the new test case to the requirements is known as test trace-back.
Trace-back assists us in determining whether the new test case is redundant.
116
117
Basic concepts
We will now examine some test adequacy criteria based on the
flow of data in a program. This is in contrast to criteria based on
flow of control that we have examined so far.
Test adequacy criteria based on the flow of data are useful in
improving tests that are adequate with respect to control-flow
based criteria. Let us look at an example.
118
119
Example (contd.)
Neither of the two tests force the use of z
7, at line 9. To do so one
defined on line 6,
requires a test that causes conditions at
lines 5 and 8 to be true.
120
Example (contd.)
Verify that the following test set covers all def-use pairs of z
and reveals the error.
121
122
123
124
125
C-use
Uses of a variable that occurs within an expression as part of an
assignment statement, in an output statement, as a parameter within
a function call, and in subscript expressions, are classified as c-use,
where the c in c-use stands for computational.
How many c-uses of x can you find in the following statements?
Answer: 5
Aditya P. Mathur 2007
126
p-use
The occurrence of a variable in an expression used as a condition in a
branch statement, such as an if or a while, is considered a p-use.
The p in p-use stands for predicate.
How many p-uses of z and x can you find in the following statements?
Answer: 3 (2 of z and 1 of x)
Aditya P. Mathur 2007
127
128
129
130
131
Unreachable node
132
Def-clear path
Any path starting from a node at which
variable x is defined and ending at a node at
which x is used, without redefining x
anywhere else along the path, is a def-clear
path for x.
Path 2-5 is def-clear for variable z
defined at node 2 and used at node 5.
Path 1-2-5 is NOT def-clear for variable z
defined at node 1 and used at node 5.
Thus definition of z at node 2 is live at
node 5 while that at node 1 is not live at
node 5.
Aditya P. Mathur 2007
133
Def-use pairs
Def of a variable at line l1 and its use at line l2 constitute a defuse pair. l1 and l2 can be the same.
dcu (di(x)) denotes the set of all nodes where di(x) is live and
used.
dpu (di(x)) denotes the set of all edges (k, l) such that there is a
def-clear path from node i to edge (k, l) and x is used at node k.
We say that a def-use pair (di(x), uj(x)) is covered when a defclear path that includes nodes i to node j is executed.
If uj(x) is a p-use, then all edges of the kind (j, k) must also be
taken during some executions.
Aditya P. Mathur 2007
134
135
136
137
C-use coverage
138
c-use of x
139
p-use coverage
140
141
All-uses coverage
142
143
144
145
Subsumes relation
146
Subsumes relationship
147
Summary
148
Summary
We have introduced the notion of test adequacy and enhancement.
Two types of adequacy criteria considered: one based on control
flow and the other based on data flow.
Control flow based: statement, decision, condition, multiple
condition, MC/DC, and LCSAJ coverage.
Data flow based: c-uses, p-uses, all-uses, k-dr chain, data
context, elementary data context.
Aditya P. Mathur 2007
149