0% found this document useful (0 votes)
22 views36 pages

Software Testing-2

The document discusses white-box testing, emphasizing its reliance on knowledge of a program's internal structure and detailing various strategies for designing test cases, including statement coverage, branch coverage, condition coverage, and path coverage. It explains the importance of executing all statements and branches to identify potential errors and introduces cyclomatic complexity as a metric for determining the number of linearly independent paths in a program. Additionally, methods for calculating cyclomatic complexity are provided, along with examples to illustrate these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views36 pages

Software Testing-2

The document discusses white-box testing, emphasizing its reliance on knowledge of a program's internal structure and detailing various strategies for designing test cases, including statement coverage, branch coverage, condition coverage, and path coverage. It explains the importance of executing all statements and branches to identify potential errors and introduces cyclomatic complexity as a metric for determining the number of linearly independent paths in a program. Additionally, methods for calculating cyclomatic complexity are provided, along with examples to illustrate these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

White-Box Testing

White-Box test cases requires a thorough knowledge of


the internal structure of software, therefore it is also
called structural testing.

• The following are the main approaches to designing


white box test cases.
1. Statement coverage
2. Branch coverage
3. Condition coverage
4. Path coverage
Statement coverage
The statement coverage strategy aims to design test cases
so that every statement in a program is executed at least
once.
WHY??
• unless a statement is executed, it is very hard to determine
if an error exists in that statement
• Unless a statement is executed, it is very difficult to
observe whether it causes failure due to some illegal
memory access, wrong result computation, etc.
However,
executing some statement once and observing that it
behaves properly for that input value is no guarantee that it
will behave correctly for all input values
Designing of test cases using the
statement coverage strategy

Example: Consider the following program:

If(x>50 && y<10)


z=x+y;
Printf(“%d”, z);
x=x+1;
x=60 and y=5 are sufficient to execute all the statement
Test case: (x=60, y=5)
Designing of test cases using the statement
coverage strategy

Example: Consider the following program:


int compute_gcd( int x, int y) {
1 while (x! = y){
2 if (x>y)
3 x= x – y;
4 else y= y – x;
5}
6 return x;
}
By choosing the test set {(x=4, y=3), (x=3, y=4)}, we can
exercise the program such that all statements are executed at
least once.
Branch coverage
In the branch coverage-based testing strategy, test cases are
designed to make each branch condition to assume true and
false values in turn. Branch testing is also known as edge
testing as in this testing scheme, each edge of a program’s
control flow graph is traversed at least once.
It is obvious that branch testing guarantees statement coverage
and thus is a stronger testing strategy compared to the
statement coverage-based testing.

Design test cases for the same program using branch


coverage-based testing strategy…
Test Suite: {(x=3, y=3}, {x=3, y=2}, {x=4, y=3},
{x=3, y=4)}.
Condition coverage
In this structural testing, test cases are designed to make each
component of a composite conditional expression to assume
both true and false values.
Example: Consider the following program:
IF((x<20) AND (y>50))
total=total + x;
ELSE
total=total + y;
X=25, y=20 F,F
X=30, y=60 F,T
X=10, y=10 T,F
X=10, y=55 T,T
For example, in the conditional expression--
((a AND b) OR c)
the components a, b and c are each made to
assume both true and false values.
a b c
0 0 0 F
0 0 1 T
0 1 0 F
0 1 1 T
1 0 0 F
1 0 1 T
1 1 0 T
1 1 1 T
Limitation…
For a composite conditional expression of n components, for
condition coverage, 2ⁿ test cases are required. Thus, for
condition coverage, the number of test cases increases
exponentially with the number of component conditions.
Therefore, a condition coverage-based testing technique is
practical only if n is small.
Path coverage
The path coverage-based testing strategy requires us to design
test cases such that all linearly independent paths in the
program are executed at least once.

A linearly independent path can be defined in terms of the


control flow graph (CFG) of a program.
Control Flow Graph (CFG)
A control flow graph describes the sequence in which the
different instructions of a program get executed.
In other words, a control flow graph describes how the control
flows through the program.
How to draw CFG…
1. all the statements of a program must be numbered first. The
different numbered statements serve as nodes of the control
flow graph
2. An edge from one node to another node exists if the
execution of the statement representing the first node can
result in the transfer of control to the other node.
The CFG for any program can be easily drawn by knowing
how to represent the sequence, selection, and iteration type
of statements in the CFG. After all, a program is made up from
these types of statements.
Sequence: Selection: Iteration:
1 a=10; 1 if(a>b) 1 while(a>b){
2 b=a*2-1; 2 c=3; 2 b=b-1; 1
3 else c=5; 3 b=b*a; }
4 c=c*c; 4 c= a+b; 2
1
1 3

2 3
2
4
4
CFG of the program…
Example: Consider the following program:
int compute_gcd( int x, int y) {
1 while (x! = y){
2 if (x>y)
3 x= x – y;
4 else y= y – x;
5 }
6 return x;
}
Fig 1: CFG of the GCD algorithm
1

3 4

6
What is 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. Writing test cases to cover all the paths of a typical
program is impractical. For this reason, the path-coverage
testing does not require coverage of all paths but only
coverage of linearly independent paths.
What is 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, then the
path is also linearly independent.
???
Control flow graph: In order to understand the path coverage-
based testing strategy, it is very much necessary to understand
the control flow graph (CFG) of a program.

Linearly independent path: The path-coverage testing does not


require coverage of all paths but only coverage of linearly
independent paths.

But For more complicated programs it is not easy to determine


the number of independent paths of the program.
Cyclomatic complexity
McCabe’s cyclomatic complexity defines an upper bound for
the number of linearly independent paths through a program.
Thus, the McCabe’s cyclomatic complexity metric provides a
practical way of determining the maximum number of linearly
independent paths in a program. Though the McCabe’s metric
does not directly identify the linearly independent paths, but it
informs approximately how many paths to look for.
• McCabe’s cyclomatic complexity is very simple to compute.
• There are three different ways to compute the cyclomatic
complexity.
Method 1:
Given a control flow graph G of a program, the cyclomatic
complexity V(G) can be computed as:
V(G) = E – N + 2
where,
N is the number of nodes of the CFG
E is the number of edges in the CFG
For the CFG of example shown in fig.1,
E=7 and N=6.
Therefore, the cyclomatic complexity=7-6+2 = 3.
Method 2:
An alternative way of computing the cyclomatic complexity of
a program from an inspection of its control flow graph is as
follows:
V(G) = Total number of bounded areas + 1
In the program’s control flow graph G, any region enclosed by
nodes and edges can be called as a bounded area. This is an
easy way to determine the McCabe’s cyclomatic complexity.
Limitation of method 2:
what if the graph G is not planar, i.e. however you draw the
graph, two or more edges intersect? Actually, it can be shown
that structured programs always yield planar graphs. But,
presence of GOTO’s can easily add intersecting edges.
Therefore, for non-structured programs, this way of computing
the McCabe’s cyclomatic complexity cannot be used.
For the CFG of example shown in fig.1,
from a visual examination of the CFG the number of bounded
areas is 2. Therefore the cyclomatic complexity, computing
with this method is also 2+1 = 3. This method provides a very
easy way of computing the cyclomatic complexity of CFGs,
just from a visual examination of the CFG.
Method 3:
The cyclomatic complexity of a program can also be easily
computed by computing the number of decision statements of
the program. If N is the number of decision statement of a
program, then the McCabe’s metric is equal to N+1.
For the CFG of example shown in fig.1,
The number of decision statement is 2
( statement 1 and statement 2).

Therefore the cyclomatic complexity, computing with this


method is also 2+1 = 3.
Example 1
Draw the control flow graph for the following function named find-
maximum. From the control flow graph, determines its Cyclomatic
complexity.
int find-maximum(int i, int j, int k) {
int max;
if(i>j) then
if(i>k) then
max = i;
else max = k;
else if(j>k) max = j;
else max = k;
return(max);
}
Example 2
Consider the following C function named bin search:
/* num is the number the function searches in a presorted integer array arr */

int bin_search(int num)


{
int min, max;
min = 0;
max = 100;
while(min!=max){
if (arr[(min+max)/2]>num)
max = (min+max)/2;
else if(arr[(min+max)/2]<num)
min = (min+max)/2;
else return((min+max)/2); }
return(-1);
}
Determine the cyclomatic complexity of the above problem.

You might also like