Professional Documents
Culture Documents
1
White-box Testing
White Box Testing is a testing technique which evaluates the code and
the internal structure of a program.
1.Read X
2.Read Y Control flow graph of code
structure
3.IF X+Y > 100 THEN
4.Print "Large"
5.ENDIF
6.If X + Y<100 THEN
7.Print "Small"
8.ENDIF
Path 1 - A1-B2-C4-D6-E8
Path 2 - A1-B3-5-D7
Branch Coverage (BC) = Number of paths =2
Case Covered Branches Path Branch coverage
Yes 1, 2, 4, 5, 6, 8 A1-B2-C4-D6-E8 2
No 3,7 A1-B3-5-D7 4
Path Coverage
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.
Design test cases such that:
– all linearly independent paths in the program are executed at least once.
5
Control flow graph (CFG)
A control flow graph (CFG) describes:
1. The sequence in which different instructions of a program get executed.
2. How the control flows through the program.
How to draw Control flow graph?
6
How to draw Control flow graph?
Sequence: 1
– 1 a=5;
– 2 b=a*b-1;
2
Selection:
– 1 if(a>b) then 1
– 2 c=3; 2 3
– 3 else c=5;
– 4 c=c*c; 4
1
Iteration:
– 1 while(a>b){
– 2 b=b*a; 2
– 3 b=b-1;}
– 4 c=b+d;
3
4
Example
int f1(int x,int y){
1
1 while (x != y){
2 if (x>y) then 2
3 x=x-y; 3 4
4 else y=y-x;
5
5 }
6 return x; } 6
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.
8
McCabe's Cyclomatic complexity metric
Is a software metric used to indicate the complexity of a program. It is a
quantitative measure of the number of linearly independent paths through a
program's source code.
It is computed using the control flow graph of the program: the nodes of the
graph correspond to indivisible groups of commands of a program.
To test each linearly independent path through the program; the number of
test cases will equal the cyclomatic complexity of the program.
10
Cyclomatic complexity
McCabe's metric provides: quantitative measure of testing difficulty
11
Path testing
The tester proposes:
– an initial set of test data using his experience and judgement.
Derivation of Test Cases
Let us discuss the steps: to derive path coverage test cases of a program.
Draw control flow graph.
Determine V(G).
Determine the set of linearly independent paths.
Prepare test cases: to force execution along each path.
12
Example
int f1(int x,int y){ 1
1 while (x != y){ 2
2 if (x>y) then
3 4
3 x=x-y;
4 else y=y-x; 5
5 } 6
6 return x; }
14
Basis Path Testing
First, we compute the cyclomatic complexity:
Number of edges – number of nodes + 2
9-7 + 2 = 4 or number of simple decisions + 1 or
Number of enclosed areas + 1 In this case, V(G) = 4
Next, we derive the independent paths:
Since V(G) = 4, there are four paths
Path 1: 1,2,3,6,7,8
Path 2: 1,2,3,5,7,8
Path 3: 1,2,4,7,8
Path 4: 1,2,4,7,2,4,...7,8
Finally, we derive test cases to test these paths.
15
Simple
loop
Nested
Loops
Concatenated
Loops Unstructured
Loops
Loop Testing
16
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 m < n
5. (n-1), n, and (n+1) passes through the loop where n is the maximum
number of allowable passes
Nested Loops
1. Start at the innermost loop. Set all outer loops to their minimum iteration
parameter values.
2. Test the min+1, typical, max-1 and max for the innermost loop, while
holding the outer loops at their minimum values.
3. Move out one loop and set it up as in step 2, holding all
4. other loops at typical values. Continue this step until
5. the outermost loop has been tested.
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. 17
White-box Testing Example
FindMean(float Mean, FILE ScoreFile)
{ SumOfScores = 0.0; NumberOfScores = 0; Mean = 0;
Read(ScoreFile, Score);
while (! EOF(ScoreFile) {
if ( Score > 0.0 ) {
SumOfScores = SumOfScores + Score; NumberOfScores++;
}
Read(ScoreFile, Score);
}
/* Compute the mean and print the result */
if (NumberOfScores > 0 ) {
Mean = SumOfScores/NumberOfScores;
printf("The mean score is %f \n", Mean);
} else
}
printf("No scores found in file\n");
}
18
Determining the Paths
19
Constructing the Flow Graph
Finding the Test Cases
20
Structural Coverage Criteria: Control Flow
Structural Testing: Examples
Path testing
Visit-each-loop path testing
Branch testing
Statement testing 21
Infeasible paths
The simple code can have four total test paths: TP1: 1,2,4,6,8,10, TP2:
1,2,4,7,9,10, TP3: 1,3,5,6,8,10, TP4: 1,3,5,7,9,10. According to the two
conditional statements and due to the contradicted logic of the two
conditions, we can easily notice that TP1 can never be executed and
hence will be always infeasible paths.
22
Summary
Exhaustive testing of non-trivial systems is impractical:
– we need to design an optimal set of test cases
• should expose as many errors as possible.
If we select test cases randomly:
– many selected test cases don’t add to the significance of the test set.
There are mainly two approaches to testing:
– black-box testing and
– white-box testing.
White box testing:
– requires knowledge about internals of the software.
– Design and code is required.
A stronger testing strategy:
– provides more number of significant test cases than a weaker one.
– Condition coverage is strongest among strategies we discussed.
Designing test cases for black box testing:
– does not require any knowledge of how the functions have been designed
and implemented.
– Test cases can be designed by examining only SRS document.
23
References