0% found this document useful (0 votes)
34 views65 pages

Software Testing White Box Tes

Uploaded by

Tridip Barua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views65 pages

Software Testing White Box Tes

Uploaded by

Tridip Barua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 65

White-box

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>1) && (B==0))


X=X/A;

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

Statement coverage is usually weakest standard.


Branch Coverage
• Branch Coverage requires
that each branch will have
been traversed, and that
every program entry point
will have been taken, at
least once.
• Also known as Edge Coverage
or decision coverage.
2. Branch Coverage

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?

Test case A B X path

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

 Conditions in previous example :

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

A B X path Covered Covered


branches conditions

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

 The two test cases covered the four conditions,


but they didn’t cover the branches c and d. They
cannot satisfy branch coverage.
Branch/Condition
Coverage
• Branch/Condition Coverage
requires that both Branch AND
Condition Coverage will have
been achieved.
• Therefore, Branch/Condition
Coverage subsumes both Branch
Coverage and Condition
Coverage.
Test cases satisfying branch/condition coverage

A B X path Covered Covered


branches conditions
Case1 2 0 3 ace ce T1 T2 T3 T4

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

• Compound Condition Coverage


requires that all combinations of
condition values at every branch
statement will have been covered,
and that every entry point will have
been taken, at least once.
• Also know as Multiple Condition
Coverage.
• Subsumes Branch/Condition Coverage,
regardless of the order in which
conditions are evaluated.
 Combinations of condition values
① A > 1, B == 0 T1 T2 decision one is true
② A > 1, B≠0 T1 F2
③ A≤1, B == 0 F1 T2 decision one is false
④ A≤1, B≠0 F1 F2
⑤ A == 2 , X > 1 T3 T4
⑥ A == 2 , X≤1 T3 F4 decision two is true
⑦ A≠2 , X > 1 F3 T4
⑧ A≠2 , X≤1 F3 F4 decision one is false
Test cases satisfying compound condition coverage

A B X path combinati Covered


on conditions
Case1 2 0 3 ace ①⑤ T1 T2 T3 T4
Case8 2 1 1 abe ②⑥ T1 F2 T3 F4
Case9 1 0 3 abe ③⑦ F1 T2 F3 T4

Case10 1 1 1 abd ④⑧ F1 F2 F3 F4
Path Coverage

• Path Coverage requires that all


program paths will have been
traversed at least once.
• Often described as the “strongest” form of
logic coverage?
• Path Coverage is usually impossible when
loops are present.
• Test cases satisfying compound condition
coverage cannot cover path acd.
Test cases satisfying path coverage

Test case A B X path


Case1 2 0 3 ace
Case7 1 0 1 abd
Case8 2 1 1 abe
Case11 3 0 1 acd
exercise
public void work ( int x, int y, int z ) {
1 int k = 0, j = 0;
2 if ( (x>3) && (z<10) ){
3 k = x * y - 1;
4 j = sqrt ( k );
5 }
6 if( ( x==4 ) || ( y>5 ) )
7 j = x * y + 10;
8 j = j % 3;
}
Basic path testing
• We seldom use the previous logic coverage to
design test cases. But these coverage standards
are usually used to evaluate the quality of
software testing. The higher the coverage, the
better the quality.
• Path Coverage is usually impossible when loops
are present. So tester often uses basic path
testing method.
• In basic path testing, loop will be simplified. i.e.
we often consider two cases: one is that loop
never happen, the other is loop once.
Control Flow Graph:
Introduction
• An abstract representation of a structured
program/function/method.
• Consists of two major components:
– Node:
• Represents a stretch of sequential code statements with no
branches.
– Directed Edge (also called arc):
• Represents a branch, alternative path in execution.
• Path:
– A collection of Nodes linked with Directed Edges.
Simple Examples
Statement1; Can be
Statement2; represented as
Statement3; one node as there 1
Statement4; is no branch.

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

Question: Why is there a node 4 in both CFGs?


Notation Guide for CFG
• A CFG should have:
– 1 entry arc (known as a directed edge, too).
– 1 exit arc.
• All nodes should have:
– At least 1 entry arc.
– At least 1 exit arc.
• A Logical Node that does not represent any actual
statements can be added as a joining point for several
incoming edges.
– Represents a logical closure.
– Example:
• Node 4 in the if-then-else example from previous
slide.
Example: Minimum Element

min = A[0]; 1
1
I = 1;
2
while (I < N) { 2 F T

if (A[I] < min) 3 3


min = A[I]; 4 F T

I = I + 1; 5 5 4
}
print min 6 6

CFG

Note: The CFG is INCOMPLETE. Try to complete it


Number of Paths through CFG
• Given a program, how do we exercise all
statements and branches at least once?
• Translating the program into a CFG, an
equivalent question is:
– Given a CFG, how do we cover all arcs and
nodes at least once?
• Since a path is a trail of nodes linked by arcs,
this is similar to ask:
– Given a CFG, what is the set of paths that can
cover all arcs and nodes?
Example
T
3
1 1 2
F
4
CFG CFG

• Only one path is needed:


 Two paths are needed:
– [1]
 [1–2–4]
 [1–2–3–4]

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

1 • Region: Enclosed area in the


CFG.
– Do not forget the outermost
2
F T region.
3 • In this example:
F T – 3 Regions (see the circles
5 4 with different colors).
– Cyclomatic Complexity = 3
6 • Alternative way in next slide.

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

• 1-2-3-5 can be the first independent path; 1-2-4-5 is another;


1-2-3-5-2-4-5 is one more.
• There are only these 3 independent paths. The basis path
set is then having 3 paths.
• Alternatively, if we had identified 1-2-3-5-2-4-5 as the first
independent path, there would be no more independent
paths.
• The number of independent paths therefore can vary
according to the order we identify them.
11/26/24 47
Basic Path Testing: Step 3
• Cyclomatic complexity = 3.
1 • Need at most 3
independent paths to cover
2 the CFG.
F T
• In this example:
3
F T – [1–2–6]
5 4 – [1–2–3–5–2–6]
– [ 1 – 2 – 3 – 4 – 5 – 2 – 6]
6

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy