CHAPTER 3.
SOFTWARE TESTING
APPROACHES AND TECHNIQUES
White box test techniques
Basic coverage criteria
• Function coverage – Has each function (or subroutine) in the
program been called?
• Statement coverage – Has each statement in the program been
executed?
• Branch/Decision coverage – Has each branch (also called DD-path) of
each control structure (such as in if and case statements) been
executed? For example, given an if statement, have both the true
and false branches been executed? Another way of saying this is, has
every edge in the program been executed?
• Condition coverage (or predicate coverage) – Has each Boolean sub-
expression evaluated both to true and false?
• Basic-Path coverage
2
Basic coverage criteria
For example, consider the • If during this execution function 'foo' was called at least
following C function: once, then function coverage for this function is satisfied.
• Statement coverage for this function will be satisfied if it was
called e.g. as foo(1,1), as in this case, every line in the
int foo (int x, int y) function is executed including z = x;.
{ • Tests calling foo(1,1) and foo(0,1) will satisfy branch
int z = 0; coverage because, in the first case, both if conditions are
met and z = x; is executed, while in the second case, the first
if ((x > 0) && (y > 0)) condition (x>0) is not satisfied, which prevents executing z =
{ x;.
z = x; • Condition coverage can be satisfied with tests that call
} foo(1,0) and foo(0,1). These are necessary because in the
first cases, (x>0) evaluates to true, while in the second, it
return z; evaluates false. At the same time, the first case makes (y>0)
} false, while the second makes it true.
3
CFG – Control Flow Graph
• Three kinds of nodes:
• Statement nodes: represent single-entry-single-exit sequences of statements
• Predicate nodes: represent conditions for branching
• Auxiliary nodes: (optional) for easier understanding (e.g., “join points” for IF,
etc.)
• Edges: represents possible flow of control
• It is relatively easy to map standard constructs from programming
languages to elements of CFGs 1
1
1
2 3 2
2
4
4 3
4
An example for CFG
n0
if(a == 0)
if(b == 0)
cout<< ”Countless solutions”; 1
else a =0 a !=0
cout<<”No solution”;
else 2 3 x=-b/a
cout<<”x =”,-b/a; b =0 b !=0
Countless solutions No solution
4 5
ne
6
CFG – Control Flow Graph
• Path?
• CFG expression?
• G1 = n0 1 2 4 ne + n0 1 2 5 ne + n0 1 3 ne
• G1 = n0 1 (2 4 + 2 5 + 3) ne
• G1 = n0 1 (2 (4 + 5) + 3) ne
7
Practice:
Quadratic Equation Solver!
a.x2 + b.x + c = 0
Create a control flow graph and write the CFG expression!
8
Node-Coverage /Statement-Coverage Testing
• Consider the following statements
cout<<”Input value for x: ”;
cin>>x;
if(x==0) x++;
g=1/x;
Let's
- Graph the control flow!
- Determine which path and corresponding test data cover all nodes?
- So do you have any comments?
9
Node-Coverage /Statement-Coverage Testing
n0
x=0 x!=0
x++ 2 3 g=1/x
ne
10
Branch/Decision-Coverage Testing
• Consider the following statements
if(a < 2 && a==b)
x= 2- a;
else
x= a-2;
Let's
- Graph the control flow!
- Determine which path and corresponding test data cover all branches?
- So do you have any comments?
11
Branch/Decision-Coverage Testing
n0
a ≥ 2 hoặc b≠a a<2 và a=b
2 3 x=2-a
x = a-2
ne
12
Conditions-Coverage Testing
n0
1 a<2 ?
F
T
a=b?
4 2
T a=b?
F
T
F
5 x = a-2 3
x= 2- a
ne
13
Condition-Coverage Testing
Let's
- Graph the control flow!
private void GetTriangle (int a, int b, int c)
- Determine which path and
{
if ((a + b <= c) || (b + c <= a) || (a + c <= b)) corresponding test data
{ cover all conditions?
// Error
}
- So do you have any
else if ((a == b) && (b == c)) comments?
{
// Equilateral;
}
else if ((a == b) || (b == c) || (a == c))
{
// Isoceles;
}
else
{
// Scalene;
}
}
14
Condition-Coverage Testing
n0
private void GetTriangle (int a, int b, int
c)
{
if ((a + b <= c) || (b + c <= a) || (a +
1 a+b<=c
c <= b))
{
// Error
T F
}
else if ((a == b) && (b == c))
2 b+c<=a
{ T
}
// Equilateral; F
else if ((a == b) || (b == c) || (a == c))
{
T 3 a+c<=b
// Isoceles; 4
}
Error F
else
{
// Scalene;
}
}
15
Example: Condition testing
private bool IsInValidDate (int month, int day, int year)
{
if ((year == 1582 && month == 10) &&
(day >= 5 && day <= 14))
{
return true;
}
return false;
}
Example: Condition testing
no
1 year=1582
return False
T
6 2 month=10
T
ne
3 day>=5
T
5 T
4 day<=14
return True
Practice: Condition-coverage testing
• Create control flow diagram, define set of TCs
internal static decial GetCommission(int locks, int stocks, int barrels)
{
decimal sales = GetTotalSales(locks, stocks, barrels);
decimal commission = 100;
if (CommissonIsDue(locks, stocks, barrels))
{
if (sales > 1800)
{
commission += (800 * Bonus15);
commission += ((sales - 1800) * Bonus20);
}
else if (sales > 1000)
{
commission += ((sales - 1000) * Bonus15);
}
else {
commission = (sales * standardCommission);
}
}
return commission.ToString();
}
Condition-Coverage Testing
• Consider the following statements
cout<<”Nhap n: “; Let's
cin>>n; - Graph the control
cout<<”Nhap m: “; flow!
cin>>m;
s=0; - Determine which
while(n<=m){ path and
i=n; corresponding test
s=s+a[i]; data cover all
n++;
conditions?
}
cout<<1/s; - So do you have
any comments?
19
Condition-Coverage Testing
n0
a n>m
n≤m
1/s
i=n; e
b
s=s+a[i]
n++
ne
Loop Testing
private static bool IsThirtyDayMonth(int month)
{
int[] shortMonth = new int[4] { 4, 6, 9, 11 };
for (int i = 0; i < shortMonth.Length; i++)
{
if ( month == shortMonth[i])
{
return true;
}
}
return false;
}
Cyclomatic Complexity
Definition
• Cyclomatic complexity of the flow chart indicates the number of independent
paths of the program.
Formula
• 1. Number of closed regions (including the surrounding rectangle) +1
• 2. Number of binary predicate + 1.
• 3. Number of edges - Number of Nodes + 2.
Basis Path Testing
• Steps
• 1. Construct a flow graph based on a program’s decision logic.
• 2. Compute cyclomatic complexity of the flow graph.
• 3. Determine basis paths..
• 4. Design test cases to go through these basis paths.
23
Baseline path method
• Simple approach to identify basis paths
1. Find the shortest path from entry to exit
2. Return to the function entry point
3. Change the conditional expression to its alternate
outcome
4. Follow shortest path to the exit point
5. Repeat until all basis paths are defined
6. Identify data to execute paths
24
White-box Testing Example
FindMean(float Mean, FILE ScoreFile)
{ SumOfScores = 0.0; NumberOfScores = 0; Mean = 0;
Read(ScoreFile, Score); /*Read in and sum the scores*/
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");
}
Prepare for Flow Graph
FindMean (FILE ScoreFile)
{ float SumOfScores = 0.0;
int NumberOfScores = 0;
1
float Mean=0.0; float Score;
Read(ScoreFile, Score);
2 while (! EOF(ScoreFile) {
3 if (Score > 0.0 ) {
SumOfScores = SumOfScores + Score;
4
NumberOfScores++;
}
Read(ScoreFile, Score); 5
}
/* Compute the mean and print the result */
6 if (NumberOfScores > 0) {
Mean = SumOfScores / NumberOfScores; 7
printf(“ The mean score is %f\n”, Mean);
} else
printf (“No scores found in file\n”); 8
9 }
Constructing the Logic Flow Diagram
1 CC =
+ 4 (closed regions)
2 + 3 (Conditional Nodes) + 1
+ 11 – 9 + 2 = 4
3
4
5
6
7 8
9
Constructing the Logic Flow Diagram
Note: If node 4 is included
1
then node 7 must be included
2 1, 2, 6, 8, 9 ok
1, 2, 6, 7, 9 is not possible
3 1, 2, 3, 4, 5, 2, 6, 7, 9 ok
4 1, 2, 3, 4, 5, 2, 6, 8, 9 is not possible
5 1, 2, 3, 5, 2, 6, 8, 9 ok
1, 2, 3, 5, 2, 6, 7, 9 is not possible
6
7 8 These 3 tests cover all paths!
We didn’t need 4!
9 Every line of code is tested!
Constructing the Logic Flow Diagram
1
1, 2, 6, 8, 9
2
Test with no records
3 in the file
4
5
6
7 8
9
Constructing the Logic Flow Diagram
1
3 1, 2, 3, 4, 5, 2, 6, 7, 9
4
A test with one
5 positive score
6
7 8
9
Constructing the Logic Flow Diagram
1
2
1, 2, 3, 5, 2, 6, 8, 9
3 A test with one
4 negative score
5
6
7 8
9
Applying White-Box Testing
Techniques
32
Practice: Basis path testing:
static int CountC(string s)
{
int index = 0; int i = 0; int j = 0; int k = 0;
char[] strArray = s.ToCharArray();
if (strArray[index] == 'A')
{
while (++index < strArray.Length)
{
if (strArray[index] == 'B')
{
j = j + 1;
k = 0;
}
else if (strArray[index] == 'C')
{
i = i + j;
k = k + 1;
j = 0;
}
}
i = i + j;
}
else
{
return -1;
}
return i;
}
Example: Basis path testing
internal static int GetMaxDay(int month, int day, int
year)
{
int maxDay = 0;
if (!IsInvalidDate(month, day, year)) {
if (IsThirtyOneDayMonth(month)) {
maxDay = 31;
}
else if (IsThirtyDayMonth(month)) {
maxDay = 30;
}
else {
maxDay = 28;
if (IsLeapYear(year)) {
maxDay = 29;
}
}
return maxDay;
}
White Box test design and measurement techniques
• Techniques defined in BS 7925-2
• Statement testing
• Branch / Decision testing Also a measurement
• Data flow testing technique?
• Branch condition testing = Yes
• Branch condition combination testing = No
• Modified condition decision testing
• LCSAJ testing
• Also defines how to specify other techniques
Using structural coverage
Spec
Spec Enough
Software
Software tests?
Tests
Results OK?
What's
covered
More tests ? Coverage OK?
Stronger structural
techniques (different
structural elements)
Increasing coverage
The test coverage trap
Function better
better testing
testing
Function exercised,
exercised,
insufficient
insufficient structure
structure
Functional
testedness
Structure
Structure exercised,
exercised,
insufficient
insufficient function
function
% Statement % Decision % Condition
Combination
Structural testedness
100%
100% coverage
coverage does
does Coverage
Coverage isis not
not
not
not mean
mean 100%
100% tested!
tested! thoroughness
thoroughness
Statement
Statement coverage
coverage
Statement coverage is
is normally
normally measured
measured
by
by aa software
software tool.
tool.
• percentage of executable statements exercised by a test suite
number of statements exercised
total number of statements
• example:
= has 100 statements ?
• program
• tests exercise 87 statements
• statement coverage = 87%
Typical
Typical ad
ad hoc
hoc testing
testing achieves
achieves 60
60 -- 75%
75%
Example of statement coverage
1 read(a) Test Input Expected
2 IF a > 6 THEN case output
3 b=a
1 7 7
4 ENDIF
5 print b
As all 5 statements are ‘covered’ by
this test case, we have achieved
Statement 100% statement coverage
numbers
Decision coverage Decision
Decision coverage
coverage
(Branch coverage) is
is normally
normally measured
measured
by
by aa software
software tool.
tool.
• percentage of decision outcomes
exercised by a test suite
number of decisions outcomes exercised
total number of decision outcomes
False
• example:= ?
• program has 120 decision outcomes True
• tests exercise 60 decision outcomes
• decision coverage = 50%
Typical
Typical ad
ad hoc
hoc testing
testing achieves
achieves 40
40 -- 60%
60%
Paths through code 1234
12 12 123
?
? ? ? ?
?
Paths through code with loops
1 2 3 4 5 6 7 8 ….
for as many times as it
is possible to go round
? the loop (this can be
unlimited, i.e. infinite)
Wait
Example 1
Wait for card to be inserted Yes
Valid Display
IF card is a valid card THEN card? “Enter..
display “Enter PIN number” No
IF PIN is valid THEN
Reject Valid Yes Select
select transaction
card PIN? trans...
ELSE (otherwise) No
display “PIN invalid”
ELSE (otherwise) Display
reject card “PIN in..
End
End
Example 2 Read
Read A Yes Yes
A>0 A=21
IF A > 0 THEN
IF A = 21 THEN No No
Print
Print
Print“Key”
“Key”
ENDIF
ENDIF
End
3
• Cyclomatic complexity: _____
• Minimum tests to achieve:
1
• Statement coverage: ______
3
• Branch coverage: _____
Example 3 Read
Read A Yes No
Read B A>0 B=0 Print
IF A > 0 THEN No Yes
Yes
IF B = 0 THEN Print A>21 Print
Print “No values”
No
ELSE
Print B
IF A > 21 THEN End
Print A 4
• Cyclomatic complexity: _____
ENDIF • Minimum tests to achieve:
ENDIF 2
• Statement coverage: ______
ENDIF 4
• Branch coverage: _____
Yes
Print
Example 4 Read
No
A<0
Read A Note: there Print
Read B are 4 paths
IF A < 0 THEN Yes
Print “A negative” B<0 Print
ELSE No
Print “A positive” Print
ENDIF
IF B < 0 THEN
Print “B negative” End
ELSE 3
• Cyclomatic complexity: _____
Print “B positive” • Minimum tests to achieve:
ENDIF 2
• Statement coverage: ______
2
• Branch coverage: _____
Yes
Print
Example 5 Read
No
A<0
Read A
Read B Yes
B<0 Print
IF A < 0 THEN
No
Print “A negative”
ENDIF
IF B < 0 THEN End
Print “B negative”
ENDIF
3
• Cyclomatic complexity: _____
• Minimum tests to achieve:
1
• Statement coverage: ______
2
• Branch coverage: _____
Yes
Print
Example 6 Read
No
A<0
Read A
IF A < 0 THEN Yes
A>0 Print
Print “A negative”
No
ENDIF
IF A > 0 THEN
Print “A positive” End
ENDIF
3
• Cyclomatic complexity: _____
• Minimum tests to achieve:
2
• Statement coverage: ______
2
• Branch coverage: _____