manual testing program testing and analysis...empirical evidence studies on thebenefit of coverage...

30
1 Program Testing and Analysis: Manual Testing Prof. Dr. Michael Pradel Software Lab, TU Darmstadt Partly based on slides from Peter M¨ uller, ETH Zurich

Upload: others

Post on 08-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

1

Program Testing and Analysis:

Manual Testing

Prof. Dr. Michael Pradel

Software Lab, TU Darmstadt

Partly based on slides from Peter Muller, ETH Zurich

Page 2: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

2

Warm-up Quiz

var a, b;

var x = {};

x[a] = 23;

console.log(x[b]);

What does the following code print?

Nothing 23 undefined false

Page 3: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

2

Warm-up Quiz

var a, b;

var x = {};

x[a] = 23;

console.log(x[b]);

What does the following code print?

Nothing 23 undefined false

Page 4: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

2

Warm-up Quiz

var a, b;

var x = {};

x[a] = 23;

console.log(x[b]);

What does the following code print?

Nothing 23 undefined false

Have value undefined

Write and thenread ”undefined”property of x

Page 5: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

3

Outline (Manual Testing)

� Overview

� Control flow testing� Statement coverage� Branch coverage� Path coverage� Loop coverage

� Data flow testing� DU-pair coverage

� Interpretation of coverage

Page 6: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

4

Path Coverage

Idea:Test all possible paths through the CFG

covpath =Nb. of executed paths

Total nb. of paths

� A path is a sequence n1, .., nk where

� n1 = entry node

� nk = exit node

� There is an edge ni → ni+1 for all 1 ≤ i < k

Page 7: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

5

Path Coverage: Example

function fourPaths(a, b) {var x = 1;var y = 1;if (a)x = 0;

elsey = 0;

if (b)return 5 / x;

elsereturn 5 / y;

}

Page 8: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

6

Path Coverage: Discussion

function contains(a, x) {if (!a) return false;var found = false;for (var i = 0; i <= a.length; i++) {if (a[i] === x) {found = true;break;

}}return found;

}

Is path coverage the solution?

Page 9: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

18

Page 10: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

8

Path Coverage: Discussion (2)

� Leads to more thorough testing thanboth statement and branch coverage� Complete path coverage implies complete

statement coverage and branch coverage

� But: ”at least n% path coverage” impliesneither ”at least n% statement coverage” nor”at least n% branch coverage”

� Complete path coverage is notfeasible for loops: Unboundednumber of paths

Page 11: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

9

Loop Coverage

Idea: For each loop, test 0, 1, or morethan 1 consecutive iterations

Let nk be the number of loops with exactly k

consecutively executed iterations

covloop =n0 + n1 + n>1

Total nb. of loops ∗ 3

� Typically combined with other criteria, such asstatement or branch coverage

Page 12: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

19

Page 13: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

11

Measuring Coverage

� Coverage information is collectedwhile the test suite executes

� Typically implemented via codeinstrumentation or debug interface

� Count executed basic blocks, branches taken,etc.

Page 14: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

12

Measuring Coverage: Example

function fourPaths(a, b) {var x = 1;var y = 1;if (a) {x = 0;

} else {y = 0;

}if (b) {return 5 / x;

} else {return 5 / y;

}}

Page 15: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

12

Measuring Coverage: Example

function fourPaths(a, b) {var x = 1;var y = 1;if (a) {executedBranches[0]++; x = 0;

} else {executedBranches[1]++; y = 0;

}if (b) {executedBranches[2]++; return 5 / x;

} else {executedBranches[3]++; return 5 / y;

}}

Page 16: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

20

Page 17: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

14

Outline

� Overview

� Control flow testing� Statement coverage� Branch coverage� Path coverage� Loop coverage

� Data flow testing� DU-pair coverage

� Interpretation of coverage

Page 18: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

15

Data Flow Testing

� Problem: Testing all paths is notfeasible

� Number grows exponentially in the number ofbranches

� Loops

� Idea: Test those paths where acomputation in one part of the pathaffects the computation of anotherpart

Page 19: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

16

Variable Definition and Use

� A variable definition for a variable v isa basic block that assigns to v

� v can be a local or global variable, parameter,or property

� A variable use for a variable v is abasic block that reads the value of v

� In conditions, computations, output, etc.

Page 20: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

17

Definition-Clear Paths

A definition-clear path for a variable v isa path n1, .., nk in the CFG such that

� n1 is a variable definition for v

� nk is a variable use for v

� No ni (1 < i ≤ k) is a variable definition for v� nk may be a variable definition if each assignment

to v occurs after a use

Note: Def-clear paths do not go from entry to exit (incontrast to our earlier definition of paths)

Page 21: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

18

Definition-Use Pair

A definition-use pair (DU-pair) for avariable v is a pair of nodes (d, u) suchthat there is a definition-clear path d, .., u

in the CFG

Page 22: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

21

Page 23: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

20

DU-Pairs Coverage

Idea:Test all paths that provide a value for avariable use

covDU =Nb. of executed DU -pairsTotal nb. of DU -pairs

Page 24: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

22

Page 25: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

22

DU-Pair Coverage: Discussion

� Complements control flow testing� Use both: Choose tests that maximize branch

and DU-pair coverage

� As with path coverage, not allDU-pairs are feasible� Static analysis overapproximates data flow

� Complete DU-pair coverage does notimply that all bugs are detected

Page 26: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

23

Outline

� Overview

� Control flow testing� Statement coverage� Branch coverage� Path coverage� Loop coverage

� Data flow testing� DU-pair coverage

� Interpretation of coverage

Page 27: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

24

Interpreting Coverage

� High coverage does not imply thatcode is well tested

� But: Low coverage means that code isnot well tested

� Do not blindly increase coverage butdevelop test suites that are effective atdetecting bugs

Page 28: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

25

Empirical Evidence

� Studies on the benefit of coveragemetricsE.g., Andrews et al.: ”Using Mutation Analysis for Assessingand Comparing Testing Coverage Criteria”, 2006

� Approach� Seed bugs into code� Develop test suites that satisfy various

coverage criteria� Measure how many of the seeded bugs are

found

Page 29: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

26

Empirical Evidence (2)

� The higher the coverage, the morebugs are detected

� Tests written with coverage criteria inmind are more effective than randomtests (for the same test suite size)

� Test suite size grows exponentially inthe achieved coverage

Page 30: Manual Testing Program Testing and Analysis...Empirical Evidence Studies on thebenefit of coverage metrics E.g., Andrews et al.: ”Using Mutation Analysis for Assessing and Comparing

27

Summary: Manual Testing

Black box testing

� Exhaustive testing� Random testing� Functional testing

White box testing� Structural testing

� Control flow-based coverage criteria:Statements, branches, paths, loops

� Data flow-based coverage criterion: DU-pairs