1
White Box Testing
2
Organization of this
Lecture
Introduction to Testing.
White-box testing:
statement coverage
path coverage
branch testing
condition coverage
Cyclomatic complexity
Summary
3
White-Box Testing
There exist several popular white-box
testing methodologies:
Statement coverage
branch coverage
path coverage
condition coverage
mutation testing
Statement Coverage
In a programming language, a statement is
nothing but the line of code or instruction for
the computer to understand and act
accordingly. A statement becomes an
executable statement when it gets compiled and
converted into the object code and performs the
action when the program is in a running mode.
Hence “Statement Coverage”, as the name
itself suggests, it is the method of validating
whether each and every line of the code is
executed at least once.
 INPUT A & B
 C = A + B
 IF C>100
 PRINT “ITS DONE”
 For Statement Coverage – we would only need one
test case to check all the lines of the code.
 That means:
 If I consider TestCase_01 to be (A=40 and B=70),
then all the lines of code will be executed.
 Now the question arises:
 Is that sufficient?
 What if I consider my Test case as A=33 and
B=45?
 Because Statement coverage will only cover the true
side, for the pseudo code, only one test case would
NOT be sufficient to test it. As a tester, we have to
consider the negative cases as well.
 Hence for maximum coverage, we need to consider
“Branch Coverage”, which will evaluate the “FALSE”
conditions.
Branch Coverage
“Branch” in a programming language
is like the “IF statements”. An IF
statement has two branches: True
and False.
So in Branch coverage (also called
Decision coverage), we validate
whether each branch is executed at
least once.
In case of an “IF statement”, there will be
two test conditions:
One to validate the true branch and,
Other to validate the false branch.
Hence, in theory, Branch Coverage is a testing
method which is when executed ensures that
each and every branch from each decision point
is executed.
INPUT A & B
C = A + B
IF C>100
PRINT “ITS DONE”
ELSE
PRINT “ITS PENDING”
Since Statement coverage is not sufficient
to test the entire pseudo code, we would
require Branch coverage to ensure
maximum coverage.
So for Branch coverage, we would require two
test cases to complete the testing of this pseudo
code.
TestCase_01: A=33, B=45
TestCase_02: A=25, B=30
With this, we can see that each and every line
of the code is executed at least once.
 Here are the Conclusions that are derived so far:
 Branch Coverage ensures more coverage than
Statement coverage.
 Branch coverage is more powerful than Statement
coverage.
 100% Branch coverage itself means 100% statement
coverage.
 But 100 % statement coverage does not guarantee
100% branch coverage.
Path Coverage
Path coverage tests all the paths of the
program. This is a comprehensive
technique which ensures that all the paths
of the program are traversed at least
once. Path Coverage is even more
powerful than Branch coverage. This
technique is useful for testing the complex
programs.
 Path coverage is used to test the
complex code snippets, which basically
involve loop statements or combination of
loops and decision statements.
 Consider this pseudocode:
 INPUT A & B
 C = A + B
 IF C>100
 PRINT “ITS DONE”
 END IF
 IF A>50
 PRINT “ITS PENDING”
 END IF
 there are 2 decision statements, so for
each decision statement, we would need
two branches to test. One for true and the
other for the false condition. So for 2
decision statements, we would require 2
test cases to test the true side and 2 test
cases to test the false side, which makes
a total of 4 test cases.
In order to have the full coverage, we would
need following test cases:
TestCase_01: A=50, B=60
TestCase_02: A=55, B=40
TestCase_03: A=40, B=65
TestCase_04: A=30, B=30
17
Statement Coverage
Statement coverage methodology:
design test cases so that
every statement in a program
is executed at least once.
18
Statement Coverage
The principal idea:
unless a statement is executed,
we have no way of knowing if an
error exists in that statement.
19
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; }
20
Branch Coverage
Test cases are designed such that:
different branch conditions
given true and false values in
turn.
21
Branch Coverage
Branch testing guarantees statement
coverage:
a stronger testing compared to the
statement coverage-based testing.
22
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; }
23
Example
Test cases for branch coverage can
be:
{(x=3,y=3),(x=3,y=2), (x=4,y=3),
(x=3,y=4)}
24
Condition Coverage
Test cases are designed such that:
each component of a composite
conditional expression
given both true and false
values.
25
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.
26
Condition testing
stronger testing than branch
testing:
Branch testing
stronger than statement coverage
testing.
27
Condition coverage
Consider a boolean expression
having n components:
for condition coverage we require
2n
test cases.
28
Path Coverage
Design test cases such that:
all linearly independent paths
in the program are executed at
least once.
29
Linearly independent paths
Defined in terms of
control flow graph (CFG) of a
program.
30
Path coverage-based
testing
To understand the path coverage-
based testing:
we need to learn how to draw
control flow graph of a program.
31
Control flow graph (CFG)
A control flow graph (CFG) describes:
the sequence in which different
instructions of a program get
executed.
the way control flows through the
program.
32
How to draw Control flow
graph?
Number all the statements of a
program.
Numbered statements:
represent nodes of the control
flow graph.
33
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; }
34
Example Control Flow
Graph
1
2
3 4
5
6
35
How to draw Control flow
graph?
Sequence:
1 a=5;
2 b=a*b-1;
1
2
36
How to draw Control flow
graph?
Selection:
1 if(a>b) then
2 c=3;
3 else c=5;
4 c=c*c;
1
2 3
4
37
How to draw Control flow
graph?
Iteration:
1 while(a>b){
2 b=b*a;
3 b=b-1;}
4 c=b+d;
1
2
3
4
38
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.
39
Independent path
Any path through the program:
introducing at least one new node:
that is not included in any other
independent paths.
40
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.
41
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.
42
McCabe's cyclomatic
metric
Given a control flow graph G,
cyclomatic complexity V(G):
 V(G)= E-N+2
N is the number of nodes in G
E is the number of edges in G
43
Example Control Flow
Graph
1
2
3 4
5
6
44
Example
Cyclomatic complexity =
7-6+2 = 3.
45
Cyclomatic complexity
Another way of computing cyclomatic
complexity:
inspect control flow graph
determine number of bounded areas in
the graph
V(G) = Total number of bounded
areas + 1
46
Bounded area
Any region enclosed by a nodes and
edge sequence.
47
Example Control Flow
Graph
1
2
3 4
5
6
48
Example
From a visual examination of the
CFG:
the number of bounded areas is 2.
cyclomatic complexity = 2+1=3.
49
Cyclomatic complexity
McCabe's metric provides:
a quantitative measure of
testing difficulty and the ultimate
reliability
Intuitively,
number of bounded areas
increases with the number of
decision nodes and loops.
50
Cyclomatic complexity
The first method of computing V(G)
is amenable to automation:
you can write a program which
determines the number of nodes
and edges of a graph
applies the formula to find V(G).
51
Cyclomatic complexity
The cyclomatic complexity of a
program provides:
a lower bound on the number of
test cases to be designed
to guarantee coverage of all
linearly independent paths.
52
Cyclomatic complexity
Defines the number of independent
paths in a program.
Provides a lower bound:
for the number of test cases for
path coverage.
53
Cyclomatic complexity
Knowing the number of test cases
required:
does not make it any easier to
derive the test cases,
only gives an indication of the
minimum number of test cases
required.
54
Path testing
The tester proposes:
an initial set of test data using his
experience and judgement.
55
Path testing
A dynamic program analyzer is
used:
to indicate which parts of the
program have been tested
the output of the dynamic analysis
used to guide the tester in selecting
additional test cases.
56
Derivation of Test Cases
Let us discuss the steps:
to derive path coverage-based test
cases of a program.
57
Derivation of Test Cases
Draw control flow graph.
Determine V(G).
Determine the set of linearly
independent paths.
Prepare test cases:
to force execution along each
path.
58
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; }
59
Example Control Flow
Diagram
1
2
3 4
5
6
60
Derivation of Test Cases
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)
61
An interesting application
of cyclomatic complexity
Relationship exists between:
McCabe's metric
the number of errors existing in
the code,
the time required to find and
correct the errors.
62
Cyclomatic complexity
Cyclomatic complexity of a program:
also indicates the psychological
complexity of a program.
difficulty level of understanding
the program.
63
Cyclomatic complexity
From maintenance perspective,
limit cyclomatic complexity
of modules to some reasonable
value.
Good software development
organizations:
restrict cyclomatic complexity of
functions to a maximum of ten or
so.
64
Data Flow-Based Testing
Selects test paths of a
program:
according to the locations of
definitions and uses of different
variables in a program.
65
Data Flow-Based Testing
For a statement numbered S,
DEF(S) = {X/statement S contains
a definition of X}
USES(S)= {X/statement S contains
a use of X}
Example: 1: a=b; DEF(1)={a},
USES(1)={b}.
Example: 2: a=a+b; DEF(1)={a},
USES(1)={a,b}.
66
Data Flow-Based Testing
A variable X is said to be live
at statement S1, if
X is defined at a statement S:
there exists a path from S to S1
not containing any definition of
X.
67
DU Chain Example
1 X(){
2 a=5; /* Defines variable a */
3 While(C1) {
4 if (C2)
5 b=a*a; /*Uses variable a */
6 a=a-1; /* Defines variable a */
7 }
8 print(a); } /*Uses variable a */
68
Definition-use chain (DU
chain)
[X,S,S1],
S and S1 are statement numbers,
X in DEF(S)
X in USES(S1), and
the definition of X in the statement S is
live at statement S1.
69
Data Flow-Based Testing
One simple data flow testing
strategy:
every DU chain in a program be
covered at least once.
70
Data Flow-Based Testing
Data flow testing strategies:
useful for selecting test paths of a
program containing nested if and
loop statements
71
1 X(){
2 B1; /* Defines variable a */
3 While(C1) {
4 if (C2)
5 if(C4) B4; /*Uses variable a */
6 else B5;
7 else if (C3) B2;
8 else B3; }
9 B6 }
Data Flow-Based Testing
72
Data Flow-Based Testing
[a,1,5]: a DU chain.
Assume:
DEF(X) = {B1, B2, B3, B4, B5}
USED(X) = {B2, B3, B4, B5, B6}
There are 25 DU chains.
However only 5 paths are needed to cover
these chains.
73
Mutation Testing
The software is first tested:
using an initial testing method based on
white-box strategies we already discussed.
After the initial testing is complete,
mutation testing is taken up.
The idea behind mutation testing:
make a few arbitrary small changes to a
program at a time.
74
Mutation Testing
Each time the program is
changed,
it is called a mutated program
the change is called a mutant.
75
Mutation Testing
A mutated program:
tested against the full test suite of the
program.
If there exists at least one test case in the
test suite for which:
a mutant gives an incorrect result,
then the mutant is said to be dead.
76
Mutation Testing
If a mutant remains alive:
even after all test cases have been exhausted,
the test suite is enhanced to kill the mutant.
The process of generation and killing of mutants:
can be automated by predefining a set of
primitive changes that can be applied to the
program.
77
Mutation Testing
The primitive changes can be:
altering an arithmetic operator,
changing the value of a constant,
changing a data type, etc.
78
Mutation Testing
A major disadvantage of
mutation testing:
computationally very expensive,
a large number of possible mutants
can be generated.
79
Cause and Effect Graphs
Testing would be a lot easier:
if we could automatically generate
test cases from requirements.
Work done at IBM:
Can requirements specifications be
systematically used to design
functional test cases?
80
Cause and Effect Graphs
Examine the requirements:
restate them as logical relation between
inputs and outputs.
The result is a Boolean graph
representing the relationships
called a cause-effect graph.
81
Cause and Effect Graphs
Convert the graph to a decision
table:
each column of the decision table
corresponds to a test case for
functional testing.
82
Steps to create cause-
effect graph
Study the functional requirements.
Mark and number all causes and
effects.
Numbered causes and effects:
become nodes of the graph.
83
Steps to create cause-
effect graph
Draw causes on the LHS
Draw effects on the RHS
Draw logical relationship between
causes and effects
as edges in the graph.
Extra nodes can be added
to simplify the graph
84
Drawing Cause-Effect
Graphs
A B
If A then B
A
B
If (A and B)then C
C
85
Drawing Cause-Effect
Graphs
A
B
If (A or B)then C
C
A
B
If (not(A and B))then C
C
86
Drawing Cause-Effect
Graphs
A
B
If (not (A or B))then C
C
A B
If (not A) then B
87
Cause effect graph-
Example
A water level monitoring
system
used by an agency involved in
flood control.
Input: level(a,b)
a is the height of water in dam
in meters
b is the rainfall in the last 24
hours in cms
88
Cause effect graph-
Example
Processing
The function calculates whether the
level is safe, too high, or too low.
Output
message on screen
level=safe
level=high
invalid syntax
89
Cause effect graph-
Example
We can separate the requirements
into 5 clauses:
first five letters of the command is
“level”
command contains exactly two
parameters
separated by comma and enclosed in
parentheses
90
Cause effect graph-
Example
Parameters A and B are real numbers:
such that the water level is calculated to
be low
or safe.
The parameters A and B are real numbers:
such that the water level is calculated to
be high.
91
Cause effect graph-
Example
Command is syntactically valid
Operands are syntactically
valid.
92
Cause effect graph-
Example
Three effects
level = safe
level = high
invalid syntax
93
Cause effect graph-
Example
10 E
3
11
E
1
E
2
1
2
3
4
5
94
Cause effect graph-
Decision table
Cause 1
Cause 2
Cause 3
Cause 4
Cause 5
Effect 1
Effect 2
Effect 3
Test 1 Test 2 Test 3 Test 4 Test 5
I I I
I
I
I I
S I
X S
S
S
S
S
P P
S
I
S
A A A
A
A
P
P
P
A
A
A
A A
X
X
X
X
X
X
I
95
Cause effect graph-
Example
Put a row in the decision table for
each cause or effect:
in the example, there are five rows
for causes and three for effects.
96
Cause effect graph-
Example
The columns of the decision table
correspond to test cases.
Define the columns by examining
each effect:
list each combination of causes that can
lead to that effect.
97
Cause effect graph-
Example
We can determine the number of
columns of the decision table
by examining the lines flowing into the
effect nodes of the graph.
98
Cause effect graph-
Example
Theoretically we could have
generated 25=32 test cases.
Using cause effect graphing technique
reduces that number to 5.
99
Cause effect graph
Not practical for systems which:
include timing aspects
feedback from processes is used
for some other processes.

white box testing.ppt

  • 1.
  • 2.
    2 Organization of this Lecture Introductionto Testing. White-box testing: statement coverage path coverage branch testing condition coverage Cyclomatic complexity Summary
  • 3.
    3 White-Box Testing There existseveral popular white-box testing methodologies: Statement coverage branch coverage path coverage condition coverage mutation testing
  • 4.
    Statement Coverage In aprogramming language, a statement is nothing but the line of code or instruction for the computer to understand and act accordingly. A statement becomes an executable statement when it gets compiled and converted into the object code and performs the action when the program is in a running mode. Hence “Statement Coverage”, as the name itself suggests, it is the method of validating whether each and every line of the code is executed at least once.
  • 5.
     INPUT A& B  C = A + B  IF C>100  PRINT “ITS DONE”  For Statement Coverage – we would only need one test case to check all the lines of the code.  That means:  If I consider TestCase_01 to be (A=40 and B=70), then all the lines of code will be executed.
  • 6.
     Now thequestion arises:  Is that sufficient?  What if I consider my Test case as A=33 and B=45?  Because Statement coverage will only cover the true side, for the pseudo code, only one test case would NOT be sufficient to test it. As a tester, we have to consider the negative cases as well.  Hence for maximum coverage, we need to consider “Branch Coverage”, which will evaluate the “FALSE” conditions.
  • 7.
    Branch Coverage “Branch” ina programming language is like the “IF statements”. An IF statement has two branches: True and False. So in Branch coverage (also called Decision coverage), we validate whether each branch is executed at least once.
  • 8.
    In case ofan “IF statement”, there will be two test conditions: One to validate the true branch and, Other to validate the false branch. Hence, in theory, Branch Coverage is a testing method which is when executed ensures that each and every branch from each decision point is executed.
  • 9.
    INPUT A &B C = A + B IF C>100 PRINT “ITS DONE” ELSE PRINT “ITS PENDING” Since Statement coverage is not sufficient to test the entire pseudo code, we would require Branch coverage to ensure maximum coverage.
  • 10.
    So for Branchcoverage, we would require two test cases to complete the testing of this pseudo code. TestCase_01: A=33, B=45 TestCase_02: A=25, B=30 With this, we can see that each and every line of the code is executed at least once.
  • 11.
     Here arethe Conclusions that are derived so far:  Branch Coverage ensures more coverage than Statement coverage.  Branch coverage is more powerful than Statement coverage.  100% Branch coverage itself means 100% statement coverage.  But 100 % statement coverage does not guarantee 100% branch coverage.
  • 12.
    Path Coverage Path coveragetests all the paths of the program. This is a comprehensive technique which ensures that all the paths of the program are traversed at least once. Path Coverage is even more powerful than Branch coverage. This technique is useful for testing the complex programs.
  • 13.
     Path coverageis used to test the complex code snippets, which basically involve loop statements or combination of loops and decision statements.
  • 14.
     Consider thispseudocode:  INPUT A & B  C = A + B  IF C>100  PRINT “ITS DONE”  END IF  IF A>50  PRINT “ITS PENDING”  END IF
  • 15.
     there are2 decision statements, so for each decision statement, we would need two branches to test. One for true and the other for the false condition. So for 2 decision statements, we would require 2 test cases to test the true side and 2 test cases to test the false side, which makes a total of 4 test cases.
  • 16.
    In order tohave the full coverage, we would need following test cases: TestCase_01: A=50, B=60 TestCase_02: A=55, B=40 TestCase_03: A=40, B=65 TestCase_04: A=30, B=30
  • 17.
    17 Statement Coverage Statement coveragemethodology: design test cases so that every statement in a program is executed at least once.
  • 18.
    18 Statement Coverage The principalidea: unless a statement is executed, we have no way of knowing if an error exists in that statement.
  • 19.
    19 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; }
  • 20.
    20 Branch Coverage Test casesare designed such that: different branch conditions given true and false values in turn.
  • 21.
    21 Branch Coverage Branch testingguarantees statement coverage: a stronger testing compared to the statement coverage-based testing.
  • 22.
    22 Example int f1(int x,inty){ 1 while (x != y){ 2 if (x>y) then 3 x=x-y; 4 else y=y-x; 5 } 6 return x; }
  • 23.
    23 Example Test cases forbranch coverage can be: {(x=3,y=3),(x=3,y=2), (x=4,y=3), (x=3,y=4)}
  • 24.
    24 Condition Coverage Test casesare designed such that: each component of a composite conditional expression given both true and false values.
  • 25.
    25 Example Consider the conditionalexpression ((c1.and.c2).or.c3): Each of c1, c2, and c3 are exercised at least once, i.e. given true and false values.
  • 26.
    26 Condition testing stronger testingthan branch testing: Branch testing stronger than statement coverage testing.
  • 27.
    27 Condition coverage Consider aboolean expression having n components: for condition coverage we require 2n test cases.
  • 28.
    28 Path Coverage Design testcases such that: all linearly independent paths in the program are executed at least once.
  • 29.
    29 Linearly independent paths Definedin terms of control flow graph (CFG) of a program.
  • 30.
    30 Path coverage-based testing To understandthe path coverage- based testing: we need to learn how to draw control flow graph of a program.
  • 31.
    31 Control flow graph(CFG) A control flow graph (CFG) describes: the sequence in which different instructions of a program get executed. the way control flows through the program.
  • 32.
    32 How to drawControl flow graph? Number all the statements of a program. Numbered statements: represent nodes of the control flow graph.
  • 33.
    33 Example int f1(int x,inty){ 1 while (x != y){ 2 if (x>y) then 3 x=x-y; 4 else y=y-x; 5 } 6 return x; }
  • 34.
  • 35.
    35 How to drawControl flow graph? Sequence: 1 a=5; 2 b=a*b-1; 1 2
  • 36.
    36 How to drawControl flow graph? Selection: 1 if(a>b) then 2 c=3; 3 else c=5; 4 c=c*c; 1 2 3 4
  • 37.
    37 How to drawControl flow graph? Iteration: 1 while(a>b){ 2 b=b*a; 3 b=b-1;} 4 c=b+d; 1 2 3 4
  • 38.
    38 Path A path througha 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.
  • 39.
    39 Independent path Any paththrough the program: introducing at least one new node: that is not included in any other independent paths.
  • 40.
    40 Independent path It isstraight forward: to identify linearly independent paths of simple programs. For complicated programs: it is not so easy to determine the number of independent paths.
  • 41.
    41 McCabe's cyclomatic metric An upperbound: 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.
  • 42.
    42 McCabe's cyclomatic metric Given acontrol flow graph G, cyclomatic complexity V(G):  V(G)= E-N+2 N is the number of nodes in G E is the number of edges in G
  • 43.
  • 44.
  • 45.
    45 Cyclomatic complexity Another wayof computing cyclomatic complexity: inspect control flow graph determine number of bounded areas in the graph V(G) = Total number of bounded areas + 1
  • 46.
    46 Bounded area Any regionenclosed by a nodes and edge sequence.
  • 47.
  • 48.
    48 Example From a visualexamination of the CFG: the number of bounded areas is 2. cyclomatic complexity = 2+1=3.
  • 49.
    49 Cyclomatic complexity McCabe's metricprovides: a quantitative measure of testing difficulty and the ultimate reliability Intuitively, number of bounded areas increases with the number of decision nodes and loops.
  • 50.
    50 Cyclomatic complexity The firstmethod of computing V(G) is amenable to automation: you can write a program which determines the number of nodes and edges of a graph applies the formula to find V(G).
  • 51.
    51 Cyclomatic complexity The cyclomaticcomplexity of a program provides: a lower bound on the number of test cases to be designed to guarantee coverage of all linearly independent paths.
  • 52.
    52 Cyclomatic complexity Defines thenumber of independent paths in a program. Provides a lower bound: for the number of test cases for path coverage.
  • 53.
    53 Cyclomatic complexity Knowing thenumber of test cases required: does not make it any easier to derive the test cases, only gives an indication of the minimum number of test cases required.
  • 54.
    54 Path testing The testerproposes: an initial set of test data using his experience and judgement.
  • 55.
    55 Path testing A dynamicprogram analyzer is used: to indicate which parts of the program have been tested the output of the dynamic analysis used to guide the tester in selecting additional test cases.
  • 56.
    56 Derivation of TestCases Let us discuss the steps: to derive path coverage-based test cases of a program.
  • 57.
    57 Derivation of TestCases Draw control flow graph. Determine V(G). Determine the set of linearly independent paths. Prepare test cases: to force execution along each path.
  • 58.
    58 Example int f1(int x,inty){ 1 while (x != y){ 2 if (x>y) then 3 x=x-y; 4 else y=y-x; 5 } 6 return x; }
  • 59.
  • 60.
    60 Derivation of TestCases 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)
  • 61.
    61 An interesting application ofcyclomatic complexity Relationship exists between: McCabe's metric the number of errors existing in the code, the time required to find and correct the errors.
  • 62.
    62 Cyclomatic complexity Cyclomatic complexityof a program: also indicates the psychological complexity of a program. difficulty level of understanding the program.
  • 63.
    63 Cyclomatic complexity From maintenanceperspective, limit cyclomatic complexity of modules to some reasonable value. Good software development organizations: restrict cyclomatic complexity of functions to a maximum of ten or so.
  • 64.
    64 Data Flow-Based Testing Selectstest paths of a program: according to the locations of definitions and uses of different variables in a program.
  • 65.
    65 Data Flow-Based Testing Fora statement numbered S, DEF(S) = {X/statement S contains a definition of X} USES(S)= {X/statement S contains a use of X} Example: 1: a=b; DEF(1)={a}, USES(1)={b}. Example: 2: a=a+b; DEF(1)={a}, USES(1)={a,b}.
  • 66.
    66 Data Flow-Based Testing Avariable X is said to be live at statement S1, if X is defined at a statement S: there exists a path from S to S1 not containing any definition of X.
  • 67.
    67 DU Chain Example 1X(){ 2 a=5; /* Defines variable a */ 3 While(C1) { 4 if (C2) 5 b=a*a; /*Uses variable a */ 6 a=a-1; /* Defines variable a */ 7 } 8 print(a); } /*Uses variable a */
  • 68.
    68 Definition-use chain (DU chain) [X,S,S1], Sand S1 are statement numbers, X in DEF(S) X in USES(S1), and the definition of X in the statement S is live at statement S1.
  • 69.
    69 Data Flow-Based Testing Onesimple data flow testing strategy: every DU chain in a program be covered at least once.
  • 70.
    70 Data Flow-Based Testing Dataflow testing strategies: useful for selecting test paths of a program containing nested if and loop statements
  • 71.
    71 1 X(){ 2 B1;/* Defines variable a */ 3 While(C1) { 4 if (C2) 5 if(C4) B4; /*Uses variable a */ 6 else B5; 7 else if (C3) B2; 8 else B3; } 9 B6 } Data Flow-Based Testing
  • 72.
    72 Data Flow-Based Testing [a,1,5]:a DU chain. Assume: DEF(X) = {B1, B2, B3, B4, B5} USED(X) = {B2, B3, B4, B5, B6} There are 25 DU chains. However only 5 paths are needed to cover these chains.
  • 73.
    73 Mutation Testing The softwareis first tested: using an initial testing method based on white-box strategies we already discussed. After the initial testing is complete, mutation testing is taken up. The idea behind mutation testing: make a few arbitrary small changes to a program at a time.
  • 74.
    74 Mutation Testing Each timethe program is changed, it is called a mutated program the change is called a mutant.
  • 75.
    75 Mutation Testing A mutatedprogram: tested against the full test suite of the program. If there exists at least one test case in the test suite for which: a mutant gives an incorrect result, then the mutant is said to be dead.
  • 76.
    76 Mutation Testing If amutant remains alive: even after all test cases have been exhausted, the test suite is enhanced to kill the mutant. The process of generation and killing of mutants: can be automated by predefining a set of primitive changes that can be applied to the program.
  • 77.
    77 Mutation Testing The primitivechanges can be: altering an arithmetic operator, changing the value of a constant, changing a data type, etc.
  • 78.
    78 Mutation Testing A majordisadvantage of mutation testing: computationally very expensive, a large number of possible mutants can be generated.
  • 79.
    79 Cause and EffectGraphs Testing would be a lot easier: if we could automatically generate test cases from requirements. Work done at IBM: Can requirements specifications be systematically used to design functional test cases?
  • 80.
    80 Cause and EffectGraphs Examine the requirements: restate them as logical relation between inputs and outputs. The result is a Boolean graph representing the relationships called a cause-effect graph.
  • 81.
    81 Cause and EffectGraphs Convert the graph to a decision table: each column of the decision table corresponds to a test case for functional testing.
  • 82.
    82 Steps to createcause- effect graph Study the functional requirements. Mark and number all causes and effects. Numbered causes and effects: become nodes of the graph.
  • 83.
    83 Steps to createcause- effect graph Draw causes on the LHS Draw effects on the RHS Draw logical relationship between causes and effects as edges in the graph. Extra nodes can be added to simplify the graph
  • 84.
    84 Drawing Cause-Effect Graphs A B IfA then B A B If (A and B)then C C
  • 85.
    85 Drawing Cause-Effect Graphs A B If (Aor B)then C C A B If (not(A and B))then C C
  • 86.
    86 Drawing Cause-Effect Graphs A B If (not(A or B))then C C A B If (not A) then B
  • 87.
    87 Cause effect graph- Example Awater level monitoring system used by an agency involved in flood control. Input: level(a,b) a is the height of water in dam in meters b is the rainfall in the last 24 hours in cms
  • 88.
    88 Cause effect graph- Example Processing Thefunction calculates whether the level is safe, too high, or too low. Output message on screen level=safe level=high invalid syntax
  • 89.
    89 Cause effect graph- Example Wecan separate the requirements into 5 clauses: first five letters of the command is “level” command contains exactly two parameters separated by comma and enclosed in parentheses
  • 90.
    90 Cause effect graph- Example ParametersA and B are real numbers: such that the water level is calculated to be low or safe. The parameters A and B are real numbers: such that the water level is calculated to be high.
  • 91.
    91 Cause effect graph- Example Commandis syntactically valid Operands are syntactically valid.
  • 92.
    92 Cause effect graph- Example Threeeffects level = safe level = high invalid syntax
  • 93.
    93 Cause effect graph- Example 10E 3 11 E 1 E 2 1 2 3 4 5
  • 94.
    94 Cause effect graph- Decisiontable Cause 1 Cause 2 Cause 3 Cause 4 Cause 5 Effect 1 Effect 2 Effect 3 Test 1 Test 2 Test 3 Test 4 Test 5 I I I I I I I S I X S S S S S P P S I S A A A A A P P P A A A A A X X X X X X I
  • 95.
    95 Cause effect graph- Example Puta row in the decision table for each cause or effect: in the example, there are five rows for causes and three for effects.
  • 96.
    96 Cause effect graph- Example Thecolumns of the decision table correspond to test cases. Define the columns by examining each effect: list each combination of causes that can lead to that effect.
  • 97.
    97 Cause effect graph- Example Wecan determine the number of columns of the decision table by examining the lines flowing into the effect nodes of the graph.
  • 98.
    98 Cause effect graph- Example Theoreticallywe could have generated 25=32 test cases. Using cause effect graphing technique reduces that number to 5.
  • 99.
    99 Cause effect graph Notpractical for systems which: include timing aspects feedback from processes is used for some other processes.