0% found this document useful (0 votes)
12 views35 pages

Whitebox Structural Testing

Uploaded by

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

Whitebox Structural Testing

Uploaded by

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

CA 7503

Software Testing
Lecture 5 :
Structural Testing
“Structural” testing
• Testing that is based on the structure of the program.

• Also know as ‘white-box’ / ‘glass-box’ testing

• Usually better for finding defects than for exploring the


behaviour of the system.

• Fundamental idea is that of basic block and flow graph -


most work is defined in those terms.

Two main approaches:


• Control oriented: how much of the control aspect of the code has
been explored?
• Data oriented: how much of the definition/use relationship
between data elements has been explored.
3
Goal of “Structural”
testing
• Guarantee that all independent paths within a
module have been exercised at least once.
• Exercise all logical decisions one their true and
false sides.
• Execute all loops at their boundaries and within
their operational bounds.
• Exercise internal data structures to assure their
validity.
• Exercise all data define and use paths.
4
Basic Blocks
• A basic block has at most one entry point and usually at most
two exit points.

Can you think of exceptions to this?

• We decompose our program into basic blocks. These are the


nodes of the control graph.

• The edges of the control graph indicate control flow -


possibly under some conditions.

• Sequence of consecutive statements such that: – Control


enters only at beginning of sequence – Control leaves only at
end of sequence.

• No branching in or out in the middle of basic blocks 5


Control Flow Testing
• Control flow testing uses the control structure of a
program to develop the test cases for the program.

• The test cases are developed to sufficiently cover the


whole control structure of the program.

• The control structure of a program can be represented


by the control flow graph of the program.

• Control flow graph (CFG) is a directed graph in


which the nodes represent basic blocks and the edges
represent control flow paths.

• A control flow graph specifies all possible execution


6
Flow Graph
Representation

7
Flow Graph Representation
C
If C Then S1 else S2;

Sequential statement block


S1 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”

nodes N and a set of edge E.

• Each node represents a set of


program statements. There are
“edges”
five types of nodes. There is a
unique entry node and a unique
exit node.

• There is an edge from node n1 to


node n2 if the control may flow
10
from the last statement in n1 to
Control Flow Graph: Nodes
•A decision node contains a conditional
statement that creates 2 or more control branches
(e.g. if or switch statements).

• A merge node usually does not contain any


statement and is used to represent a program
point where multiple control branches merge.

• A statement node contains a sequence of


statements. The control must enter from the first
statement and exit from the last statement.
11
Control Flow Graph: An Example

12
Test Cases
• A test case is a complete path from the
entry node to the exit node of a control flow
graph.

• A test coverage criterion measures the


extent to which a set of test cases covers a
program.
• Statement Coverage

• 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

% Statement % Decision % Condition


Combination
Structural testedness

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.

• percentage of executable statements


exercised by a test suite

= number of statements exercised


?
total number of statements

• example:
• program has 100 statements

• tests exercise 87 statements

• statement coverage = 87%


Typical
Typical ad
ad hoc
hoc testing
testing achieves
achieves 60
60 -- 16
75%
75%
Example of statement coverage

Test Input Expected


1 read(a) case output
2 IF a > 6
THEN
3 1 7 7
b=a
4
ENDIF
5
print b

As all 5 statements are ‘covered’ by


this test case, we have achieved
100% statement coverage

Statement
numbers

17
Decision coverage (Branch
coverage) Decision coverage Decision coverage
isisnormally
normallymeasured
measured
• percentage of decision outcomes by
byaasoftware
softwaretool.
tool.

exercised by a test suite


number of decisions outcomes exercised
= False
?
total number of decision outcomes
True
• example:
• program has 120 decision outcomes

• tests exercise 60 decision outcomes

• decision coverage = 50%


Typical
Typical ad
ad hoc
hoc testing
testing achieves
achieves 40
40 -- 18
60%
Paths through code
1 2 3 4

1 2 1 2 1 2 3
?

? ? ? ?

19
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)
?

20
Cyclomatic Complexity
• Cyclomatic complexity is a software metric used to

measure the complexity of a program.

• This metric measures independent paths through

the program's source code. An independent path is

defined as a path that has at least one edge which has

not been traversed before in any other paths.

• Cyclomatic complexity can be calculated with respect

to functions, modules, methods or classes within a

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. {

03. (1) Int i,j,k;

04. (2) for ((2a)i=0; (2b)i<=N; (2c)i++)

05. (3) p[i] = i;

06. (4) for ((4a)i=2; (4b)i<=N; (4c)i++)

07. {

08. (5) k=p[i];j=1;

09. (6) while (a[p[j-1]] > a[k]) {

10. (7) p[j] = p[j-1];

11. (8) j--

12. }

13. (9) p[j] = k;

14. }

23
An Example

• Now, to calculate the cyclomatic complexity you use one of three


methods:
• Count the number of regions on the graph: 4
• No. of predicates (red on graph) + 1 : 3 + 1 = 4
• No of edges – no. of nodes + 2: 14 – 12 + 2 = 4

24
Example 1 ATM Wait

Wait for card to be inserted


Yes
Valid Display
IF card is a valid card THEN card? “Enter..

display “Enter PIN No

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:

• Minimum tests to achieve: End


1 ______
• Statement coverage:
3_____
• Branch coverage:
26
Example 3
Read
Read A
Read B Yes No
A>0 B=0 Print
IF A > 0 THEN
No Yes
IF B = 0 THEN Yes
Print A>21 Print
Print “No values”
No
ELSE
Print B End
IF A > 21 THEN
4 _____
• Cyclomatic complexity:
Print A
ENDIF • Minimum tests to achieve:
2
ENDIF • Statement coverage: ______
ENDIF • Branch coverage: _____
4
27
Example 4 Read A<0
Yes
Print
Read A No
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
End
IF B < 0 THEN
3 _____
• Cyclomatic complexity:
Print “B negative”
ELSE • Minimum tests to achieve:
2
Print “B positive” • Statement coverage: ______
2
ENDIF • Branch coverage: _____ 28
Example 5
Yes
Read A Read A<0 Print
Read B No

IF A < 0 THEN Yes


B<0 Print
Print “A negative”
No
ENDIF
IF B < 0 THEN End
Print “B negative”
3
• Cyclomatic complexity: _____
ENDIF
• Minimum tests to achieve:
1
• Statement coverage: ______
2
29
• Branch coverage: _____
Example 6
Read A Yes
Read A<0 Print
IF A < 0 THEN No

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

• Trial and error / Ad hoc

• Error guessing / Experience-driven

• 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

• after systematic techniques have been used

• can find some faults that systematic


techniques can miss

• a ‘mopping up’ approach

• supplements systematic techniques


Not
Notaagood
goodapproach
approachto
tostart
starttesting
testingwith
with
33
Error Guessing: deriving test cases

• Consider:
• past failures

• intuition

• experience

• brain storming

• “What is the craziest thing we can do?”

34
Summary: Key Points

Test techniques are ‘best practice’: help to find faults


Black Box techniques are based on behaviour
White Box techniques are based on structure
Error Guessing supplements systematic techniques

You might also like