Structural Testing: Understanding Control Flow Graphs and Coverage in Software Testing, Study notes of Software Engineering

A lecture note from the university of colorado, csci 3308 - fall semester, 2003, focusing on structural testing. It covers the concept of control flow graphs (cfgs), types of coverage such as statement, edge, condition, and relational coverage, and the importance of testing all statements, edges, and conditions. The document also discusses the challenges of achieving path coverage and the use of heuristics to approximate it.

Typology: Study notes

Pre 2010

Uploaded on 02/10/2009

koofers-user-p8m
koofers-user-p8m 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 19: Structural Testing
Kenneth M. Anderson
Software Methods and Tools
CSCI 3308 - Fall Semester, 2003
(Happy Halloween!)
October 31, 2003 © University of Colorado, 2003 2
Today’s Lecture
Discuss Structural Testing
Terminology
Techniques
Examples
October 31, 2003 © University of Colorado, 2003 3
Structural Testing
Structural Testing supplies another criteria to answer
the question:
“How many test cases are enough?”
Recall that functional testing’s criteria was “Test all
functions”
Structural Testing’s criteria is “Test all code”
Structural Testing is also known as white box testing,
because now we look at a program’s source code to help
create test cases
October 31, 2003 © University of Colorado, 2003 4
Control Flow Graphs (CFGs)
Structural Testing is based on CFGs
Control flow graphs capture the various ways
in which a program can execute
A node in a CFG represents a program statement
An edge in the CFG represents the ability for a
program to flow from its current statement to the
statement at the other end of the edge
If an edge is associated with a conditional, label the edge
with the conditional’s value, either true or false
pf3
pf4
pf5

Partial preview of the text

Download Structural Testing: Understanding Control Flow Graphs and Coverage in Software Testing and more Study notes Software Engineering in PDF only on Docsity!

Lecture 19: Structural Testing

Kenneth M. Anderson

Software Methods and Tools

CSCI 3308 - Fall Semester, 2003

(Happy Halloween!)

October 31, 2003

© University of Colorado, 2003

Today’s Lecture^ ^

Discuss Structural Testing^ 

Terminology  Techniques  Examples

October 31, 2003

© University of Colorado, 2003

3

Structural Testing^ ^

Structural Testing supplies another criteria to answerthe question:^ 

“How many test cases are enough?” ^

Recall that functional testing’s criteria was “Test allfunctions” ^

Structural Testing’s criteria is “Test all code”^ 

Structural Testing is also known as white box testing,because now we look at a program’s source code to helpcreate test cases

October 31, 2003

© University of Colorado, 2003

Control Flow Graphs (CFGs)^ ^

Structural Testing is based on CFGs

^

Control flow graphs capture the various waysin which a program can execute^ 

A node in a CFG represents a program statement  An edge in the CFG represents the ability for aprogram to flow from its current statement to thestatement at the other end of the edge

^

If an edge is associated with a conditional, label the edgewith the conditional’s value, either true or false

© University of Colorado, 2003

5

(^123456789101112131415)

function

P

return

INTEGER

is

begin

X, Y: INTEGER;READ(X); READ(Y); while

(X > 10)

loop

X := X – 10; exit when

X = 10;

end loop

;

if^ (Y < 20

and then

X

mod

2 = 0)

then

Y := Y + 20; else

Y := Y – 20; end if

; return

^2

∗^

X^ +

Y;

end

P;

A Sample Ada Program

October 31, 2003

© University of Colorado, 2003

T^

T F

F^

T

F

T

F

P’s Control Flow Graph (CFG)

© University of Colorado, 2003

7

Types of Coverage^ ^

Statement Coverage^ ^

Every statement is executed at least once

^

Edge Coverage^ ^

Every edge is traversed at least once

^

Condition Coverage^ ^

For binary logical operators (&&, ||), the individual components areevaluated in every possible combination of true and false

^

Relational Coverage^ ^

For relational operators (<, >, <=, >=) the equal condition is treated as aseparate branch

^

Path Coverage^ ^

Every possible path is executed at least once

October 31, 2003

© University of Colorado, 2003

White-box Testing Criteria^ ^

Statement Coverage^ 

Execute each statement at least once  Pick test case and plot its path through theCFG  Keep picking test cases until all statementsare covered

© University of Colorado, 2003

13

White-box Testing Criteria^ ^

Edge Coverage^ 

Traverse each edge at least once  Pick test case and plot its path through CFG  Keep picking test cases until all edges arecovered ^

Also known as Branch Coverage^ 

We must traverse each conditional (such as an ifstatement) along its true and false edge

October 31, 2003

© University of Colorado, 2003

All-Edges Coverage of P 2,3,

T^

T F

F^

T

F

T

F

© University of Colorado, 2003

15

Test Case 1: X = 20, Y = 10 2,3,

T^

T F

F^

T

F

T

F

Example all-edges-adequate test set:

(X = 20, Y = 10)

October 31, 2003

© University of Colorado, 2003

Test Case 2: X = 15, Y = 30 2,3,

T^

T F

F^

T

F

T

F

Example all-edges-adequate test set:

(X = 20, Y = 10)(X =15, Y = 30)

© University of Colorado, 2003

17

Combined: Complete Coverage 2,3,

T^

T F

F^

T

F

T

F

Example all-edges-adequate test set:

(X = 20, Y = 10)(X =15, Y = 30)

October 31, 2003

© University of Colorado, 2003

White-box Testing Criteria^ ^

Condition Coverage^ 

Traverse all edges at least once but

^

in binary logical operators (also known as shortcircuit operators), all possible combinations oftrue and false must be tested

^

Pick test case and plot its path throughCFG; keep creating test cases until allconditions are covered

© University of Colorado, 2003

19

All-Conditions Coverage of P 2,3,

T^

T F

F^

T

F

T

F

October 31, 2003

© University of Colorado, 2003

Test Case 1: X = 20, Y = 10 2,3,

T^

T F

F^

T

F

T

F

Example all-conditions-adequate test set:

(X = 20, Y = 10)

© University of Colorado, 2003

25

Relational Coverage, continued^ ^

Relational coverage is thus a stronger form ofedge coverage

^

It is saying that for each conditional youshould have at least three test cases^ 

x < y, x > y, x == y ^

Combine this approach with conditionalcoverage and you have the strongest form ofedge coverage possible

October 31, 2003

© University of Colorado, 2003

Path Coverage^ ^

Path Coverage^ 

Traverse each path at least once ^

Problem^ 

Way too many paths, even in simple programs ^

Approach^ 

Use heuristics

^

e.g. for each loop take loop zero, one, and multiple times

© University of Colorado, 2003

27

Example^ ^

How many paths does thefollowing program fragmenthave?^ a << cin; b = 0;while (a > 0) {

a--; b++; } if (b > 5) {

printf(“b > 5”); } else {

printf(“b <= 5”); }

^

For any particular valueof a, there is only onepath possible ^

but since a is enteredby user, there are aninfinite number ofpossible paths!

October 31, 2003

© University of Colorado, 2003

Path Coverage, continued^ ^

In general^ ^

for loops^ 

traversing a loop zero, one, two, … times is each a differentpath, so a loop has a potentially infinite number of paths ^

for conditionals^ 

traverse true and false branches  for a program consisting of only if statements

^

if x is the number of if statements, there are a total of 2

x^ paths!

^

As such, path coverage is an infeasible testingcriteria in the general case; so use heuristics toapproximate it, as discussed previously