Escolar Documentos
Profissional Documentos
Cultura Documentos
Testing
Lecture# 35
DCS, NUCES, Islamabad Spring 2013 2
Why Testing and Analysis?
If not
Defect
removed
1. Do you have a test case that represents a valid scalene triangle? (Note that
test cases such as 1, 2, 3 and 2, 5, 10 do not warrant a “yes” answer
because there does not exist a triangle having these dimensions.)
2. Do you have a test case that represents a valid equilateral triangle?
3. Do you have a test case that represents a valid isosceles triangle? (Note
that a test case representing 2, 2, 4 would not count because it is not a
valid triangle.)
4. Do you have at least three test cases that represent valid isosceles
triangles such that you have tried all three permutations of two equal sides
(such as, 3, 3, 4; 3, 4, 3; and 4, 3, 3)?
5. Do you have a test case in which one side has a zero value?
6. Do you have a test case in which one side has a negative value?
7. Do you have a test case with three integers greater than zero such that the
sum of two of the numbers is equal to the third? (That is, if the program
said that 1, 2, 3 represents a scalene triangle, it would contain a bug.)
8 Do you have at least three test cases in category 7 such that you have tried
all three permutations where the length of one side is equal to the sum of
the lengths of the other two sides (for example, 1, 2, 3; 1, 3, 2; and 3, 1, 2)?
9 Do you have a test case with three integers greater than zero such that the
sum of two of the numbers is less than the third (such as 1, 2, 4 or
12,15,30)?
10 Do you have at least three test cases in category 9 such that you have tried
all three permutations (for example, 1, 2, 4; 1, 4, 2; and 4, 1, 2)?
11 Do you have a test case in which all sides are zero (0, 0, 0)?
12 Do you have at least one test case specifying noninteger values (such as
2.5, 3.5, 5.5)?
13 Do you have at least one test case specifying the wrong number of values
(two rather than three integers, for example)?
14 For each test case did you specify the expected output from the program
in addition to the input values?
Boris Beizer
OBJECTIVE ---- to uncover errors
CRITERIA ----- In a complete manner
CONSTRAINT ------ with a minimum of effort and time
FURTHER
Logical errors and incorrect assumptions are inversely
proportional to the probability that a program path will
be executed.
Sometime a path executes counterintuitive.
Typographical errors are random.
Also: if black box testing finds error, locating it is easier
with additional white box testing!
DCS, NUCES, Islamabad Spring 2013 31
Exhaustive Testing
loop 20
loop 20
Gives an abstract
representation of the code
Directed Graph
Nodes are blocks of
sequential statements
Edges are transfers of control
May be labeled with predicate
representing the condition of
control transfer
41
Control Flow Graph - Example
42
Basics of CFG: Patterns
43
Control Flow Coverage
44
(1) Statement/Node Coverage
45
Statement coverage
46
Incompleteness of Statement/Node
Coverage
47
Statement coverage
Problems:
Line 2: if the requirement was || rather than &&
unchanged
Same test (a=2, b=0) tests all statements but
48
Decision (or branch) Coverage
49
Decision Coverage
If there is an error for x >= 0, it will be discovered
by decision coverage *
If (A or B)
TF and FF will toggle the decision *
50
Decision coverage
You must write enough test
cases that each decision
has a T and a F outcome at
least once.
ace & abd or acd & abe
(2, 0, 4) & (1, 1, 1)
(3, 0, 3) & (2, 1, 1)
ace & abe
(2, 0, X) & (2, 1, X)
Problem
If the condition x > 1 had a fault,
e.g., x < 1, the mistake would
not be detected
51
Test cases for Decision Coverage
If (A or B)
TF/FT and FF will toggle the conditions
53
Condition coverage
A > 1, A <= 1, B = 0, B != 0
A = 2, A != 2, X > 1, X <= 1
54
Problem with Condition Coverage
May not cover decisions
Example
A. If (A && B)
1) A: True, B: False
2) A: False, B: True
B. If (A || B)
1) A: True, B: False
2) A: False, B: True
TC21 TC22
Path Coverage – Example
Path Coverage – Dealing with Loops
In practice, however, the number of paths can be too
large, if not infinite (e.g., when we have loops) →
Impractical
A pragmatic heuristic: Look for conditions that
execute loops
Zero times
A maximum number of times
A average number of times (statistical criterion)
For example, in the array search algorithm
Skipping the loop (the table is empty)
Executing the loop once or twice and then finding the element
Searching the entire table without finding the desired element
Path Coverage: Loops
Another Example
Path Coverage - Example
Control Flow Coverage: Reachability
Not all statements are usually reachable in real-
world programs
It is not always possible to decide automatically
if a statement is reachable and the percentage of
reachable statements
When one does not reach a 100% coverage, it is
therefore difficult to determine the reason
Tools are needed to support this activity but it
cannot be fully automated
Research focuses on search algorithms to help
automate coverage
Control flow testing is, in general, more
applicable for testing small pieces of code
Cyclomatic Complexity
Cyclomatic complexity provides a quantitative
measure of the logical complexity of a program
– It is the number of tests that must be conducted
to assure that all statements have been executed
at least once
Cyclomatic complexity, V(G) is given by:
1 Node
Edge
(1) V(G) = E - N + 2 2,3
where E is the number of edges and N 6 R2 4,5
number of nodes (11-9 +2 = 4) 7 R3 8
9 R1
(2) V(G) = P + 1, where P is the number
of predicate nodes (3+1 = 4)
10 Region
(3) V(G) = number of regions (4)
11 R4
65
Independent Paths
67
Basis Path Testing
1
derive the
independent paths:
2 Since V(G) = 4,
there are four paths
3
4
5 6 Path 1: 1,2,3,6,7,8
Path 2: 1,2,3,5,7,8
Path 3: 1,2,4,7,8
7
Path 4: 1,2,4,7,2,4,...7,8
8
derive test cases to exercise
these paths.
68
Control Structure Testing: Loop
Nested
Loops
Simple Concatenated
loop Unstructured
Loops Loops
69
Loop Testing: Simple Loops
Concatenated Loops
If the loops are independent of one another
then treat each as a simple loop
else
treat as nested* loops
endif
* for example, the final loop counter value of loop 1
is used to initialize loop 2
For Other control structures DIY
71
Black-Box Testing
requirements
output
input events
Equivalence classes:
partitions of the input set in which input data have the same
effect on the program (e.g., the result in the same output)
Entire input set is covered: completeness
Disjoint classes: to avoid redundancy
Test cases: one element of each equivalence class
But equivalence classes have to be chosen wisely
Guessing the likely system behavior is needed
77
Example
Travel service offers discounts to
travelers based on their age.
0-4 years 100%
5-15 years 50%
16-64 years 0%
64 years and older 25%
Equivalence classes for age
0, 1, 2, 3, 4
5, 6, 7, …15
16, 17, 18, …64
65, 66, 67, …120
Similarly for destinations
e.g., destination can be grouped
based on regions (that have
same fair)
Nothing special for name
78
Identifying Equivalence classes
A key concept in the identification of classes is
negation, i.e. If a characteristic is identified as an
equivalence class, then one should immediately
negate the characteristic in order to find
examples of classes which should cause the
module to do something different such as
“generate an error message”.
Partitioning each input condition into two or
more groups.
Two types of equivalence classes are identified:
1. Valid Equivalence Classes
83
Black-Box Testing
86
Example
87
Example – Identifying boundaries
88
Construct Test sets
89
Error-Guessing
2. The actor input the first number by clicking on the 3. The digit is displayed on the screen.
keypad or using keyboard.
4. The actor clicks or presses the “+” key.
5.The actor then adds the second number as (2). 6. The pressed digit is displayed on the screen.
7. The actor clicks the “=” key. 8. The sum of the two digits is displayed on the screen.