Testing Cyclomatic
Testing Cyclomatic
Testing Cyclomatic
2
How do you test a system?
3
How usually you test something?
How do you test a system?
5
Errors and Failures
A failure is a manifestation of an error.
presence of an error may not lead to a failure.
6
Test cases and Test suite
A TEST CASE is a set of actions executed to
verify a particular feature or functionality of
your software application. A Test Case
contains test steps, test data, precondition,
postcondition
7
1. Check system behavior when valid email id and password is entered.
2. Check system behavior when invalid email id and valid password is
entered.
3. Check system behavior when valid email id and invalid password is
entered.
4. Check system behavior when invalid email id and invalid password is
entered.
5. Check system behavior when email id and password are left blank and
Sign in entered.
6. Check Forgot your password is working as expected
7. Check system behavior when valid/invalid phone number and
password is entered.
1. Check system behavior when "Keep me signed" is checked
8
9
Test cases and Test suite
11
12
13
14
15
Verification versus Validation
Verificationis the process of
determining:
whether output of one phase of development
conforms to its previous phase.
Validation is the process of determining
whether a fully developed system conforms
to its SRS document.
16
Verification versus Validation
Aim of Verification:
phase containment of errors
Aim of validation:
final product is error free.
17
Verification versus Validation
Verification:
are we doing right?
Validation:
have we done right?
18
Design of Test Cases
Exhaustive testing of any system is
impractical:
input data domain is extremely large.
Design an optimal test suite:
of reasonable size
to uncover as many errors as possible.
19
Design of Test Cases
If test cases are selected randomly:
many test cases do not contribute to the
significance of the test suite,
do not detect errors not already detected by
other test cases in the suite.
The number of test cases in a randomly
selected test suite:
not an indication of the effectiveness of the
testing.
20
TYPES/APPROACHES
OF
SOFTWARE TESTING
21
Two main approaches to design test cases:
Black-box approach
White-box (or glass-box) approach
22
Black Box Testing
23
Black-box Testing
Testcases are designed using only
functional specification of the software:
without any knowledge of the internal
structure of the software.
Forthis reason, black-box testing is also
known as functional testing.
24
25
White-box Testing
Designing white-box test cases:
requires knowledge about the internal
structure of software.
white-box testing is also called
structural testing.
26
27
Black-box Testing
There are essentially two main approaches to
design black box test cases:
Equivalence class partitioning
Boundary value analysis
28
29
Equivalence Class
Partitioning
Input values to a program are
partitioned into equivalence classes
.
Partitioning is done such that:
program behaves in similar ways to
every input value belonging to an
equivalence class.
30
Why define equivalence classes?
Test the code with just one representative value from each equivalence class:
31
Equivalence Class Partitioning
How do you determine the equivalence classes?
examine the input data.
few general guidelines for determining the equivalence classes can
be given
32
Equivalence Class Partitioning
Ifthe input data to the program is
specified by a range of values:
e.g.numbers between 1 to 5000.
one valid and two invalid equivalence
classes are defined.
1 5000
33
Equivalence Class Partitioning
34
Example (cont.)
There are three equivalence classes:
35
Example (cont.)
The test suite must include:
representatives from each of the three
equivalence classes:
a possible test suite can be:
{-5,500,6000}.
1 5000
36
Boundary Value Analysis
Some typical programming errors occur:
37
Boundary Value
Analysis
Programmers may improperly use < instead of <=
Boundary value analysis:
select test cases at the boundaries of different equivalence classes.
38
Example
For a function that computes the
square root of an integer in the
range of 1 and 5000:
testcases must include the values:
{0,1,5000,5001}.
1 5000
39
White-Box Testing
There exist several popular white-box
testing methodologies:
Statement coverage
branch coverage
path coverage
condition coverage
mutation testing
data flow-based testing
40
Statement Coverage
Statement coverage
methodology:
design test cases so that
every statement in a program
is executed at least once.
41
Statement Coverage
The principal idea:
unless a statement is executed,
we have no way of knowing if an error exists in that statement.
42
Example
int f1(int x, int y){
1 while (x != y){
2 if (x>y) then
3 x=x-y;
4 else y=y-x;
5 }
6 return x; }
47
Condition Coverage
Test cases are designed such that:
each component of a composite conditional expression is
executed at least once.
given both true and false values.
48
Example
Consider the conditional expression
((c1.and.c2).or.c3):
Each of c1, c2, and c3 are exercised at least once,
i.e. given true and false values.
49
Branch testing
Condition testing
stronger testing than branch testing:
Branch testing
stronger than statement coverage testing.
50
Condition coverage
Consider a boolean
expression having n
components:
for condition coverage
n
we require 2 test cases.
(n is no. of conditions)
51
Path Coverage
Defined in terms of
controlflow graph
(CFG) of a program.
53
Path coverage-based testing
To understand the path coverage-based testing:
we need to learn how to draw control flow graph of a program.
54
Control flow graph (CFG)
55
How to draw Control flow graph?
Number all the statements of a program.
Numbered statements:
represent nodes of the control flow graph.
56
How to draw Control flow
graph?
Sequence:
1
1 a=5;
2 b=a*b-1;
2
57
How to draw Control flow
graph?
Selection:
1 if(a>b) then 1
2 c=3;
3 else c=5;
2 3
4 c=c*c; 4
58
How to draw Control flow
graph?
1
Iteration:
1 while(a>b){
2 b=b*a; 2
3 b=b-1;}
4 c=b+d;
3
4
59
Example
intf1(int x,int y){
1 while (x != y){
2 if (x>y) then
3 x=x-y;
4 else y=y-x;
5 }
6 return x; }
60
Example Control Flow Graph
1
2
3 4
5
6
Path
A path through a program:
a node and edge sequence from the starting node to a terminal
node of the control flow graph.
There may be several terminal nodes for program.
62
Independent path
Any path through the program:
introducing at least one new node:
that is not included in any other independent paths.
63
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.
64
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.
65
McCabe's cyclomatic metric
66
Example Control Flow Graph
1
2
3 4
5
6
67
Example
Cyclomatic complexity
=
7-6+2 = 3.
68
Cyclomatic complexity
Anotherway of computing
cyclomatic complexity:
inspect control flow graph
determine number of bounded
areas in the graph
V(G)= Total number of
bounded areas + 1
69
Bounded area
Any region enclosed
by a nodes and edge
sequence.
70
Example Control Flow Graph
1
2
3 4
5
6
71
Example
From a visual examination of the CFG:
the number of bounded areas is 2.
cyclomatic complexity = 2+1=3.
72
IF A = 10 THEN
IF B > C
THEN A = B
ELSE A = C
ENDIF
ENDIF
Print A Print B Print C
73
Cyclomatic complexity
77
Cyclomatic complexity
80
Derivation of Test Cases
1
2
3 4
5
6
Number of independent paths: 3
1,6 test case (x=1, y=1)
1,2,3,5,1,6 test case(x=1, y=2)
1,2,4,5,1,6 test case(x=2, y=1) 83
Exercise: 2
IF A = 354
THEN IF B >C
THEN A = B
ELSE A= C
ENDIF
ENDIF
Print A
84
Exercise:3
i = 0;
while (i<n-1)
do j = i + 1;
while (j<n)
do
if A[i]<A[j] then
swap (A[i], A[j]);
end do;
i=i+1;
85
end do;
Data Flow-Based Testing
86
Advantages of Data Flow Testing:
87
Mutation Testing
88
Definition
89
90
Mutation Testing
92
Mutation Testing
A major disadvantage of
mutation testing:
computationally very expensive,
a large number of possible
mutants can be generated.
93