Whitebox Structural Testing
Whitebox Structural Testing
Software Testing
Lecture 5 :
Structural Testing
“Structural” testing
• Testing that is based on the structure of the program.
7
Flow Graph Representation
C
If C Then S1 else S2;
C C
Case C of
If C Then S1;
L1: S1;
L2: S2;
S1 Sn … S1
Kn: Sn;
end;
8
Flow Graph Representation
C I=1
While C do S; For loop:
F for I = 1 to n do S;
T S
S
yes
I <=n
no
S1 Do loop:
do S1 until C;
F
C
T 9
Control Flow Graph
• The control flow graph G = (N, E)
of a program consists of a set of “nodes”
12
Test Cases
• A test case is a complete path from the
entry node to the exit node of a control flow
graph.
• Branch Coverage
13
Using structural coverage
Spe Enough
Spe
cc Software
Software tests?
Tests
Results OK?
What's
covered
More tests ? Coverage OK?
Stronger structural
techniques (different
structural elements)
Increasing coverage 14
The test coverage trap
Function better
bettertesting
testing
Functionexercised,
exercised,
insufficient
insufficientstructure
structure
Functional
testedness
Structure
Structureexercised,
exercised,
insufficient
insufficientfunction
function
100%
100%coverage
coveragedoes
does Coverage
Coverageisisnot
not
not
notmean
mean100%
100%tested!
tested! Thoroughness
Thoroughness
15
Statement coverage Statement
Statementcoverage
isisnormally
coverage
normallymeasured
measured
by
byaasoftware
softwaretool.
tool.
• example:
• program has 100 statements
Statement
numbers
17
Decision coverage (Branch
coverage) Decision coverage Decision coverage
isisnormally
normallymeasured
measured
• percentage of decision outcomes by
byaasoftware
softwaretool.
tool.
1 2 1 2 1 2 3
?
? ? ? ?
19
Paths through code with
loops
1 2 3 4 5 6 7 8 ….
20
Cyclomatic Complexity
• Cyclomatic complexity is a software metric used to
program. 21
Cyclomatic Complexity
Three ways to compute cyclomatic complexity:
• The number of regions of the flow graph correspond to
the cyclomatic complexity.
• Cyclomatic complexity, V(G), for a flow graph G is
defined as V(G) = E - N +2 , where E is the number of
flow graph edges and N is the number of flow graph
nodes.
• Cyclomatic complexity, V(G) = P + 1, where P is the
number of predicate nodes contained in the flow graph
G.
22
An Example
01. insertion_procedure (int a[], int p [], int N)
02. {
07. {
12. }
14. }
23
An Example
24
Example 1 ATM Wait
number” Valid
Yes
Reject Select
card PIN? trans...
IF PIN is valid THEN No
select transaction
Display
ELSE (otherwise) “PIN in..
display “PIN
invalid”
End
ELSE (otherwise) 25
Example 2
Read A
IF A > 0 THEN Read
IF A = 21 THEN
Yes Yes
Print “Key” A>0 A=21
ENDIF No No
Print
ENDIF
3 _____
• Cyclomatic complexity:
Print “A negative”
Yes
ENDIF A>0 Print
No
IF A > 0 THEN
Print “A positive”
End
ENDIF
• Cyclomatic complexity:
3 _____
• Minimum tests to achieve:
2 ______
• Statement coverage:
2 _____
• Branch coverage: 30
Solve it
• Tell me in the following table how many test cases it needs to
cover all statements, all branches, all paths or all basic paths:
Control Flow Control Flow Control Flow Control Flow
1 2 3 4
Branches
Statemen
ts
Paths 31
Non-systematic test techniques
• User Testing
• Unscripted Testing
AAtesting
testingapproach
approachthat
thatisisonly
only
rigorous, thorough and systematic
rigorous, thorough and systematic
isisincomplete
incomplete
32
Error-Guessing
• always worth including
• Consider:
• past failures
• intuition
• experience
• brain storming
34
Summary: Key Points