0% found this document useful (0 votes)
110 views60 pages

Structural Testing Explained

This document discusses structural testing and path testing concepts. It defines key terms like: - Structural testing tests based on program source code and uses knowledge of program structure. - A program graph models a program as nodes (statements) and edges (control flow). - A decision-to-decision (DD) path is a path in the program graph from one decision node to another. - A DD-path graph models the control flow between DD-paths. - An entry-exit path is a path from a method's entry node to its exit node in a control flow graph.

Uploaded by

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

Structural Testing Explained

This document discusses structural testing and path testing concepts. It defines key terms like: - Structural testing tests based on program source code and uses knowledge of program structure. - A program graph models a program as nodes (statements) and edges (control flow). - A decision-to-decision (DD) path is a path in the program graph from one decision node to another. - A DD-path graph models the control flow between DD-paths. - An entry-exit path is a path from a method's entry node to its exit node in a control flow graph.

Uploaded by

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

Path Testing and Test Coverage

Chapter 9
Structural Testing

  Also known as glass/white/open box testing

  Structural testing is based on using specific knowledge of


the program source text to define test cases
  Contrast with functional testing where the program text is
not seen but only hypothesized

PT–2
Structural Testing

  Structural testing methods are amenable to


  Rigorous definitions
  Control flow, data flow, coverage criteria
  Mathematical analysis
  Graphs, path analysis
  Precise measurement
  Metrics, coverage analysis

PT–3
Program Graph Definition

  What is a program graph?

PT–4
Program Graph Definition – 2

  Given a program written in an imperative programming


language
  Its program graph is a directed graph in which nodes are
statements and statement fragments, and edges represent
flow of control
  Two nodes are connected if execution can proceed from one
to the other

PT–5
Triangle program text

1 output ("Enter 3 integers")


2 input (a, b, c)
3 output("Side a b c: ", a, b, c)
4 if (a < b) and (b < a+c) and (c < a+b)
5 then isTriangle ← true
6 else isTriangle ← false
7 fi
8 if isTriangle
9 then if (a = b) and (b = c)
10 else output ("equilateral")
11 else if (a ≠ b ) and ( a ≠ c ) and ( b ≠ c)
12 then output ("scalene")
13 else output("isosceles")
14 fi
15 fi
16 else output ("not a triangle")
17 fi
PT–6
Triangle Program Program Graph

PT–7
DD-Path

  What is a DD-path?

PT–8
DD-Path – informal definition

  A decision-to-decision path (DD-Path) is a path chain in


a program graph such that
  Initial and terminal nodes are distinct
  Every interior node has indeg =1 and outdeg = 1
  The initial node is 2-connected to every other node in
the path
  No instances of 1- or 3-connected nodes occur

PT–9
Connectedness definition

  What is the definition of node connectedness?


  Hint: There are 4-types of connectedness

PT–10
Connectedness definition – 2

  Two nodes J and K in a directed graph are


  0-connected iff no path exists between them

  1-connected iff a semi-path but no path exists between


them

PT–11
Connectedness definition – 2

  Two nodes J and K in a directed graph are


  2-connected iff a path exists between between them

  3-connected iff a path goes from J to K , and a path goes


from K to n1

PT–12
DD-Path – formal definition

  A decision-to-decision path (DD-Path) is a chain in a


program graph such that:
  Case 1: consists of a single node with indeg=0
  Case 2: consists of a single node with outdeg=0
  Case 3: consists of a single node with
indeg ≥ 2 or outdeg ≥ 2
  Case 4: consists of a single node with
indeg =1, and outdeg = 1
  Case 5: it is a maximal chain of length ≥ 1

  DD-Paths are also known as segments

PT–13
Triangle program DD-paths

Nodes Path Case Nodes Path Case


1 First 1 10 H 4
2,3 A 5 11 I 3
4 B 3 12 J 4
5 C 4 13 K 4
6 D 4 14 L 3
7 E 3 15 M 3
8 F 3 16 N 4
9 G 3 17 Last 2

PT–14
DD-path Graph

  What is a DD-path graph?

PT–15
DD-Path Graph – informal definition

  Given a program written in an imperative language, its


DD-Path graph is a directed graph, in which
  Nodes are DD-Paths of its program graph
  Edges represent control flow between successor DD-Paths.

  Also known as Control Flow Graph

PT–16
Control Flow Graph Derivation

  Straightforward process

  Some judgment is required

  The last statement in a segment must be


  a predicate
  a loop control
  a break
  a method exit

PT–17
Triangle program DD-path graph

PT–18
displayLastMsg – Example Java program

public int displayLastMsg(int nToPrint) {!


np = 0;!
if ((msgCounter > 0) && (nToPrint > 0)) {!
for (int j = lastMsg; (( j != 0) && (np < nToPrint)); --j) {!
System.out.println(messageBuffer[j]);!
++np;!
}!
if (np < nToPrint) {!
for (int j = SIZE; ((j != 0) && (np < nToPrint)); --j) {!
System.out.println(messageBuffer[j]);!
++np;!
}!
}!
}!
return np;!
}!
!
PT–19
displayLastMsg– Segments part 1

Line Segment

1 public int displayLastMsg(int nToPrint) {!


2 np = 0;! A
3a if ( (msgCounter > 0)! A
3b && (nToPrint > 0)) B
4a { for (int j = lastMsg;! C
4b ( ( j != 0) D
4c && (np < nToPrint)); E
4d --j)! F
5 { System.out.println(messageBuffer[j]);! F
6 ++np;! F
7 } F
PT–20
displayLastMsg– Segments part 2

Line Segment
8 if (np < nToPrint)! G
9a { for (int j = SIZE; ! H
9b ((j != 0) &&! I
9c (np < nToPrint));! J
9d --j)! K
10 { System.out.println(messageBuffer[j]);! K
11 ++np;! K
12 } L
13 } L
14 } L
15 return np; L
16 } L
PT–21
displayLastMsg – Control Flow Graph

PT–22
Control flow graphs definition – 1

  Depict which program segments may be followed by


others

  A segment is a node in the CFG

  A conditional transfer of control is a branch represented


by an edge

  An entry node (no inbound edges) represents the entry


point to a method

  An exit node (no outbound edges) represents an exit


point of a method

PT–23
Control flow graphs definition – 2

  An entry-exit path is a path from the entry node to the


exit node

  Path expressions represent paths as sequences of


nodes

  Loops are represented as segments within parentheses


followed by an asterisk

  There are 22 different entry-exit path expressions in


displayLastMsg

PT–24
Entry-exit path expressions – part 1

Entry-Exit paths
1 AL
2 ABL
3 ABCDGL
4 ABCDEGL
5 A B C (D E F)* D G L
6 A B C (D E F)* D E G L
7 ABCDGHIL
8 ABCDGHIJL
9 A B C D G H (I J K)* I L
10 A B C (D E F)* D E G H (I J K)* I J L
11 ABCDEGHIL

PT–25
Entry-exit path expressions – part 2

Entry-Exit paths
12 ABCDEGHIJL
13 A B C D E G H (I J K)* I L
14 A B C D E G H (I J K)* I J L
15 A B C (D E F)* D G H I L
16 A B C (D E F)* D G H I J L
17 A B C (D E F)* D G H (I J K)* I L
18 A B C (D E F)* D G H (I J K)* I J L
19 A B C (D E F)* D E G H I L
20 A B C (D E F)* D E G H I J L
21 A B C (D E F)* D E G H (I J K)* I L
22 A B C (D E F)* D E G H (I J K)* I J L

PT–26
Paths displayLastMsg – decision table – part 1

Path condition by Segment Name


Entry/Exit Path A B D E G I J
1 AL F – – – – – –
2 ABL T F – – – – –
3 ABCDGL T T F – F – –
4 ABCDEGL T T T F – – –
5 A B C (D E F)* D G L T T T/F T/– F – –
6 A B C (D E F)* D E G L T T T/T T/F F – –
7 ABCDGHIL T T F – T F –
8 ABCDGHIJL T T F – T T F
9 A B C D G H (I J K)* I L T T F – T/F T/– T
10 A B C D G H (I J K)* I J L T T F – T/T T/F T
11 ABCDEGHIL T T T F T F –

x/x Conditions at loop-entry / loop-exit – is don’t care

PT–27
Paths displayLastMsg – decision table – part 2

Path condition by Segment Name


Entry/Exit Path A B D E G I J
12 ABCDEGHIJL T T T F T T F
13 A B C D E G H (I J K)* I L T T T F T T/F T/–
14 A B C D E G H (I J K)* I J L T T T F T T/T T/F
15 A B C (D E F)* D G H I L T T T/F T/– T F –
16 A B C (D E F)* D G H I J L T T T/T T/F T T F
17 A B C (D E F)* D G H (I J K)* I L T T T/F T/– T T/F T/–
18 A B C (D E F)* D G H (I J K)* I J L T T T/F T/– T T/T T/F
19 A B C (D E F)* D E G H I L T T T/T T/F T F –
20 A B C (D E F)* D E G H I J L T T T/T T/F T T F
21 A B C (D E F)* D E G H (I J K)* I L T T T/T T/F T T T
22 A B C (D E F)* D E G H (I J K)* I J L T T T/T T/F T T T

x/x Conditions at loop-entry / loop-exit – is don’t care

PT–28
Program text coverage Metrics

  List the program text coverage metrics.

PT–29
Program text coverage Metrics – 2

  C0 Every Statement

  C1 Every DD-path

  C1p Every predicate to each outcome

  C2 C1 coverage + loop coverage

  Cd C1 coverage + every dependent pair of DD-paths

  CMCC Multiple condition coverage


  Cik Every program path that contains k loop repetitions

  Cstat Statistically significant fraction of the paths

  C∞ Every executable path

PT–30
Program text coverage models

  What are the common program text coverage


models?

PT–31
Program text coverage models – 2

  Statement Coverage

  Segment Coverage

  Branch Coverage

  Multiple-Condition Coverage

PT–32
Statement coverage – C0

  When is statement coverage achieved?

PT–33
Statement coverage – C0 – 2

  Achieved when all statements in a method have been


executed at least once

  A test case that will follow the path expression below will
achieve statement coverage in our example

A B C (D E F)* D G H (I J K)* I L

  One test case is enough to achieve statement coverage!

PT–34
Segment coverage

  When is segment coverage achieved?

PT–35
Segment coverage – 2

  Achieved when all segments have been executed at least


once
  Segment coverage counts segments rather than statements
  May produce drastically different numbers
  Assume two segments P and Q

  P has one statement, Q has nine

  Exercising only one of the segments will give either 10%

or 90% statement coverage


  Segment coverage will be 50% in both cases

PT–36
Statement coverage problems

  What problems are there with statement coverage?

PT–37
Statement coverage problems – 2

  Important cases may be missed


  Predicate may be tested for only one value
  misses many bugs
  Loop bodies may only be iterated only once

String s = null;
if (x != y) s = Hi ;
String s2 = s.substring(1);

  What coverage solves this problem?


  Define it

PT–38
Branch coverage – C1p

  Achieved when every edge from a node is executed at


least once

  At least one true and one false evaluation for each


predicate

  How many test cases are required?

PT–39
Branch coverage – C1p – 2

  Can be achieved with D+1 paths in a control flow graph


with D 2-way branching nodes and no loops
  Even less if there are loops

  In the Java example displayLastMsg branch coverage is


achieved with three paths – see next few slides

XL
X C (Y F)* Y G L
X C (Y F)* Y G H (Z K)* Z L

PT–40
Java example program displayLastMsg – DD-path graph

X, Y & Z are shorthand for the nodes


within the dotted boxes; used for branch testing
PT–41
Java example program displastLastMsg
– aggregate predicate DD-path graph

PT–42
Aggregate Paths – decision table – part 1

Path condition by Segment Name


Branch Coverage A B D E G I J
1 XL F – – – – – –
2 XL T F – – – – –
3 XCYGL T T F – F – –
4 XCYGL T T T F – – –
5 X C (Y F)* Y G L T T T/F T/– F – –
6 X C (Y F)* Y G L T T T/T T/F F – –
7 XCYGHZL T T F – T F –
8 XCYGHZL T T F – T T F
9 X C Y G H (Z K)* I L T T F – T/F T/– T
10 X C Y G H (Z K)* I L T T F – T/T T/F T
11 XCYGHZL T T T F T F –

x/x Conditions at loop-entry / loop-exit – is don’t care

PT–43
Aggregate Paths – decision table example – part 2

Path condition by Segment Name


Branch Coverage A B D E G I J
12 XCYGHZL T T T F T T F
13 X C Y G H (Z K)* Z L T T T F T T/F T/–
14 X C Y G H (Z K)* Z L T T T F T T/T T/F
15 X C (Y F)* Y G H Z L T T T/F T/– T F –
16 X C (Y F)*Y G H Z L T T T/T T/F T T F
17 X C (Y F)* Y G H (Z K)* Z L T T T/F T/– T T/F T/–
18 X C (Y F)* Y G H (Z K)* Z L T T T/F T/– T T/T T/F
19 X C (Y F)* Y G H Z L T T T/T T/F T F –
20 X C (Y F)* Y G H Z L T T T/T T/F T T F
21 X C (Y F)* Y G H (Z K)* Z L T T T/T T/F T T T
22 X C (Y F)* Y G H (Z K)* Z L T T T/T T/F T T T

x/x Conditions at loop-entry / loop-exit – is don’t care

PT–44
Branch coverage problems

  What are the problems with branch coverage?

PT–45
Branch coverage problems – 2

  Ignores implicit paths from compound paths


  11 paths in aggregate model vs 22 in full model

PT–46
Branch coverage problems – 3

  Ignores implicit paths from compound paths


  11 paths in aggregate model vs 22 in full model

  Short-circuit evaluation means that many


predicates might not be evaluated
  A compound predicate is treated as a single
statement. If n clauses, 2n combinations, but only 2
are tested

PT–47
Branch coverage problems – 4

  Ignores implicit paths from compound paths


  11 paths in aggregate model vs 22 in full model

  Short-circuit evaluation means that many predicates might not be


evaluated
  A compound predicate is treated as a single statement. If n
clauses, 2n combinations, but only 2 are tested

  Only a subset of all entry-exit paths is tested


  Two tests for branch coverage vs 4 tests for path
coverage
  a = b = x = y = 0 and a = x = 0 ∧ b = y = 1

if (a == b) x++;
if (x == y) x--;

PT–48
Overcoming branch coverage problems

  How do we overcome branch coverage problems?

PT–49
Overcoming branch coverage problems – 2

  Use Multiple condition coverage

  All true-false combinations of simple conditions in


compound predicates are considered at least once
  Guarantees statement, branch and predicate coverage
  Does not guarantee path coverage

  A truth table may be necessary

  Not necessarily achievable


  lazy evaluation – true-true and true-false are impossible
  mutually exclusive conditions – false-false branch is
impossible
if ((x > 0) || (x < 5)) …

PT–50
Overcoming branch coverage problems – 3

  Can have infeasible paths due to dependencies and


redundant predicates
  Paths perpetual .. motion and free .. lunch are impossible
  In this case indicates a potential bug
  At least poor program text

if x = 0 then oof.perpetual
else off.free
fi

if x != 0 then oof.motion
else off.lunch
fi

PT–51
Dealing with Loops

  Loops are highly fault-prone, so they need to be tested


carefully

  Based on the previous slides on testing decisions


what would be a simple view of testing a loop?

PT–52
Dealing with Loops – 2

  Simple view
  Involves a decision to traverse the loop or not
  Test as a two way branch

  What would functional testing suggest as a better


way of testing?

  What tests does it suggest?

PT–53
Dealing with Loops – 3

  A bit better
  Boundary value analysis on the index variable
  Suggests a zero, one, many tests

  How do we deal with nested loops?

PT–54
Dealing with Loops – 3

  Nested loops
  Tested separately starting with the innermost

  Once loops have been tested what can we do with


the control flow graph?

PT–55
Dealing with Loops – 4

  Once loops have been tested


  They can be condensed to a single node

PT–56
Condensation graphs

  Condensation graphs are based on removing strong


components or DD-paths

  For programs remove structured program constructs


  One entry, one exit constructs for sequences, choices and
loops
  Each structured component once tested can be replaced by
a single node when condensing its graph

PT–57
Violations of proper structure

  Program text that violates proper structure cannot be


condensed
  Branches either into or out of the middle of a loop
  Branches either into or out of then and else phrases of if…
then…else statements
  Increases the complexity of the program
  Increases the difficulty of testing the program

PT–58
Cyclomatic number

  The cyclomatic number for a graph is given by


  CN(G) = e – v + 2*c
  e number of edges
v number of vertices
c number of connected regions

  For strongly connected graphs, need to add edges


from every sink to every source

PT–59
Cyclomatic number for programs

  For properly structured programs there is only one


component with one entry and one exit. There is no edge
from exit to entry.

  Definition 1: CN(G) = e – v + 2
  Only 1 component, not strongly connected

  Definition 2: CN(G) = p + 1
  p is the number of predicate nodes with out degree = 2

  Definition 3: CN(G) = r + 1
  r is the number of enclosed regions

PT–60

You might also like