lecture 2 white-box testing and structural coverage

42
Lecture 2 White-box Testing and Structural Coverage

Upload: vivian-kelley

Post on 16-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Lecture 2 White-box Testing and Structural Coverage

Lecture 2

White-box Testing and Structural Coverage

Page 2: Lecture 2 White-box Testing and Structural Coverage

White-box Testing (aka. Glass-box or structural testing)

• An error may exist at a location– Line numbers– Boolean tests– Expressions etc.

• If tests don’t exercise that location then error can never be observed

• So identify and exercise locations• No loops – finitely many locations – good!• Loops – infinitely many locations – bad! • Loops + branches – exponential growth in locations with

loop depth – very bad !!!!

Page 3: Lecture 2 White-box Testing and Structural Coverage

Structural Testing

Structural testing is the process of exercising software with test scenarios written from the source code, not from the requirements.

Usually structural testing has the goal to exercise a minimum collection of (combinations of) locations.

How many locations and combinations are enough?

Page 4: Lecture 2 White-box Testing and Structural Coverage

Coverage

• Coverage refers to the extent to which a given testing activity has satisfied its objectives.

• “Enough” testing is defined in terms of coverage.

• A major advantage of structural testing is that coverage can be easily and accurately defined.

• Structural coverage measures

Page 5: Lecture 2 White-box Testing and Structural Coverage

Structural Testing – Problems!

• What about sins of omission? – no path to go down! Unimplemented requirements!

• What about dead code – is a path possible? • How to avoid redundant testing?• What about testing the user requirements?• How to handle combinatorial explosion of

locations and their combinations?

Page 6: Lecture 2 White-box Testing and Structural Coverage

Requirements-based testing versus Structural testing (DO-178B)

Software Requirements-based

Test Generation

Software RequirementsCoverage analysis

Software StructureCoverage analysis

Unit tests Integration tests HW/SW integration tests

End

Page 7: Lecture 2 White-box Testing and Structural Coverage

Problems with requirements-based testing

• A test set that meets requirements coverage is not necessarily a thorough test set

• Requirements may not contain a complete and accurate specification of all code behaviour

• Requirements may be too coarse to assure that all implemented behaviours are tested

• Requirements-based testing alone cannot confirm that code doesn’t include unintended functionality. Need structural testing too!

Page 8: Lecture 2 White-box Testing and Structural Coverage

Coverage Criteria 1: Control flow

• Measure the flow of control between statements and sequences of statements

• Examples: node coverage, edge coverage.• Mainly measured in terms of statement

invocations (line numbers).• Exercise main flows of control.

Page 9: Lecture 2 White-box Testing and Structural Coverage

Coverage Criteria 2: Data Flow

• Data flow criteria measure the flow of data between variable assignments (writes) and variable references (reads).

• Examples: all-definitions, all-uses• Exercise paths between definition of a variable

and is subsequent use.

Page 10: Lecture 2 White-box Testing and Structural Coverage

Coverage Criteria 3: Logic

• Analyse the influence of all Boolean variables• Examples: predicate coverage, clause

coverage, MCDC (FAA DO178B)• Exercise Boolean variables at control points.

Page 11: Lecture 2 White-box Testing and Structural Coverage

Control Flow Requirements

• These are structural requirements on a test suite

• They ignore functionality!• Easy to define using graph theory• Possible to automate generation by constraint

solving• Easy to measure coverage!

Page 12: Lecture 2 White-box Testing and Structural Coverage

Starting Point: a Condensation Graph

Assignments1

Assignments2

Assignments3Assignments4

Cond1

Cond2

A node is either an assignment block or a Boolean test. Every program statement is associated with exactly one node.

Page 13: Lecture 2 White-box Testing and Structural Coverage

Building condensation graphs

• Boolean tests can be from:– If-then-else statements if (bexp) then .. else ..– If statements if (bexp) …– Loops (of any kind) while (bexp) …

• An assignment block contains consecutive– assignments x = exp– return statements return exp– procedure/method calls myFunc(…)– expressions e.g. i++

Page 14: Lecture 2 White-box Testing and Structural Coverage

Graph Coverage

• A path is a sequence of nodes n0 ,…, nk in a (condensation) graph G, such that each adjacent node pair, (ni, ni+1) forms an edge in G.

• For control flow testing a test requirement tr(.) is a predicate on paths– Noden( p ) iff p passes through node n

– Edgee (p) iff p has edge e

Page 15: Lecture 2 White-box Testing and Structural Coverage

Graph Coverage

Definition: Given a set TR of test requirementsfor a graph criterion C, A test suite T satisfies Con graph G if, and only if for every testrequirement tr(.) TR, there is at least one test t in T such that for the path p in G taken by t, tr(p) is true

(i.e. p satisfies tr(.))

Page 16: Lecture 2 White-box Testing and Structural Coverage

Why so Formal?

• Answer: Sometimes coverage properties become very technical to define for efficiency reasons.

Page 17: Lecture 2 White-box Testing and Structural Coverage

Structural Coverage Criteria(Amman and Offut, Chapter 2)

2.1. Node Coverage (NC) Set TR contains each reachable node in G.

Myers “NC is so weak that it is generally considered useless”

2.2. Edge Coverage (EC) Set TR contains each reachable path of length ≤ 1.

Page 18: Lecture 2 White-box Testing and Structural Coverage

Node vs. Edge Coverage

n0x < y

x >= y

n2

n1

p1 = n0, n1, n2

p2 = n0, n2

TS1 = p1 satisfies node coverage

TS2 = p1, p2 satisfies edge coverage

Abstract test cases

Page 19: Lecture 2 White-box Testing and Structural Coverage

• 2.3 Edge-Pair Coverage (EPC = EC2) Set TR contains each reachable path of length ≤ 2.

• Clearly we can continue this beyond 1,2 to ECn

• Combinatorial explosion!• Doesn’t deal with loops, which have

unbounded length.

Page 20: Lecture 2 White-box Testing and Structural Coverage

n0

n1 n2

n3

n4 n5

p1 = n0, n1, n3, n4

p2 = n0, n2, n3, n5

TS1 = p1, p2 satisfies edge coverage

p3 = n0, n2, n3, n4

p4 = n0, n1, n3, n5

TS2 = p1, p2, p3, p4 satisfies edge-pair coverage

x < y x >= y

w < y w >= y

Page 21: Lecture 2 White-box Testing and Structural Coverage

Simple Paths

• How to deal with code loops?• A path p is simple if it has no repetitions of

nodes other than (possibly) the first and last node.

• So a simple path p has no internal loops, but may itself be a loop

• Problem: there are too many simple paths, since many are just sub-paths of longer simple paths.

Page 22: Lecture 2 White-box Testing and Structural Coverage

Prime Paths

• A path p is prime iff p is a maximal simple path i.e. p cannot be extended without losing simplicity.

• This cuts down the number of cases to consider

2.4. Prime Path Coverage (PPC) Set TR contains each prime path in G

Page 23: Lecture 2 White-box Testing and Structural Coverage

n0

n1

n2

n3

n4

Prime Paths = (n0, n1, n2),(n0,n1,n3,n4),(n1,n3,n4,n1),(n3,n4,n1,n3),(n4,n1,n3,n4),(n3,n4,n1,n2)P1 = (n0, n1, n2)

P3 = (n0, n1, n3, n4, n1, n3, n4, n1, n2)

P2 = (n0, n1, n3, n4, n1, n2)

TS1 = p1, p3 satisfies prime path coverage

TS2 = p1, p2 doesn’t satisfy prime path coverage! (why?)

Page 24: Lecture 2 White-box Testing and Structural Coverage

Computing Prime Paths

• One advantage is that the set of all prime paths can be computed by a simple dynamic programming algorithm

• See Amman and Offut for details• Then test cases can be derived manually

(heuristic: start from longest paths?) or automatically.

Page 25: Lecture 2 White-box Testing and Structural Coverage

2.7. Complete Path Coverage (CPC) Set TR contains all paths in G.

Infeasible if G has infinitely many paths

2.8. Specified Path Coverage (SPC) Set TR contains a set S of test paths, where S is supplied as a parameter.

Example. S contains paths that traverse everyloop free path p in G and every loop in G both 0and 1 times.

Page 26: Lecture 2 White-box Testing and Structural Coverage

Data Flow Criteria

• A definition of a variable v is any statement that writes to v in memory

• A use of v is any statement that reads v from memory.

• A path p = (n1, .., nk) from a node n1 to a node nk is def-clear for v if for each 1 < j < k node nj has no statements which write to v

Page 27: Lecture 2 White-box Testing and Structural Coverage

Definition/Use Paths (du-paths)

• A du-path w.r.t. v is a simple pathp = (n1, .., nk)

• Such that:1. A statement in n1 writes to v

2. Path p is def-clear for v3. A statement in nk reads v

• du(n,v) = set of all du-paths wrt v starting at n • du(m,n,v) = set of all du-paths wrt v starting at m

and ending at n

Page 28: Lecture 2 White-box Testing and Structural Coverage

Data flow coverage models

• 2.9. All-defs Coverage (ADC) For each def-path set S = du(n, v) the set TR contains at least one path d in S.

• 2.10 All-uses Coverage (AUC) For each def-pair set S = du(m,n,v) the set TR contains at least one path d in S.

• 2.11 All-du-paths Coverage (ADUPC) For each def-pair set S = du(m,n,v) the set TR contains every path d in S.

Page 29: Lecture 2 White-box Testing and Structural Coverage

n0

n1 n2

n3

n4 n5

write v

read v read v

All-defs = (n0, n1, n3, n4)

All-uses = (n0, n1, n3, n4) (n0, n1, n3, n5)

All-du-paths = (n0, n1, n3, n4) (n0, n1, n3, n5),

(n0, n2, n3, n4),(n0, n2, n3, n5)

Page 30: Lecture 2 White-box Testing and Structural Coverage

Logic Coverage

• Graph and data flow coverages force execution of certain paths (branches) through code.

• They don’t necessarily exercise different ways of taking the same branch.

• This number of ways may be finite and coverable.• Since a Boolean condition may contain many

Boolean and data (int, float, object) variables, we may wish to consider exercising the condition in different ways.

Page 31: Lecture 2 White-box Testing and Structural Coverage

Clauses and Predicates

• A clause is a Boolean valued expression with no Boolean valued sub-expression

• Examples p, myGuard, x==y, x<=y, x>y• A predicate is a Boolean combination of clauses

using &, I, !, • Let P be a set of predicates• For p P, let Cp be the set of all clauses in p.• For logic coverage, a test requirement tr is a

predicate on data values

Page 32: Lecture 2 White-box Testing and Structural Coverage

Logic Coverage Measures

• 3.12 Predicate Coverage (PC) For each predicate p P, the set TR contains two requirements: p is reached and evaluates to true, and p is reached and evaluates to false.

• 3.13 Clause Coverage (CC) For each predicate p P, and each clause c Cp the set TR contains two requirements: c is reached and evaluates to true, and c is reached and evaluates to false.

Page 33: Lecture 2 White-box Testing and Structural Coverage

Distributive vs. non-distributive

• Note: this definition of PC is non-distributive, ie. We don’t take all combinations of all predicate values.

• PC doesn’t necessarily achieve edge coverage!• Non-distributive PC – linear growth.• Distributive PC - exponential growth.

Page 34: Lecture 2 White-box Testing and Structural Coverage

Logic Coverage (cont)

• 3.14 Combinatorial Coverage (CoC) For each predicate p P, the set TR contains requirements so that p is reached, and the clauses c Cp evaluate to every possible combination of truth values.

• CoC is also called multiple condition coverage (MCC).

Page 35: Lecture 2 White-box Testing and Structural Coverage

Active Clause Coverage

• Clearly PC and CC grow linearly in the size of the Boolean predicate p

• CoC grows exponentially.• Can we find something more expressive but

still linear in-between?• Use notion of active variables which

determine or influence the overall predicate value

Page 36: Lecture 2 White-box Testing and Structural Coverage

Determination

Given a clause ci in a predicate p we say that ci determines p iff the other clauses in p can be assigned values so that changing the truth value of ci changes the truth value of p.

3.43 Active Clause Coverage (ACC) For each predicate p P and each clause c Cp which determines p, the set TR contains two requirements for c: c is reached and evaluates to true, and c is reached and evaluates to false.

Page 37: Lecture 2 White-box Testing and Structural Coverage

Ambiguity

• ACC can be seen as ambiguous.• Do the other clauses get the same assignment

when c is true and c is false, or can they have different assignments?

• Problem of masking, overlap or side-effects between clauses

• Consider e.g. c !b (c iff not b)• Here b cannot have the same assignment for

c = true and c = false

Page 38: Lecture 2 White-box Testing and Structural Coverage

Different Values

• 3.15 General Active Clause Coverage (GACC)For each predicate p P and each clause c Cp which determines p, the set TR contains two requirements for c: c is reached and evaluates to true, and c is reached and evaluates to false. The values chosen for the other clauses d Cp , d c, need not be the same in both cases.

Page 39: Lecture 2 White-box Testing and Structural Coverage

Same Values

3.15 Restricted Active Clause Coverage (RACC) For each predicate p P and each clause c Cp

which determines p, the set TR contains two requirements for c: c is reached and evaluates to true, and c is reached and evaluates to false. The values chosen for the other clauses d Cp , d c, must be the same in both cases.• So RACC is infeasible for c in c !b

Page 40: Lecture 2 White-box Testing and Structural Coverage

Correlation

• Another problem is that GACC does not imply PC.

• Let p be the predicate a b • Clearly a determines p (so does b)• If a = true then b = true so p = true• If a = false then b = false so p = true• Thus p never becomes false so PC fails!

Page 41: Lecture 2 White-box Testing and Structural Coverage

Handling Correlation

3.16 Correlated Active Clause Coverage (CACC)For each predicate p P and each clause c Cp

which determines p, the set TR contains two requirements for c: c is reached and evaluates to true, and c is reached and evaluates to false. The values chosen for the other clauses d Cp , d c, must cause p to be true in one case and false in the other.• Now CACC implies PC

Page 42: Lecture 2 White-box Testing and Structural Coverage

Safety Critical Testing: MCDC

• In DO-178B, the Federal Aviation Authority (FAA) has mandated a minimum level of logic coverage for level A (highest safety) avionic software.

• “Modified Condition Decision Coverage” (MCDC)• Has been some confusion about this definition• “Unique Cause MCDC” (original definition) is RACC• “Masking MCDC” (new definition) is CACC