Unit 4-Testing
Unit 4-Testing
1
Software Testing
• What is Testing?
Many people understand many definitions of testing :
2
Software Testing
A more appropriate definition is:
3
Software Testing
• Why should We Test ?
In the software life cycle the earlier the errors are discovered and removed,
the lower is the cost of their removal.
4
Software Testing
• Who should Do the Testing ?
o Testing requires the developers to find errors from their software.
o It is difficult for software developer to point out errors from own
creations.
o Many organisations have made a distinction between development
and testing phase by making different people responsible for each
phase.
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International 5
Publishers, 2007
Software Testing
• What should We Test ?
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International 6
Publishers, 2007
Software Testing
Some Terminologies
➢ Error, Mistake, Bug, Fault and Failure
People make errors. A good synonym is mistake. This may be a syntax
error or misunderstanding of specifications. Sometimes, there are logical
errors.
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
9
Publishers, 2007
Software Testing
➢ Test, Test Case and Test Suite
Test and Test case terms are used interchangeably. In practice, both are
same and are treated as synonyms. Test case describes an input
description and an expected output description.
Test Case ID
Section-I Section-II
(Before Execution) (After Execution)
Purpose : Execution History:
Pre condition: (If any) Result:
Inputs: If fails, any possible reason (Optional);
Expected Outputs: Any other observation:
Post conditions: Any suggestion:
Written by: Run by:
Date: Date:
Fig. 2: Test case template
The set of test cases is called a test suite. Hence any combination of test
cases may generate a test suite.
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
10
Publishers, 2007
Software Testing
11
1
0
1
1
Software Testing
Boundary Value Analysis
Consider a program with two input variables x and y. These input variables
have specified boundaries as:
a≤x≤b
c≤y≤
d
Input
domain
d
y
c
a x b
14
Fig.4: Input domain for program having two input variables
2.2 Boundary Value Analysis
• Test cases that have values on the boundaries
of equivalence classes.
• Chose input lie at the edges of
equivalence class.
• So, if the range is 0.0 < x< 1.0, then the test
cases are 0.0, 1.0 (valid inputs), and -0.1,
and
1.1 (for invalid inputs).
1
3
Software Testing
The boundary value analysis test cases for our program with two inputs
variables (x and y) that may have any value from 100 to 300 are: (200,100),
(200,101), (200,200), (200,299), (200,300), (100,200), (101,200), (299,200) and
(300,200). This input domain is shown in Fig. 8.5. Each dot represent a test case and
inner rectangle is the domain of legitimate inputs. Thus, for a program of n variables,
boundary value analysis yield 4n + 1 test cases.
Input domain
400
300
y
200
100
0
100 200
x 300
400
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
16
Publishers, 2007
Software Testing
Solution
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
17
Publishers, 2007
Software Testing
The boundary value test cases are :
Test Case a b c Expected output
1 0 50 50 Not Quadratic
2 1 50 50 Real Roots
3 50 50 50 Imaginary Roots
4 99 50 50 Imaginary Roots
5 100 50 50 Imaginary Roots
6 50 0 50 Imaginary Roots
7 50 1 50 Imaginary Roots
8 50 99 50 Imaginary Roots
9 50 100 50 Equal Roots
10 50 50 0 Real Roots
11 50 50 1 Real Roots
12 50 50 99 Imaginary Roots
13 50 50 100 Imaginary Roots
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
18
Publishers, 2007
Software Testing
Example – 8.2
Consider a program for determining the Previous date. Its input is a triple of
day, month and year with the values in the range
1 ≤ month ≤ 12
1 ≤ day ≤ 31
1900 ≤ year ≤ 2025
The possible outputs would be Previous date or invalid input date. Design the
boundary value test cases.
19
Software Testing
Solution
The Previous date program takes a date as input and checks it for validity.
If valid, it returns the previous date as its output.
With single fault assumption theory, 4n+1 test cases can be designed and
which are equal to 13.
20
Software Testing
The boundary value test cases are:
Test Case Month Day Year Expected output
21
Software Testing
Example – 8.3
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
22
Publishers, 2007
Software
Solution
Testing
The boundary value test cases are shown below:
Test case x y Z Expected Output
1 50 50 1 Isosceles
2 50 50 2 Isosceles
3 50 50 50 Equilateral
4 50 50 99 Isosceles
5 50 50 100 Not a triangle
6 50 1 50 Isosceles
7 50 2 50 Isosceles
8 50 99 50 Isosceles
9 50 100 50 Not a triangle
10 1 50 50 Isosceles
11 2 50 50 Isosceles
12 99 50 50 Isosceles
13 100 50 50 Not a triangle
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
23
Publishers, 2007
Black-Box Testing
• Test cases are decided solely on the basis of the requirements or
specifications of the program or module, and the internals of the module
or the program are not considered for selection of test cases.
• The tester only knows the inputs that can be give into the system and
what output the system should give.
2
4
2.1 Equivalence Class Partitioning
2
5
• If the input data values to a system can be
specified by a range of values, then one valid
and two invalid equivalence classes should be
defined.
• If the input data assumes values from a set of
discrete members of some domain, then one
equivalence class for valid input values and
another equivalence class for invalid input
values should be defined.
2
6
Software Testing
Invalid input
Most of the time, equivalence class testing defines classes of the input domain.
However, equivalence classes should also be defined for output domain. Hence,
we should design equivalence classes based on input and output domain.
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
62
Publishers, 2007
Software Testing
Example 8.7
Consider the program for the determination of nature of roots of a quadratic
equation as explained in example 8.1. Identify the equivalence class test
cases for output and input domains.
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
63
Publishers, 2007
Software Testing
Solution
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
64
Publishers, 2007
Software Testing
We may have another set of test cases based on input domain.
I1= {a: a = 0}
I2= {a: a <
0}
I3= {a: 1 ≤ a ≤ 100}
I4= {a: a > 100}
I5= {b: 0 ≤ b ≤ 100}
I6= {b: b < 0}
I7= {b: b > 100}
I8= {c: 0 ≤ c ≤ 100}
I9= {c: c < 0}
I10={c: c > 100}
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
65
Publishers, 2007
Software Testing
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
66
Publishers, 2007
Software Testing
Example 8.8
Consider the program for determining the previous date in a calendar as
explained in example 8.3. Identify the equivalence class test cases for output
& input domains.
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
67
Publishers, 2007
Software Testing
Solution
Output domain equivalence class are:
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
68
Publishers, 2007
Software Testing
We may have another set of test cases which are based on input domain.
I1={month: 1 ≤ m ≤ 12}
I2={month: m < 1}
I3={month: m > 12}
I4={day: 1 ≤ D ≤ 31}
I5={day: D < 1}
I6={day: D > 31}
I7={year: 1900 ≤ Y ≤ 2025}
I8={year: Y < 1900}
I9={year: Y > 2025}
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
69
Publishers, 2007
Software Testing
Inputs domain test cases are :
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
70
Publishers, 2007
Software Testing
Example – 8.9
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
71
Publishers, 2007
Software Testing
Solution
Output domain equivalence classes are:
Software Engineering (3 rd ed.), By K.K Aggarwal & Yogesh Singh, Copyright © New Age International
72
Publishers, 2007
Software Testing
Input domain based classes are:
75
Software Testing
76
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
5
5
White Box Testing
• Is concerned with testing the implementation of the program.
5
6
Approaches
• control flow-based testing:- control flow graph (or simply flow
graph) of a program P be G.
A node in this graph represents a block of statements that is always
• executed together.
An edge (i, j) represents a possible transfer of control after executing the
• last statement of the block represented by node i to the first statement of
the block represented by node j .
A node corresponding to a block whose first statement is the start
statement of P is called the start node of G, and a node corresponding to
• a block whose last statement is an exit statement is called an exit node.
A complete path is a path whose first node is the start node and the
last node is an exit node.
• 5
7
5
8
5
9
6
0
• each circle, called a flow graph node, represents one or more procedural
statements.
• The arrows on the flow graph, called edges or links, represent flow of
control.
• Each node that contains a condition is called a predicate node and is characterized
by two or more edges emanating from it.
–Each compound condition in a conditional expression containing one or more 6
Boolean operators (e.g., and, or) is represented by a separate predicate node 1
Consider flow-chart
6
2
Corresponding Flow-Graph
6
3
6
4
A Second Flow Graph Example
3
4
6
8 7
10 9 16
11 17
13 12
14
6
5
15 49
A Sample Function to Diagram and Analyze
1 int functionZ(int y) 3
2 {
3 int x = 0;
4
4 while (x <= (y * y))
5 { 6 7
6 if ((x % 11 == 0) &&
7 (x % y == 0))
9
8
9
{
printf(“%d”, x); x+
12 13
0 +;
1 } // End if 10
2 else if ((x % 7 == 0) 15
3 ||
4 (x % y == 1)) 16
5 {
6 printf(“%d”, y); x 18
7 = x + 2;
8 } // End else
9 printf(“\n”); 20
} // End
while
0 printf("End of list\n"); 21 6
1 return 0; 6
2 } // End functionZ 50
Compound logic
6
7
Cyclomatic Complexity
• Cyclomatic complexity is a software metric that provides a
quantitative measure of the
logical complexity of a program.
• When used in the context of the basis path testing
method, the value computed for cyclomatic complexity defines
the number of independent paths in the basis set of a program
and provides us with an upper bound for the number of tests
that must be conducted to ensure that all statements have
been
executed at least once.
6
8
Path
• A path through a program is a node and edge sequence
from the starting node to a terminal node of the control
flow graph of a program. There can be more than one
terminal node in a program.
6
9
linearly independent path
• A linearly independent path is any path through the
program that introduces at
least one new edge that is not included in any other
linearly independent paths.
• If a path has one new node compared to all other
linearly independent paths.
• For example, a set of independent paths for the flow
graph illustrated in Figure
7
0
path 1: 1-11
path 2: 1-2-3-4-5-10-1-11
path 3: 1-2-3-6-8-9-10-1-11
path 4: 1-2-3-6-7-9-10-1-11
7
1
Methods
• There are three different ways to compute the cyclomatic complexity.
• The number 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, N is the number of flow
graph nodes.
• Cyclomatic complexity, V(G), for a flow graph, G, is also defined as
V(G) = P + 1
where P is the number of predicate nodes contained in the flow graph
G.
7
2
Example
1. The flow graph has four regions.
2. V(G) = 11 edges 9 nodes + 2 = 4.
3. V(G) = 3 predicate nodes + 1 = 4.
7
3
Graph matrices
• A graph matrix is a square matrix whose size
(i.e., number of rows and columns)
is equal to the number of nodes on the flow
graph. Each row and column corresponds
to an identified node, and matrix entries
correspond to connections (an edge) between
nodes.
7
4
7
5
• Each letter has been replaced with a 1, indicating that a connection
exists (zeros have been excluded for clarity). Represented in this form,
the graph matrix is called a connection matrix.
• Each row with two or more entries represents a predicate node.
7
6
Loop Testing - General
• A white-box testing technique that focuses
exclusively on the validity of loop constructs
• Four different classes of loops exist
– Simple loops
– Nested loops
– Concatenated loops
– Unstructured loops
7
7
61
7
8
Testing of Simple Loops
1) Skip the loop entirely
2) Only one pass through the loop
3) Two passes through the loop
4) m passes through the loop, where m < n
5) n –1, n, n + 1 passes through the loop
7
9
79
Testing of Nested Loops
1) Start at the innermost loop; set all other loops to minimum
values
2) Conduct simple loop tests for the innermost loop while
holding the outer loops at their minimum iteration
parameter values; add other tests for out-of-range or
excluded values
3) Work outward, conducting tests for the next loop, but
keeping all other outer loops at minimum values and other
nested loops to “typical” values
4) Continue until all loops have been tested
8
0
80
Testing Concatenated Loop
• Concatenated loops can be tested using the
approach defined for simple loops, if each of the
loops is independent of the other. However, if
two loops are concatenated and the loop counter
for loop 1 is used as the initial value for loop 2,
then the loops are not independent. When the
loops are not independent, the approach applied
to nested loops is recommended.
8
1
Testing Unstructured Loops
• Whenever possible, this class of loops should
be redesigned to reflect the use of the
structured programming constructs.
8
2