Software Testing White Box Tes
Software Testing White Box Tes
Testing
LIU Weihong
School of computer science and technology
Anhui university of technology
56381319@qq.com
White-box testing
Analyz Test
Source e cases
progra
m
Program
under
Coverage
test
analysis
Executio
n route
White-box testing
Logic coverage
Basic path testing
Logic coverage
Statement coverage
Decision coverage ( branch
coverage )
Condition coverage
Branch/Condition coverage
Compound Condition coverage
Path coverage
Sample program :
if ((A==2) || (X>1))
X=X+1;
program flow chart
a
F T
(A>1) AND (B==0)
c
b X=X/A
F T
(A==2) OR (X>1) e
d X=X+1
1. Statement Coverage
• Statement Coverage requires that
each statement will have been
executed at least once.
• Simplest form of logic coverage.
• Also known as Node Coverage.
• What is the minimum number of test
cases required to achieve statement
coverage for the program segment
given below?
1.Statement coverage
Test A B X path
case √
Case1
Case2
2
2
0
1
3
3
ace
abe
×
Statemen
t
Case1 : A=2, B=0, X=3
coverage
a
F T
(A>1) AND (B==0)
c
b X=X/A
F T
(A==2) OR (X>1) e
d X=X+1
Case2 : A=2, B=1, X=3
a This statement is
not covered
F T
(A>1) AND (B==0)
c
b X=X/A
F T
(A==2) OR (X>1) e
d X=X+1
Statemen
Case1 : A=2, B=0, X=3 t
coverage
a
F T
(A>1) AND (B==0)
c
b If OR
X=X/A
is written wrongly
F T
(A==2) OR (X>1) e
d If AND
X=X+1
is written wrongly
Test A B X path
case
Case1 2 0 3 ace
Case3 1 0 1 abd
Branch
Case1 : A=2, B=0, X=3 coverag
e
a
F T
(A>1) AND (B==0)
c
b X=X/A
F T
(A==2) OR (X>1) e
d X=X+1
Branch
coverag
Case3 : A=1, B=0, X=1
e
a
F T
(A>1) AND (B==0)
c
b X=X/A
F T
(A==2) OR (X>1) e
d X=X+1
Branch
coverage
Sometimes the wrong internal condition cannot be checked if only the
branch coverage is satisfied
a
F T
(A>1) AND (B==0)
c
b X=X/A
F T
(A==2) OR (X>1) e
d X=X+1
If X<1 is written
wrongly
question :
Except ace and abd, are there other
paths satisfying the branch coverage?
Case4 3 0 1 acd
Case5 2 1 1 abe
Case4 : A=3, B=0, X=1
Case5 : A=2, B=1, X=1
a
F T
(A>1) AND (B=0)
c
b X=X/A
F T
(A==2) OR (X>1) e
d X=X+1
If X<1 is written
wrongly
Condition Coverage
• A branch predicate may have
more than one condition.
• Condition Coverage requires
that each condition will have
been True at least once and
False at least once.
3.Condition coverage
A>1 T1 F1
B==0 T2 F2
A==2 True table
T3 FalseF3
table
X>1 T4 F4
3. Condition coverage
Test cases satisfying condition coverage
Case6 2 1 1 a b e be T1 F2 T3 F4
Case7 1 0 3 a b e be F1 T2 F3 T4
Conditio
Case6 : A=2, B=1, X=1 n
Case7 : A=1, B=0, X=3 coverage
a
F T
(A>1) AND (B==0)
c
b X=X/A
F T
(A==2) OR (X>1) e
d X=X+1
Case8 1 1 1 abd bd F1 F2 F3 F4
Case1 : A=2, B=0, X=3
a
F T
(A>1) AND (B==0)
c
b X=X/A
F T
(A==2) OR (X>1) e
d X=X+1
Case8 : A=1, B=1, X=1
a
F T
(A>1) AND (B=0)
c
b X=X/A
F T
(A=2) OR (X>1) e
d X=X+1
Compound Condition Coverage
Case10 1 1 1 abd ④⑧ F1 F2 F3 F4
Path Coverage
CFG
Statement1; 1 T
Statement2; 3
1 2
if X < 10 then 2
Statement3; 3 F
4
CFG
Statement4; 4
More Examples
1 T
if X > 0 then 2
Statement1; 2 1 4
else
Statement2; 3 F 3
CFG
while X < 10 { 1
Statement1; 2 1 T
2 3 4
X++; } 3
CFG
min = A[0]; 1
1
I = 1;
2
while (I < N) { 2 F T
I = I + 1; 5 5 4
}
print min 6 6
CFG
T
2
1 4 Two paths are needed:
[1–2–4]
F 3 [1–3–4]
CFG
White Box Testing: Path Based
• A generalized technique to find out the number of
paths needed (known as cyclomatic complexity) to
cover all arcs and nodes in CFG.
• Steps:
1. Draw the CFG for the code fragment.
2. Compute the cyclomatic complexity number C, for the
CFG.
3. Find at most C paths that cover the nodes and arcs in a
CFG, also known as Basic Paths Set;
4. Design test cases to force execution along paths in the
Basic Paths Set.
Basic path Testing: Step 1
min = A[0]; 1
I = 1;
while (I < N) { 2
if (A[I] < min) F T
min = A[I];
3
I = I + 1; F T
}
5 4
print min
6
CFG
Basic Path Testing: Step 2
1
• Cyclomatic complexity =
2
F T – The number of ‘regions’ in the
3 graph; OR
F T
– The number of predicates + 1.
5 4
6
CFG
Basic Path Testing: Step 2
CFG
Basic Path Testing: Step 2
• Predicates:
1 – Nodes with multiple exit
arcs.
– Corresponds to
2
F T branch/conditional
statement in program.
3 • In this example:
F T
– Predicates = 2
5 4
• (Node 2 and 3)
6 – Cyclomatic Complexity
=2+1
CFG
=3
11/26/24 45
Basic Path Testing: Step 3
• Independent path:
– An executable or realizable path through the graph from
the start node to the end node that has not been traversed
before.
– Must move along at least one arc that has not been yet
traversed (an unvisited arc).
– The objective is to cover all statements in a program by
independent paths.
• The number of independent paths to discover <=
cyclomatic complexity number.
• Decide the Basis Path Set:
– It is the maximal set of independent paths in the flow graph.
– NOT a unique set.
11/26/24 46
Example Example
CFG
11/26/24 48
Basic Path Testing: Step 4
• Prepare a test case for each independent path.
• In this example:
– Path: [ 1 – 2 – 6 ]
• Test Case: A = { 5, …}, N = 1
• Expected Output: 5
– Path: [ 1 – 2 – 3 – 5 – 2 – 6 ]
• Test Case: A = { 5, 9, … }, N = 2
• Expected Output: 5
– Path: [ 1 – 2 – 3 – 4 – 5 – 2 – 6]
• Test Case: A = { 8, 6, … }, N = 2
• Expected Output: 6
• These tests will result a complete decision and
statement coverage of the code.
Try to verify that the test cases actually force execution along a desired path.
49
Another Example
int average (int[ ] value, int min, int max, int N) {
int i, totalValid, sum, mean;
i = totalValid = sum = 0;
while ( i < N && value[i] != -999 ) {
if (value[i] >= min && value[i] <= max){
totalValid += 1; sum += value[i];
}
i += 1;
}
if (totalValid > 0)
mean = sum / totalValid;
else
mean = -999;
return mean;
}
Step 1: Draw CFG
int average (int[ ] value, int min, int max, int N) {
int i, totalValid, sum, mean;
i = totalValid = sum = 0; 1
while ( i < N && value[i] != -999 ) {
2 4 3 5
if (value[i] >= min && value[i] <= max){
totalValid += 1; sum += value[i]; 6
}
i += 1; 7
}
if (totalValid > 0) 8
mean = sum / totalValid;9
else
mean = -999;10
return mean; 11
}
Step 1: Draw CFG
T 9
8 F
11
F
F
10
T T T T
1 2 3 4 5 6
F F
7
CFG
52
Step 2: Find Cyclomatic
Complexity
T 9
8 F
11
F
F
10
T T T T
1 2 3 4 5 6
F F
Regions = 6 7
Cyclomatic Complexity = 6
CFG
11/26/24 53
Step 2: Find Cyclomatic
Complexity
T 9
8 F
11
F
F
10
T T T T
1 2 3 4 5 6
F F
Predicates = 5
Cyclomatic Complexity
=5+1 7
=6
CFG
11/26/24 54
Step 3: Find Basic Path Set
• Find at most 6 independent paths.
• Usually, simpler path == easier to find a test case.
• However, some of the simpler paths are not possible (not realizable):
– Example: [ 1 – 2 – 8 – 9 – 11 ].
• Not Realizable (i.e., impossible in execution).
• Verify this by tracing the code.
• Basic Path Set:
– [ 1 – 2 – 8 – 10 – 11 ].
– [ 1 – 2 – 3 – 8 – 10 – 11 ].
– [ 1 – 2 – 3 – 4 – 7 – 2 – 8 – 10 – 11 ].
– [ 1 – 2 – 3 – 4 – 5 – 7 – 2 – 8 – 10 – 11 ].
– [ 1 – ( 2 – 3 – 4 – 5 – 6 – 7 ) – 2 – 8 – 9 – 11 ].
• In the last case, ( … ) represents possible repetition.
11/26/24 55
Step 4: Derive Test Cases
• Path: ... i = 0;
1
– [ 1 – 2 – 8 – 10 – 11 ]
• Test Case:
while (i < N && 2
value[i] != -999) {
– value = {…} irrelevant. ......
}
– N = 0
if (totalValid > 0) 8
– min, max irrelevant. ......
else
• Expected Output: mean = -999;10
– average = -999
return mean; 11
11/26/24 56
Step 4: Derive Test Cases
• Path: ... i = 0; 1
– [ 1 – 2 – 3 – 8 – 10 –
while (i < N && 2
11 ]
value[i] != -999) {3
• Test Case: ......
}
– value = {-999}
if (totalValid > 0) 8
– N = 1 ......
else
– min, max irrelevant 10
mean = -999;
• Expected Output: 11
return mean;
– average = -999
11/26/24 57
Step 4: Derive Test Cases
• Path:
– [ 1 – 2 – 3 – 4 – 7 – 2 – 8 – 10 – 11 ]
• Test Case:
– A single value in the value[ ] array which is smaller than min.
– value = { 25 }, N = 1, min = 30, max irrelevant.
• Expected Output:
– average = -999
• Path:
– [ 1 – 2 – 3 – 4 – 5 – 7 – 2 – 8 – 10 – 11 ]
• Test Case:
– A single value in the value[ ] array which is larger than max.
– value = { 99 }, N = 1, max = 90, min irrelevant.
• Expected Output:
– average = -999
11/26/24 58
Step 4: Derive Test Cases
• Path:
– [ 1 – 2 – 3 – 4 – 5 – 6 – 7 – 2 – 8 – 9 – 11 ]
• Test Case:
– A single valid value in the value[ ] array.
– value = { 25 }, N = 1, min = 0, max = 100
• Expected Output:
– average = 25
OR
• Path:
– [ 1 – 2 – 3 – 4 – 5 – 6 – 7 – 2 – 3 – 4 – 5 – 6 – 7 – 2 – 8 – 9 – 11 ]
• Test Case:
– Multiple valid values in the value[ ] array.
– value = { 25, 75 }, N = 2, min = 0, max = 100
• Expected Output:
– average = 50
11/26/24 59
Summary: Basic Path Testing
• A simple test that:
– Cover all statements.
– Exercise all decisions (conditions).
• The cyclomatic complexity is an upperbound of the
independent paths needed to cover the CFG.
– If more paths are needed, then either cyclomatic complexity
is wrong, or the paths chosen are incorrect.
• Although picking a complicated path that covers more
than one unvisited edge is possible all times, it is not
encouraged:
– May be hard to design the test case.
11/26/24 60
example
void Sort ( int iRecordNum, int iType )
1{
2 int x=0;
3 int y=0;
4 while ( iRecordNum-- > 0 )
5 {
6 If ( iType==0 )
7 x=y+2;
8 else
9 If ( iType==1 )
10 x=y+10;
11 else
12 x=y+20;
13 }
14 }
Steps:
Step 1: draw CFG
Step 2: compute cyclomatic complexity 4
10 ( edges ) - 8 ( nodes ) + 2 = 4
6
Step 3: basic paths set
path 1 : 4→14 7 9
path 2 : 4→6→7 →13→4 →14
path 3 : 4→6→9→10→13→4→14 10 12
path 4 : 4→6→9→12→13→4→14 14 13
Steps:
Step 4: design test cases :
Exercise : selection sort
public void select_sort ( int a[ ] ) {
int i, j, k, t, n = a.length;
for ( i = 0; i<n-1; i++ ) {
k = i;
for( j = i + 1; j < n; j++ ) {
if ( a[j] < a[k] )
k = j;
}
if ( i != k ) {
t = a[k];
a[k] = a[i];
a[i] = t;
} }}
Exercise : NextDate
public void nextday( ){ case 2:
switch( this.month){ if( this.isleap()){
case 1,3, 5, 7, 8, 10: if(this.day == 29) {
if( this.day == 31){ this.day = 1;
this.month = this.month + 1; this.month = 3;
this.day = 1; } }
else{ this.day = this.day + 1; } else{ this.day = this.day + 1; }
break; }
case 4, 6, 9,11:
else{
if( this.day == 30){
this.month = this.month + 1; if(this.day == 28){
this.day = 1; this.day = 1;
} this.month = 3;
else{ this.day = this.day + 1; } }
break; else{ this.day = this.day + 1; }
case 12: }
if( this.day == 31){ break;
this.month = 1; }
this.day = 1; }
this.year = this.year + 1;
}else{ this.day = this.day + 1; }
break;
1.