White Box Testing (Introduction to)

55
Università degli Studi dell’Aquila L20: White Box/Structural Testing Henry Muccini DISIM, University of L’Aquila www.henrymuccini.com , [email protected]

description

An introductory lecture to white box testing

Transcript of White Box Testing (Introduction to)

Page 1: White Box Testing (Introduction to)

Università degli Studi dell’Aquila

L20: White Box/Structural Testing

Henry Muccini

DISIM, University of L’Aquila

www.henrymuccini.com, [email protected]

Page 2: White Box Testing (Introduction to)

The material in these slides may be freely reproduced and distributed, partially or totally, as far as an explicitreference or acknowledge to the material author ispreserved.

Partially based on material from Alex Orso, Mauro Pezze’, Michal Young, and Andreas Zeller

Page 3: White Box Testing (Introduction to)

Recap

Fault, Error, Failure chain

White Box Testing

Statement Coverage

Branch Coverage

AGENDA

Branch Coverage

Basic Condition Coverage

MC/CD Coverage

Page 4: White Box Testing (Introduction to)

Recap

�“Systematic” testing vs. “ad hoc”testing

→Repeatable, Measurable

�Software Testing Process

→Test Selection, Test Execution, Oracle, Adequacy

�Type of testing

→Unit, Integration, System

→Performance, Stress

→Regression

→WB, BB

Page 5: White Box Testing (Introduction to)

Fundamental Testing Questions

Test Case:How is test described / recorded?

Test Criteria: What should we test?

Test Oracle: Is the test correct?

Test Adequacy: Test Adequacy: How much is enough?

Test Process: Is testing complete and effective?

Test Driver: used to execute the system on the SUT

Stubs: to reproduce missing objects, for integration testing

Page 6: White Box Testing (Introduction to)

Fault, Error, Failure

Fault: anomaly in source code

→ may or may not produce a failure

→ a “bug”

Error: inappropriate development action

→ action introduced by a fault→ action introduced by a fault

→ Cause of a fault

Failure: incorrect/unexpected behavior or output

→ incident is the symptoms revealed by execution

→ failures are usually classified

Page 7: White Box Testing (Introduction to)

THE FAULT-FAILURE MODEL

Fault

must be activated

Error

sw internal stateis corrupted(latent or manifest)

Failure

affects thedelivered service

must be activated

is corrupted(latent or manifest)

A fault will manifest itself as a failure if all of the 3 following conditions hold:

1) EXECUTION: the faulty piece of code is executed

2) INFECTION: the internal state of the program is corrupted (error state)

3) PROPAGATION: the error propagates to program output

PIE MODEL (Voas, IEEE TSE Aug. 1992)

Page 8: White Box Testing (Introduction to)

WHITE BOX/STRUCTURAL TESTING

Page 9: White Box Testing (Introduction to)

How to test this

Java program?

Page 10: White Box Testing (Introduction to)

Selection of test suite is based on some elements in the code

Assumption: Executing the faulty “element” is a necessary condition for revealing a fault Source

Code

(Test) Inputs

There exist several examples

→ Control flow (statement, branch, basis path, path)

→ Condition (simple, multiple)

→ Loop

→ Dataflow (all-uses, all-du-paths)

→ Fault based (mutation)

Code

Output

Internal behavior

Page 11: White Box Testing (Introduction to)

Idea:

→ A program is a graph

→ A graph can be traversed, by using some inputs

→ A test suite is adequate if I guarantee certain

coveragecoverage

─ Metric: percentage of coverage achieved

Objective: Cover the software structure

Page 12: White Box Testing (Introduction to)

Control Flow

→ The partial order of statement execution, as defined by the

semantics of the language

Data Flow

→ The flow of values from definitions of a variable to its uses→ The flow of values from definitions of a variable to its uses

Graph representation of control flow and

data flow relationships

Page 13: White Box Testing (Introduction to)

1234567

function P return INTEGER isbegin

X, Y: INTEGER;READ(X); READ(Y);while (X > 10) loop

X := X – 10;exit when X = 10;7

89

101112131415

exit when X = 10;end loop;if (Y < 20 and then X mod 2 = 0) then

Y := Y + 20;else

Y := Y – 20;end if;return 2*X + Y;

end P;

Page 14: White Box Testing (Introduction to)

6 10

T T

7

TF

2,3,4 5 9´

12

14

T T

F

F 9 T

F

T

9a 9b

Page 15: White Box Testing (Introduction to)

printSum(int a, int b) {

int result = a + b;

if (result > 0)

Test this case

if (result > 0)

printcol(“red”, result);

else if (result < 0)

printcol(“blue”, result);

[else do nothing]

}

and this one

and this one!

Page 16: White Box Testing (Introduction to)

Defined in terms of

→ test requirements

Result in

→ test specifications→ test specifications

→ test cases

Selection VS adequacy/evaluation criteria

Page 17: White Box Testing (Introduction to)

test specifications

printSum(int a, int b) {

int result = a + b;

if (result > 0)

a + b > 0

if (result > 0)

printcol(“red”, result);

else if (result < 0)

printcol(“blue”, result);

[else do nothing]

}

a + b < 0

a + b == 0

Page 18: White Box Testing (Introduction to)

test cases

printSum(int a, int b) {

int result = a + b;

if (result > 0)

a == 3

b == 9

a == -5if (result > 0)

printcol(“red”, result);

else if (result < 0)

printcol(“blue”, result);

[else do nothing]

}

a == -5

b == -8

a == 0

b == 0

Page 19: White Box Testing (Introduction to)

Test requirements: Statements in program

Cstmts = (number of executed statements)

(number of statements)

Page 20: White Box Testing (Introduction to)

printSum(int a, int b) {

int result = a + b;

if (result > 0)if (result > 0)

printcol(“red”, result);

else if (result < 0)

printcol(“blue”, result);

}

Coverage: 0%

Page 21: White Box Testing (Introduction to)

printSum(int a, int b) {

int result = a + b;

if (result > 0)

a == 3

b == 9

if (result > 0)

printcol(“red”, result);

else if (result < 0)

printcol(“blue”, result);

}

Coverage: 71%

Page 22: White Box Testing (Introduction to)

printSum(int a, int b) {

int result = a + b;

if (result > 0)

a == 3

b == 9

a == -5

b == -8

if (result > 0)

printcol(“red”, result);

else if (result < 0)

printcol(“blue”, result);

}

Coverage: 100%

Page 23: White Box Testing (Introduction to)

Test hypothesis:

→By executing a path once, potential faults related to it will

be revealed (independently from the input)

Ideally:Ideally:

→To exhaustively cover all possible paths along the program

graph representation

Approximations to path coverage

→Coverage criteria

Page 24: White Box Testing (Introduction to)

Only about 1/3 of NASA statements were executed under test before software was released (Stucki 1973)

Microsoft reports 80-90% statement coverage

Boeing must get 100% statement coverage (feasible) for all softwarefor all software

Usually can about 85% coverage; 100% is harder

→ Unreachable code; dead code

→ Complex sequences

→ Not enough resources

Page 25: White Box Testing (Introduction to)
Page 26: White Box Testing (Introduction to)

printSum(int a, int b) {

int result = a + b;

if (result > 0)

a == 3

b == 9

a == -5

b == -8

if (result > 0)

printcol(“red”, result);

else if (result < 0)

printcol(“blue”, result);

}

Coverage: 100%

Page 27: White Box Testing (Introduction to)

printSum(int a, int b) {

int result = a + b;

if (result > 0)

a == 3

b == 9

a == -5

b == -8

if (result > 0)

printcol(“red”, result);

else if (result < 0)

printcol(“blue”, result);

[missing branch]

}

Coverage: 100%

Page 28: White Box Testing (Introduction to)

Test requirements: Branches in the program

Cbranches = (number of traversed branches)

(number of branches)

Page 29: White Box Testing (Introduction to)

printSum(int a, int b) {

int result = a + b;

if (result > 0)

a == 3

b == 9

a == -5

b == -8

if (result > 0)

printcol(“red”, result);

else if (result < 0)

printcol(“blue”, result);

[missing branch]

}

Coverage: ?

Page 30: White Box Testing (Introduction to)

printSum(int a, int b) {

int result = a + b;

if (result > 0)

a == 3

b == 9

a == -5

b == -8

if (result > 0)

printcol(“red”, result);

else if (result < 0)

printcol(“blue”, result);

[missing branch]

}

Coverage: 88%

Page 31: White Box Testing (Introduction to)

printSum(int a, int b) {

int result = a + b;

if (result > 0)

a == 3

b == 9

a == -5

b == -8

a == 0

b == 0

if (result > 0)

printcol(“red”, result);

else if (result < 0)

printcol(“blue”, result);

[missing branch]

}

Coverage: 100%

Page 32: White Box Testing (Introduction to)
Page 33: White Box Testing (Introduction to)

1. void main() {

2. float x, y;

3. read(x);

4. read(y);

33

entryentryentryentry

44

X=0 ||!(X=0

• Consider test cases {(x=5,y=5), (x=5, y=-5)}

4. read(y);

5. if(x==0)||(y>0)

6. y = y/x;

7. else x = y+2;

8. write(x);

9. write(y);

10.}

77

88

exitexitexitexit

X=0 ||

y>0

99

66

!(X=0

||y>0)

Page 34: White Box Testing (Introduction to)

1. void main() {

2. float x, y;

3. read(x);

4. read(y);

33

entryentryentryentry

44

X=0 ||!(X=0

• Consider test cases {(x=5,y=5), (x=5, y=-5)}

• The test suite is adequate for branch coverage, but does not reveal the fault at statement 6

4. read(y);

5. if(x==0)||(y>0)

6. y = y/x;

7. else x = y+2;

8. write(x);

9. write(y);

10.}

77

88

exitexitexitexit

X=0 ||

y>0

99

66

!(X=0

||y>0)at statement 6

• Predicate 5 can be true or false operating on only one condition

Basic condition coverageBasic condition coverage

Page 35: White Box Testing (Introduction to)
Page 36: White Box Testing (Introduction to)

Test requirements: Truth values assumed by

basic conditions

Cbc = (number of boolean values assumed by all basic conditions)

(number of boolean values of all basic conditions)(number of boolean values of all basic conditions)

x y

T T

F F

Page 37: White Box Testing (Introduction to)

1. void main() {

2. float x, y;

3. read(x);

4. read(y);

33

entryentryentryentry

44

X=0 ||!(X=0

• Consider test cases {(x=0,y=-5), (x=5, y=5)}

• The test suite is adequate for basic condition coverage

x y4. read(y);

5. if(x==0)||(y>0)

6. y = y/x;

7. else x = y+2;

8. write(x);

9. write(y);

10.}

77

88

exitexitexitexit

X=0 ||

y>0

99

66

!(X=0

||y>0)

• but is not adequate for branch coverage.

⇓Branch and condition Branch and condition

coveragecoverage

x y

T T

F F

Page 38: White Box Testing (Introduction to)

Test requirements: Branches and truth values

assumed by basic conditions

if ( ( a || b ) && c ) { … }

a b c Outcome

T T T T

F F F F

Page 39: White Box Testing (Introduction to)

Key idea: Test important combinations of

conditions, avoiding exponential blowup

A combination is “important” if each basic

condition is shown to independently affect the

outcome of each decision

Page 40: White Box Testing (Introduction to)

MC/DC criterion: For each basic condition C, there are

two test cases in which the truth values of all two test cases in which the truth values of all

conditions except C are the same, and the compound

condition as a whole evaluates to True for one of those

test cases and False for the other

Page 41: White Box Testing (Introduction to)

( a && b && c )Test case a b c outcome

1 True True True True

2 True True False False

3 True False True False3 True False True False

4 True False False False

5 False True True False

6 False True False False

7 False False True False

8 False False False False

Page 42: White Box Testing (Introduction to)

( a && b && c )Test case a b c outcome

1 True True True True

2 True True False False

3 True False True False3 True False True False

4 True False False False

5 False True True False

6 False True False False

7 False False True False

8 False False False False

Page 43: White Box Testing (Introduction to)

( a && b && c )Test case a b c outcome

1 True True True True

2 True True False False

3 True False True False

MC/DC criterion: For

each basic condition

C, there are two test

cases in which the

truth values of all

conditions except C 3 True False True False

4 True False False False

5 False True True False

6 False True False False

7 False False True False

8 False False False False

conditions except C

are the same, and the

compound condition

as a whole evaluates

to True for one of

those test cases and

False for the other

Page 44: White Box Testing (Introduction to)

( a && b && c )Test case a b c outcome

1 True True True True

2 True True False False

3 True False True False3 True False True False

4 True False False False

5 False True True False

6 False True False False

7 False False True False

8 False False False False

1 True True True True

5 False True True False

Page 45: White Box Testing (Introduction to)

( a && b && c )Test case a b c outcome

1 True True True True

2 True True False False

3 True False True False3 True False True False

4 True False False False

5 False True True False

6 False True False False

7 False False True False

8 False False False False

1 True True True True

5 False True True False

Page 46: White Box Testing (Introduction to)

( a && b && c )Test case a b c outcome

1 True True True True

2 True True False False

3 True False True False3 True False True False

4 True False False False

5 False True True False

6 False True False False

7 False False True False

8 False False False False

1 True True True True

5 False True True False

Page 47: White Box Testing (Introduction to)

( a && b && c )Test case a b c outcome

1 True True True True

2 True True False False

3 True False True False3 True False True False

4 True False False False

5 False True True False

6 False True False False

7 False False True False

8 False False False False

1 True True True True

5 False True True False

3 True False True False

Page 48: White Box Testing (Introduction to)

( a && b && c )Test case a b c outcome

1 True True True True

2 True True False False

3 True False True False3 True False True False

4 True False False False

5 False True True False

6 False True False False

7 False False True False

8 False False False False

1 True True True True

5 False True True False

3 True False True False

Page 49: White Box Testing (Introduction to)

( a && b && c )Test case a b c outcome

1 True True True True

2 True True False False

3 True False True False3 True False True False

4 True False False False

5 False True True False

6 False True False False

7 False False True False

8 False False False False

1 True True True True

5 False True True False

3 True False True False

Page 50: White Box Testing (Introduction to)

( a && b && c )Test case a b c outcome

1 True True True True

2 True True False False

3 True False True False3 True False True False

4 True False False False

5 False True True False

6 False True False False

7 False False True False

8 False False False False

1 True True True True

5 False True True False

3 True False True False

2 True True False False

Page 51: White Box Testing (Introduction to)

Tradeoff between number of required test

cases and thoroughness of the test

Required by both US and European quality

standards in aviation

Page 52: White Box Testing (Introduction to)
Page 53: White Box Testing (Introduction to)

Path coverage: cover each path in the program

Loop coverage (given n): cover all paths that contain at

most n iterations of any loop in the program

Data-flow coverage: cover data-flow relations in the

program (du pairs)program (du pairs)

Mutation coverage: cover (kill) mutated versions of the

program

Page 54: White Box Testing (Introduction to)

�Most widely used as adequacy criteria because fully

automatable!

�Not all graph paths correspond to actual program paths

→some paths could be not executable

→Conditions may not be satisfiable due to interdependent →Conditions may not be satisfiable due to interdependent

conditions

→Paths may not be executable due to interdependent decisions

�The testing output is strongly related to the testing

inputs

Page 55: White Box Testing (Introduction to)