TESTING
METHODS
LECTURE 3 : WEEK 3
Credit : (3 + 0) / Week
Learning Outcome
2
Code coverage testing
Decision coverage
Condition coverage
Branch coverage
Loop coverage
Path coverage
Data coverage testing
Data flow coverage
Decision Coverage
Decision (Branch) Coverage Method
Causes every decision (if, switch, while, etc.) in
the program to be made both ways (or every
possible way for switch)
System: Design a test case to exercise each
decision in the program each way (true / false)
Completion criterion: A test case for each side of
each decision
Can be checked by track branches taken in execution
Example Decision Coverage
Example Decision Coverage
Read A
Read B
IF A+B > 10 THEN
Print "A+B is Large"
ENDIF
If A > 5 THEN
Print "A Large"
ENDI
Example Decision Coverage
Read A
Read B
IF A+B > 10 THEN
Print "A+B is Large"
ENDIF
If A > 5 THEN
Print "A Large"
ENDI
Example Decision Coverage
To calculate Decision Coverage, one has to find
out the minimum number of paths which will
ensure that all the edges are covered. The aim
is to cover all possible true/false decisions.
(1) 1A-2C-3D-E-4G-5H
(2) 1A-2B-E-4F
Hence Decision Coverage is 2
Condition Coverage
Condition Coverage Method
Like decision coverage, but causes every condition expression
to be exercised both ways (true / false)
A condition is any true / false subexpression in a decision
Example:
if (( x == 1 || y > 2) && z < 3)
Requires separate condition coverage tests for each of:
x == 1 true / false
y > 2 true / false
z < 3 true / false
More effective than simple decision coverage since exercises
the different entry preconditions for each branch selected
Condition Coverage
if ((A || B) && C)
{
<< Few Statements >>
}
else
{
<< Few Statements >>
}
Condition Coverage
So, in our example, the 3 following
tests would be sufficient for 100%
Condition coverage testing.
A = true | B = not eval | C = false
A = false | B = true | C = true
A = false | B = false | C = not eva
Loop Coverage Method
Most programs do their real work in do, while and for loops
This method makes tests to exercise each loop in the program in four
different states
execute body zero times (do not enter loop)
execute body once (i.e., do not repeat)
execute body twice (i.e., repeat once)
execute body many times
Usually used as an enhancement of a statement, block, decision or
condition coverage method
System: Devise test cases to exercise each loop with zero, one, two
and many repetitions
Completion criterion: A test for each of these cases for each loop
Example Loop Coverage
Loop Coverage Method
if there is an upper bound, n, on the number of
times the loop body can be executed, then the
following cases should also be applied.
Design a test in which the loop body is executed
exactly n-1 times.
Design a test in which the loop body is executed
exactly n times.
Try to design a test causing the loop body to be
executed exactly n+1 times
Loop Coverage Method
Apply the following guidelines to ensure that each loop is tested
at its boundaries (that is, with both minimal and maximal
numbers of iterations of the loop body) while using a number of
tests that is only linear in the number of loops:
Conduct the ``simple loop tests'' for the innermost loop (which
is a simple loop), while keeping the number of iterations of the
outer loops at their minimal nonzero values.
Work outward, conducting tests (as described above) for each
loop, while holding the number of iterations of outer loops at
the minimal nonzero values possible (that is, the minimal
values that can be used when the inner loop body is to be
executed the desired number of times), and holding the
Execution Paths
An execution path is a sequence of executed
statements starting at the entry to the unit
(usually the first statement) and ending at the
exit from the unit (usually the last statement)
Two paths are independent if there is at least one
statement on one path which is not executed on
the other
Path analysis (also know as cyclomatic
complexity analysis) identifies all the
Cyclomatic Complexity
Region, R= 6
Number of Nodes = 13
Number of edges = 17
Number of Predicate Nodes = 5
Cyclomatic Complexity
Cyclomatic Complexity for a flow graph is computed in one of three ways:
The numbers of regions of the flow graph correspond to the Cyclomatic
complexity.
Cyclomatic complexity, V(G), for a flow graph G is defined as
V(G) = E – N + 2
where E is the number of flow graph edges and N is the number of flow
graph nodes.
Cyclomatic complexity, V(G), for a graph flow G is also defined as
V(G) = P + 1
Where P is the number of predicate nodes contained in the flow graph G.
Cyclomatic Complexity
Cyclomatic Complexity, V( C) :
V( C ) = R = 6;
Or
V(C) = Predicate Nodes + 1
=5+1 =6
Or
V( C)= E-N+2
= 17-13+2
Execution Paths Analysis
Flow Graphs
It is easiest to do path analysis if we look at the execution
flow graph of the program or unit
The flow graph simply shows program control flow between
basic blocks
Execution Paths Analysis
Execution Paths Analysis
To achieve 100% basis path coverage,
you need to define your basis set. The
cyclomatic complexity of this method is
four (one plus the number of decisions),
so you need to define four linearly
independent paths. To do this, you pick
an arbitrary first path as a baseline, and
then flip decisions one at a time until
Execution Paths Analysis
Path 1: Any path will do for your baseline, so pick true for the decisions' outcomes
(represented as TTT). This is the first path in your basis set.
Path 2: To find the next basis path, flip the first decision (only) in your baseline,
giving you FTT for your desired decision outcomes.
Path 3: You flip the second decision in your baseline path, giving you TFT for your
third basis path. In this case, the first baseline decision remains fixed with the true
outcome.
Path 4: Finally, you flip the third decision in your baseline path, giving you TTF for
your fourth basis path. In this case, the first baseline decision remains fixed with
the true outcome.
So, your four basis paths are TTT, FTT, TFT, and TTF.
Path Coverage Testing
Advantages
Covers all basic blocks (does all of basic block
testing)
Covers all conditions (does all of decision/condition
testing)
Does all of both, but with fewer tests!
Automatable (in practice requires automation)
Disadvantages
Does not take data complexity into account at all
Path Coverage Testing Disadvantages
Example: These
fragments should
be tested the same
way, since they
actually implement
the same solution -
but the one on the
left gets five tests,
whereas the one
on the right gets
only one
White Box Data Coverage
Data Coverage Methods
Data coverage methods explicitly try to
cover the data aspects of the program
code, rather than the control aspects
In this course we will cover data flow
coverage including several different data
flow coverage test criteria.
(Won’t do these in detail, just overview)
White Box Data Coverage
Data Flow Coverage
Data flow coverage is concerned with variable definitions
and uses along execution paths
A variable is defined if it is assigned a new value during a
statement execution
A variable definition in one statement is alive in another if
there is a path between the two statements that does not redefine
the variable
There are two types of variable uses
A P-use of a variable is a predicate use (e.g. if statement)
A C-use of a variable is a computation use or any other use (e.g.
I/O statements)
Example: Definition, P-Use, C-Use
of Variables
Example: Definition, P-Use, C-Use of
Variables
Example: Definition Clear path
Example: Definition-C-use Association
Example: Definition-P-use Association
White Box Data Coverage
Data Flow Coverage
There are a variety of different testing strategies related
to data flow:
All-Uses coverage: test all uses of each definition
All-Defs coverage: test each definition at least once
All C-Uses/Some P-Uses coverage: test all computation uses. If
no computation uses for a given definition then test at least
one predicate use
All P-Uses/Some C-Uses coverage: test all predicate uses. If no
predicate uses for a given definition then test at least one
computation use
White Box Data Coverage
Data Flow Coverage
We have covered definitions of data, uses of data,
and testing strategies for data flow coverage.
System: Identify definitions (and uses) of variables
and testing strategy. design a set of test cases
that cover the testing strategy
Completion criterion: Depends on the test
strategy. For example, in All-Defs we are done
when we have a test case for each variable
Summary
Testing Methods: White Box Testing II
Code coverage methods:
Decision analysis methods
(decision, condition, loop coverage, path
coverage)
Data coverage methods:
data flow coverage
Assignment
36 END OF LECTURE
Any Questions !!!