TDDD04: White box testing Lena Buffoni lena.buffoni@liu.se 2 White - - PowerPoint PPT Presentation

tddd04 white box testing
SMART_READER_LITE
LIVE PREVIEW

TDDD04: White box testing Lena Buffoni lena.buffoni@liu.se 2 White - - PowerPoint PPT Presentation

TDDD04: White box testing Lena Buffoni lena.buffoni@liu.se 2 White box testing - outline Control flow coverage Statement, decision and condition coverage Condition/decision coverage Multiple condition coverage Modified


slide-1
SLIDE 1

TDDD04: White box testing

Lena Buffoni lena.buffoni@liu.se

slide-2
SLIDE 2

White box testing - outline

2

  • Control flow coverage

– Statement, decision and condition coverage – Condition/decision coverage – Multiple condition coverage – Modified condition/decision coverage – Loop testing – Basis path testing (a.k.a. structured testing)

  • Program complexity
  • Mutation testing
slide-3
SLIDE 3

White box testing

3

  • Analyze SUT
  • Identify paths to execute
  • Choose inputs to trigger those paths and determine

expected outputs

  • Run tests
  • Compare actual outputs to expected
  • Can be applied at all levels: unit, integration and

system

slide-4
SLIDE 4

Limitations

4

  • Testing all paths is complicated/impossible
  • Not data sensitive

– eg: p=q/r;

  • Non-existent paths cannot be discovered
  • The tester must have programming skills
slide-5
SLIDE 5

Control flow testing

5

  • Based on the flow of control in the program
  • Logical decisions
  • Loops
  • Execution paths
  • Coverage metrics
  • Measure of how complete the test cases are
  • Not the same as how good they are!
slide-6
SLIDE 6

Control flow graphs (CFGs)

6

  • Definition: a control flow graph is a graph

representation of a program in which the vertices (nodes) represent basic blocks of the program, and edges represent transfer of execution between basic blocks.

  • A basic block is a region in the program with a

single entry point and a single exit point. This means that all jump targets start new basic blocks, and all jumps terminate basic blocks.

slide-7
SLIDE 7

CFG Notation

7

Decision point Process block Junction point Sequence Until While If Case

slide-8
SLIDE 8

Levels of code coverage

8

  • Statement/Line/Basic block/Segment Coverage
  • Decision (Branch) Coverage
  • Condition Coverage
  • Multiple Condition Coverage
  • Decision/Condition Coverage
  • Loop Coverage
  • Path Coverage
slide-9
SLIDE 9

Statement coverage

9

// Return smallest value int min(int y, int x) { if (y < x) y = x; return y; } Test case x y expected actual Start End Y<X Y=X no yes Return Y Write test cases to execute each statement at least once

slide-10
SLIDE 10

Statement coverage

10

// Return smallest value int min(int y, int x) { if (y < x) y = x; return y; } Test case x y expected actual 1 1 1 Start End Y<X Y=X no yes Return Y 100% statement coverage

slide-11
SLIDE 11

What is wrong with line (statement) coverage?

Steve Cornett (Bullseye testing technology)

11

  • Software developers and testers commonly use line coverage

because of its simplicity and availability in object code instrumentation technology.

  • Of all the structural coverage criteria, line coverage is the

weakest, indicating the fewest number of test cases.

  • Bugs can easily occur in the cases that line coverage cannot see.
  • The most significant shortcoming of line coverage is that it fails

to measure whether you test simple if statements with a false decision outcome. Experts generally recommend to only use line coverage if nothing else is available. Any other measure is better.

slide-12
SLIDE 12

Decision coverage

12

// Return smallest value int min(int y, int x) { if (y < x) y = x; return y; } Test case x y expected actual Start End Y<X Y=X no yes Return Y Write test cases to execute each edge in the CFG at least once

slide-13
SLIDE 13

Decision coverage

13

// Return smallest value int min(int y, int x) { if (y < x) y = x; return y; } Test case x y expected actual 1 (T) 1 1 1 1 2 (F) 1 1 Start End Y<X Y=X no yes Return Y Write test cases to execute each edge in the CFG at least once 100% branch coverage

slide-14
SLIDE 14

Condition coverage

14

// Return true if (x,y) is in the // lower left size x size grid sector. boolean is_ll(int x, int y, int size) { boolean ret if (x < size && y < size) ret = false; else ret = false; return ret; } Start End x < size && y < size ret= false no yes Return ret Write test cases so that each condition in each decision is both true and false ret= false Test case x y size expected actual

slide-15
SLIDE 15

Condition coverage

15

// Return true if (x,y) is in the // lower left size x size grid sector. boolean is_ll(int x, int y, int size) { boolean ret if (x < size && y < size) ret = false; else ret = false; return ret; } Test case x y size expected actual 1(TF) 3 7 5 false false 2(FT) 7 3 5 false false Start End x < size && y < size ret= false no yes Return ret Write test cases so that each condition in each decision is both true and false No 100% branch coverage ret= false

slide-16
SLIDE 16

Multiple condition coverage (MCC)

16

// Return true if (x,y) is in the // lower left size x size grid sector. boolean is_ll(int x, int y, int size) { boolean ret if (x < size && y < size) ret = false; else ret = false; return ret; } Start End x < size && y < size ret= false no yes Return ret Write test cases so that all combinations of conditions are executed in each decision ret= false Test case x y size expected actual 1(TF) 2(FT) 3(TT) 4(FF)

slide-17
SLIDE 17

Problems with MCC

17

Consider the following simplified rule for insurance coverage if ((age >= 17 and age + duration <= 80 and issued <= today – timedelta(year=1) and country == ‘UK’ and accidents <= 5 and convictions <= 3 and and (owner == applicant or owner == spouse or owner == partner) and not modified and (driver == applicant or driver == spouse or driver == partner) and milage <= 50000 and value <= 50000 and not (job == entertainment or job == fashion

  • r job == sports or job == fs or job == diplomatic
  • r job == scrap or job == general_dealer or job == minicab)

and ((use == social or use == pleasure or use == commuting) and (job == motor_trade or job == retired or job == house

  • r job == driver or job == none or job == student
  • r job == unemployed)):
  • How many test cases? Are all test cases even possible?
slide-18
SLIDE 18

Condition/decision coverage

18

// Return true if (x,y) is in the // lower left size x size grid sector. boolean is_ll(int x, int y, int size) { boolean ret if (x < size && y < size) ret = false; else ret = false; return ret; } Start End x < size && y < size ret= false no yes Return ret Write test cases so that achieve both condition and decision coverage ret= false Test case x y size expected actual

slide-19
SLIDE 19

Condition/decision coverage

19

// Return true if (x,y) is in the // lower left size x size grid sector. boolean is_ll(int x, int y, int size) { boolean ret if (x < size && y < size) ret = false; else ret = false; return ret; } Start End x < size && y < size ret= false no yes Return ret Write test cases so that achieve both condition and decision coverage ret= false Test case x y size expected actual 1(TT) 2(FF)

slide-20
SLIDE 20

Modified condition/decision coverage (MCDC)

20

// Return true if (x,y) is in the // lower left size x size grid sector. boolean is_ll(int x, int y, int size) { boolean ret if (x < size && y < size) ret = false; else ret = false; return ret; } Start End x < size && y < size ret= false no yes Return ret Write test cases so that achieve both condition and decision coverage AND each condition in a decision has been shown to independently affect that decision’s

  • utcome

ret= false Test case x y size expected actual

slide-21
SLIDE 21

Modified condition/decision coverage (MCDC)

21

// Return true if (x,y) is in the // lower left size x size grid sector. boolean is_ll(int x, int y, int size) { boolean ret if (x < size && y < size) ret = false; else ret = false; return ret; } Start End x < size && y < size ret= false no yes Return ret Write test cases so that achieve both condition and decision coverage AND each condition in a decision has been shown to independently affect that decision’s

  • utcome

ret= false Test case x y size expected actual 1(TF) 2(FT) 3(TT)

slide-22
SLIDE 22

Loop coverage

22

Simple loop Concatenated loops Nested loops Knotted loops

slide-23
SLIDE 23

Loop coverage

23

  • Minimum number
  • Minimum number + 1
  • Skip the loop entirely (unless covered above)
  • One pass through the loop (unless covered above)
  • Two passes through the loop (unless covered above)
  • Maximum expected – 1
  • Maximum expected
  • One less than minimum (if possible)
  • One more than maximum (if possible)
slide-24
SLIDE 24

Path testing

24

  • A path is a sequence of branches, or conditions
  • A path corresponds to a test case, or a set of inputs
  • Bugs are often sensitive to branches and conditions
  • All-paths coverage: cover all possible paths through a

program – Not possible in the general case (e.g. loops) – Approximations must be used: statement, branch, MCC, MCDC, loop...

  • Basis path coverage: cover all independent paths

– Idea behind the structured testing method – Independent paths are limited in number

slide-25
SLIDE 25

Independent paths

25

Finding independent paths

  • An independent path is any path through the program that

introduces at least one new set of processing statements

  • r a new condition. When stated in terms of a flow graph, an

independent path must move along at least one edge that has not been traversed before the path is defined. Basis path set

  • A set of linearly independent paths through the program
  • Any path through the program can be formed as a linear

combination of elements in the basis set

  • Size equals the cyclomatic complexity of the control flow

graph

slide-26
SLIDE 26

Cyclomatic complexity

26

Cyclomatic Complexity is a software metric that provides a quantitative measure of the logical complexity of a program. When used in context of the basis path testing method, the value computed for cyclomatic complexity defines the number of independent paths in the basis set of a program and provides us with an upper bound for the number of tests that must be conducted to ensure that all statements have been executed at least once. Developed by John McCabe in 1976 as a software complexity metric

slide-27
SLIDE 27

Computation of cyclomatic complexity

27

  • Cyclomatic complexity V(G) for a control flow graph

G, is defined as:

  • V(G) = E – N + 2P

E: number of edges N: number of nodes P: number of disconnected partitions of the graph

  • Simplifications
  • No decisions: V(G) = 1
  • Only b binary decisions: V(G) = b + 1
slide-28
SLIDE 28

Calculate cyclomatic complexity

28

slide-29
SLIDE 29

Calculate cyclomatic complexity

29

E=1 , N=2 , P=1 V =1 – 2 + 2*1= 1 E=4 , N=4 , P=1 V = 4 – 4 + 2 * 1= 2 E=2 , N=4 , P=2 V = 2 – 4 + 2*2 = 2 E=12 , N=7 , P=1 V = 12 – 7 + 2*1= 7 E=13, N=11 , P=3 V = 13 – 11 + 2*3 = 8

slide-30
SLIDE 30

Basis Path Testing

30

  • Derive the control flow graph from the software

module

  • Compute the cyclomatic complexity of the resultant

flow graph

  • Determine a basis set of linearly independent

paths

  • Create a test case for each basis path
  • Execute these tests
slide-31
SLIDE 31

Basis path testing: cyclomatic complexity

31

A B C D E G H K F I J L O P R M N Q S V(G) = E – N + 2P E= N= P= V(G) = V(G) = b + 1 b= V(G) =

slide-32
SLIDE 32

Basis path testing: cyclomatic complexity

32

A B C D E G H K F I J L O P R M N Q S V(G) = E – N + 2P E = 24 N = 19 P=1 V(G) = 24 – 19 + 2*1 = 7 V(G) = b + 1 b= 6 V(G) = 6 + 1 = 7

slide-33
SLIDE 33

Determine a basis set of linearly independent paths McCabe’s baseline method

33

1. Pick a “baseline” path. This path should be a “normalcase” program execution. McCabe advises: choose a path with as many decisions as possible. 2. To choose the next path, change the outcome of the first decision along the baseline path while keeping the maximum number of other decisions the same as the baseline path. 3. To generate the third path, begin again with the base line but vary the second decision rather than the first. 4. Repeat step 3 for other paths until all decisions along baseline path have been flipped. 5. Now proceed to the second path(from step 2), flipping its decisions, one by one until the basis path set is completed.

slide-34
SLIDE 34

Create the basis set: paths 1-2

34

A B C D E G H K F I J L O P R M N Q S D1 D2 D3 D4 D5 D6 A B C D E G H K F I J L O P R M N Q S D1

slide-35
SLIDE 35

Create the basis set: paths 1-2

35

A B C D E G H K F I J L O P R M N Q S D1 D2 D3 D4 D5 D6 A B C D E G H K F I J L O P R M N Q S D1

slide-36
SLIDE 36

36

A B C D E G H K F I J L O P R M N Q S D1 D2 D3 D4 D5 D6 A B C D E G H K F I J L O P R M N Q S D2

Baseline + second decision (path 3)

slide-37
SLIDE 37

37

A B C D E G H K F I J L O P R M N Q S D1 D2 D3 D4 D5 D6 A B C D E G H K F I J L O P R M N Q S D2

Baseline + second decision (path 3)

slide-38
SLIDE 38

38

A B C D E G H K F I J L O P R M N Q S D1 D2 D3 D4 D5 D6 A B C D E G H K F I J L O P R M N Q S D3

Baseline + third decision (path 4)

slide-39
SLIDE 39

39

A B C D E G H K F I J L O P R M N Q S D1 D2 D3 D4 D5 D6 A B C D E G H K F I J L O P R M N Q S D3

Baseline + third decision (path 4)

slide-40
SLIDE 40

40

A B C D E G H K F I J L O P R M N Q S D1 D2 D3 D4 D5 D6 A B C D E G H K F I J L O P R M N Q S D4

Baseline + fourth decision (path 5)

slide-41
SLIDE 41

41

A B C D E G H K F I J L O P R M N Q S D1 D2 D3 D4 D5 D6 A B C D E G H K F I J L O P R M N Q S D4

Baseline + fourth decision (path 5)

slide-42
SLIDE 42

42

D2 D3 D4 D5 D6 A B C D E G H K F I J L O P R M N Q S

Third path + fifth decision (path 6)

D1 A B C D E G H K F I J L O P R M N Q S D5

slide-43
SLIDE 43

43

D2 D3 D4 D5 D6 A B C D E G H K F I J L O P R M N Q S

Third path + fifth decision (path 6)

D1 A B C D E G H K F I J L O P R M N Q S D5

slide-44
SLIDE 44

44

D2 D3 D4 D5 D6 A B C D E G H K F I J L O P R M N Q S

Third path + sixth decision (path 7)

D1 A B C D E G H K F I J L O P R M N Q S D6

slide-45
SLIDE 45

45

D2 D3 D4 D5 D6 A B C D E G H K F I J L O P R M N Q S

Third path + sixth decision (path 7)

D1 A B C D E G H K F I J L O P R M N Q S D6

slide-46
SLIDE 46

Set of basis paths

46

A B C D E G H K F I J L O P R M N Q S

  • 1. ABDEGKMQS
  • 2. ACDEGKMQS
  • 3. ABDFILORS
  • 4. ABDEHKMQS
  • 5. ABDEGKNQS
  • 6. ACDFJLORS
  • 7. ACDFILPRS
slide-47
SLIDE 47

Observation

47

  • Basis path testing calls for the creation of a test

case for each of these

  • paths.
  • This set of test cases will guarantee both

statement and branch

  • coverage.
  • Using a path/edge indicence matrix, independent

paths becomes linearly independent vectors, spanning the vector space of possible paths

slide-48
SLIDE 48

Why coverage?

48

“We found that there is a low to moderate correlation between coverage and effectiveness when the number

  • f test cases in the suite is controlled for. In addition, we

found that stronger forms of coverage do not provide greater insight into the effectiveness of the suite. Our results suggest that coverage, while useful for identifying under-tested parts of a program, should not be used as a quality target because it is not a good indicator of test suite effectiveness.” [1]

[1] L. Inozemtseva and R. Holmes. Coverage is not strongly correlated with test suite effectiveness. In Proceedings of the 36th International Conference on Software Engineering, ICSE 2014, pages 435–445, New York, NY, USA, 2014. ACM.

slide-49
SLIDE 49

Why is Coverage insufficient?

49

slide-50
SLIDE 50

Remember this example?

50

int scale(int j) { j = j - 1; // should be j = j + 1 j = j / 30000; return j;} Would coverage testing find this bug?

slide-51
SLIDE 51

Example with branch coverage:

51

public static String foo(boolean b) { if ( b ) { performVitallyImportantBusinessFunction(); return "OK"; } return "FAIL"; } @Test public void shouldFailWhenGivenFalse() { assertEquals("FAIL", foo(false)); } @Test public void shouldBeOkWhenGivenTrue() { assertEquals("OK", foo(true)); }

slide-52
SLIDE 52

Mutation testing

52

SUT Mutate the program Mutant n Mutant 2 Mutant 1 Execute Test Suite Test suite Compare results Mutant 1 Mutant 1 Mutant 2 killed survived

slide-53
SLIDE 53

Mutation testing

53

  • Mutation operators
  • Conditionals Boundary Mutator
  • Negate Conditionals Mutator
  • Remove Conditionals Mutator
  • Math Mutator
  • Increments Mutator
  • Invert Negatives Mutator
  • Inline Constant Mutator
  • Return Values Mutator
  • Void Method Calls Mutator
  • Non Void Method Calls Mutator
slide-54
SLIDE 54

Conditionals Boundary Mutator

54

Original conditional Mutated conditional < <= <= < > >= >= > For example if (a < b) { // do something } ‘ will be mutated to if (a <= b) { // do something }

slide-55
SLIDE 55

Equivalent mutations

55

  • The resulting mutant behaves the same as the
  • riginal
  • The difference in behavior is outside the scope of

testing

slide-56
SLIDE 56

Automating white-box testing

56

A B C D E G H K F I J L O P R M N Q S

  • Creating a representation –

abstract syntax tree (AST)

  • Generating a symbolic

execution model

  • Putting the right constraints
  • n the inputs
slide-57
SLIDE 57

57

Automation of white-box testing: Java Pathfinder Lab on symbolic execution

slide-58
SLIDE 58

Next lecture:

58

  • Guest lecture from Ahmed Rezne on model checking
slide-59
SLIDE 59

Questions?

Thank you for your attention