manual testing program testing and analysis...empirical evidence studies on thebenefit of coverage...
TRANSCRIPT
1
Program Testing and Analysis:
Manual Testing
Prof. Dr. Michael Pradel
Software Lab, TU Darmstadt
Partly based on slides from Peter Muller, ETH Zurich
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
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
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
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
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
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;
}
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?
18
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
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
19
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.
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;
}}
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;
}}
20
14
Outline
� Overview
� Control flow testing� Statement coverage� Branch coverage� Path coverage� Loop coverage
� Data flow testing� DU-pair coverage
� Interpretation of coverage
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
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.
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)
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
21
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
22
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
23
Outline
� Overview
� Control flow testing� Statement coverage� Branch coverage� Path coverage� Loop coverage
� Data flow testing� DU-pair coverage
� Interpretation of coverage
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
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
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
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