cse 2123: testing & test...

82
1 CSE 2123: Testing & Test Cases Jeremy Morris

Upload: others

Post on 26-Jan-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

1

CSE 2123: Testing & Test Cases

Jeremy Morris

Page 2: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Software Lifecyle – Waterfall Model

2

Requirements Specification &

Implement (Code)

Design

Testing

Maintenance/Fix

Page 3: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Test Driven Model: Discover Design

3

Requirements Specification &

Implement (Code)

Test Plan/Testing

Maintenance/Fix

Find Design

Page 4: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Test Driven Model: Test/Refine Design

4

Requirements Specification &

Implement (Code)

Test Plan/Testing

Maintenance/Fix

Candidate Design

Page 5: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Test Driven Development (TDD)

TDD Features/Benefits Test/code one task (method) at a time A test plan organizes problem solving Systematic process to write your code

Write code incrementally (simple tests & code first) Incrementally find (discover) a solution

As a result you will have: Working code A suite a tests that “show” it works An easy starting point to fix/maintain code

5

Page 6: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Test Plans

Test A particular input value The correct output value (expected value)

Test Plan: A complete suite of tests Categorize all input possibilities Identify boundary cases

6

Page 7: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

Problem specification: Find the minimum number in a list of positive

integers Method header (signature): public static int listMin(int[] arr)

7

Page 8: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Test Plan: listMin

Categorize inputs by array size Array size 0 Array size 1:

Array size 2:

8

m

m

m

Page 9: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Test Plan: listMin

Categorize inputs by array size Array size 3:

9

m

m

m

Page 10: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Test Plan: listMin

Categorize inputs by array size Array size 4:

10

m

m

m

m

Page 11: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Test Plan: listMin

Test Plan

11

Input Expected Value

{} -1 {1} 1 {2} 2

{1, 2} 1 {2, 1} 1

{1, 2, 3} 1 {2, 1, 3} 1 {2, 3, 1} 1

{1, 2, 3, 4} 1 {2, 1, 3, 4} 1 {2, 3, 1, 4} 1 {2, 3, 4, 1} 1

Page 12: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Process 1. Run code with next input (simplest test case) that might

make the code fail 2. Compare the expected output value to the actual output

value An earlier test fails

Backtrack to earliest failing test Make simplest change to code to pass the test Go back to step 2

Expected value does not match actual output Make simplest change to code to pass the test Go back to step 2

Expected value matches actual output Go back to step 1

12

Page 13: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Process

Black Box Testing

If the actual output does not match our expectations, open the box to see what is happening inside (white box testing)

13

Method Input Actual Output

Page 14: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

Code initially: int listMin (int [] arr)

{

return 0;

}

(Assume we call all previous tests in our example)

Call: listMin on {} Expected: -1 Actual: 0

14

Input Expected Value

Pass

{} -1 No {1} 1 - {2} 2

{1, 2} 1 - {2, 1} 1 -

{1, 2, 3} 1 - {2, 1, 3} 1 - {2, 3, 1} 1 -

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

?

Page 15: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

Code initially: int listMin (int [] arr)

{

return 0;

}

(Assume we call all previous tests in our example)

Call: listMin on {} Expected: -1 Actual: 0

15

Input Expected Value

Pass

{} -1 No {1} 1 - {2} 2

{1, 2} 1 - {2, 1} 1 -

{1, 2, 3} 1 - {2, 1, 3} 1 - {2, 3, 1} 1 -

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

Page 16: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

16

int listMin (int [] arr)

{

return -1;

}

Input Expected Value

Pass

{} -1 Yes {1} 1 - {2} 2

{1, 2} 1 - {2, 1} 1 -

{1, 2, 3} 1 - {2, 1, 3} 1 - {2, 3, 1} 1 -

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

Page 17: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

17

int listMin (int [] arr)

{

return -1;

}

Call: listMin on {}

Expected: -1 Actual: -1

Input Expected Value

Pass

{} -1 Yes {1} 1 - {2} 2

{1, 2} 1 - {2, 1} 1 -

{1, 2, 3} 1 - {2, 1, 3} 1 - {2, 3, 1} 1 -

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

?

Page 18: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

18

int listMin (int [] arr)

{

return -1;

}

Call: listMin on {1}

Expected: 1 Actual: -1

Input Expected Value

Pass

{} -1 Yes {1} 1 No {2} 2

{1, 2} 1 - {2, 1} 1 -

{1, 2, 3} 1 - {2, 1, 3} 1 - {2, 3, 1} 1 -

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

?

Page 19: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

19

int listMin (int [] arr)

{

return -1;

}

Call: listMin on {1}

Expected: 1 Actual: -1

Input Expected Value

Pass

{} -1 Yes {1} 1 No {2} 2

{1, 2} 1 - {2, 1} 1 -

{1, 2, 3} 1 - {2, 1, 3} 1 - {2, 3, 1} 1 -

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

Page 20: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

20

int listMin (int [] arr)

{

if (arr.length == 0)

return -1;

return arr[0];

}

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2

{1, 2} 1 - {2, 1} 1 -

{1, 2, 3} 1 - {2, 1, 3} 1 - {2, 3, 1} 1 -

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

Page 21: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

21

int listMin (int [] arr)

{

if (arr.length == 0)

return -1;

return arr[0];

}

Call: listMin on {1}

Expected: 1 Actual: 1

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2

{1, 2} 1 - {2, 1} 1 -

{1, 2, 3} 1 - {2, 1, 3} 1 - {2, 3, 1} 1 -

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

?

Page 22: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

22

int listMin (int [] arr)

{

if (arr.length == 0)

return -1;

return arr[0];

}

Call: listMin on {2}

Expected: 2 Actual: 2

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2 Yes

{1, 2} 1 - {2, 1} 1 -

{1, 2, 3} 1 - {2, 1, 3} 1 - {2, 3, 1} 1 -

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

?

Page 23: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

23

int listMin (int [] arr)

{

if (arr.length == 0)

return -1;

return arr[0];

}

Call: listMin on {1, 2}

Expected: 1 Actual: 1

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2 Yes

{1, 2} 1 Yes {2, 1} 1 -

{1, 2, 3} 1 - {2, 1, 3} 1 - {2, 3, 1} 1 -

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

?

Page 24: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

24

int listMin (int [] arr)

{

if (arr.length == 0)

return -1;

return arr[0];

}

Call: listMin on {2, 1}

Expected: 1 Actual: 2

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2 Yes

{1, 2} 1 Yes {2, 1} 1 No

{1, 2, 3} 1 - {2, 1, 3} 1 - {2, 3, 1} 1 -

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

?

Page 25: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

25

int listMin (int [] arr)

{

if (arr.length == 0)

return -1;

return arr[0];

}

Call: listMin on {2, 1}

Expected: 1 Actual: 2

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2 Yes

{1, 2} 1 Yes {2, 1} 1 No

{1, 2, 3} 1 - {2, 1, 3} 1 - {2, 3, 1} 1 -

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

Page 26: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

26

int listMin (int [] arr)

{

if (arr.length == 0)

return -1;

if (arr.length == 1)

return arr[0];

if (arr[0] < arr[1])

return arr[0];

return arr[1];

}

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2 Yes

{1, 2} 1 Yes {2, 1} 1 Yes

{1, 2, 3} 1 - {2, 1, 3} 1 - {2, 3, 1} 1 -

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

Page 27: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

27

int listMin (int [] arr) { if (arr.length == 0) return -1; if (arr.length == 1) return arr[0]; if (arr[0] < arr[1]) return arr[0]; return arr[1]; }

Call: listMin on {2, 1}

Expected: 1 Actual: 1

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2 Yes

{1, 2} 1 Yes {2, 1} 1 Yes

{1, 2, 3} 1 - {2, 1, 3} 1 - {2, 3, 1} 1 -

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

?

Page 28: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

28

int listMin (int [] arr) { if (arr.length == 0) return -1; if (arr.length == 1) return arr[0]; if (arr[0] < arr[1]) return arr[0]; return arr[1]; }

Call: listMin on {1, 2, 3}

Expected: 1 Actual: 1

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2 Yes

{1, 2} 1 Yes {2, 1} 1 Yes

{1, 2, 3} 1 Yes {2, 1, 3} 1 - {2, 3, 1} 1 -

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

?

Page 29: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

29

int listMin (int [] arr) { if (arr.length == 0) return -1; if (arr.length == 1) return arr[0]; if (arr[0] < arr[1]) return arr[0]; return arr[1]; }

Call: listMin on {2, 1, 3}

Expected: 1 Actual: 1

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2 Yes

{1, 2} 1 Yes {2, 1} 1 Yes

{1, 2, 3} 1 Yes {2, 1, 3} 1 Yes {2, 3, 1} 1 -

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

?

Page 30: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

30

int listMin (int [] arr) { if (arr.length == 0) return -1; if (arr.length == 1) return arr[0]; if (arr[0] < arr[1]) return arr[0]; return arr[1]; }

Call: listMin on {2, 3, 1}

Expected: 1 Actual: 2

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2 Yes

{1, 2} 1 Yes {2, 1} 1 Yes

{1, 2, 3} 1 Yes {2, 1, 3} 1 Yes {2, 3, 1} 1 No

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

?

Page 31: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

31

int listMin (int [] arr)

{

if (arr.length == 0)

return -1;

if (arr.length == 1)

return arr[0];

if (arr[0] < arr[1])

return arr[0];

return arr[1];

}

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2 Yes

{1, 2} 1 Yes {2, 1} 1 Yes

{1, 2, 3} 1 Yes {2, 1, 3} 1 Yes {2, 3, 1} 1 No

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

Page 32: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

32

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2 Yes

{1, 2} 1 Yes {2, 1} 1 Yes

{1, 2, 3} 1 Yes {2, 1, 3} 1 Yes {2, 3, 1} 1 Yes

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

int listMin (int [] arr) { if (arr.length == 0) return -1; int min = arr[0]; if (arr.length > 1) if (arr[1] < min) min = arr[1]; if (arr.length > 2) if (arr[2] < min) min = arr[2]; return min; }

Page 33: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

33

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2 Yes

{1, 2} 1 Yes {2, 1} 1 Yes

{1, 2, 3} 1 Yes {2, 1, 3} 1 Yes {2, 3, 1} 1 Yes

{1, 2, 3, 4} 1 - {2, 1, 3, 4} 1 - {2, 3, 1, 4} 1 - {2, 3, 4, 1} 1 -

int listMin (int [] arr) { if (arr.length == 0) return -1; int min = arr[0]; if (arr.length > 1) if (arr[1] < min) min = arr[1]; if (arr.length > 2) if (arr[2] < min) min = arr[2]; return min; } Call: listMin on {2, 3, 1}

Expected: 1 Actual: 1

?

Page 34: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

34

int listMin (int [] arr) { if (arr.length == 0) return -1; int min = arr[0]; if (arr.length > 1) if (arr[1] < min) min = arr[1]; if (arr.length > 2) if (arr[2] < min) min = arr[2]; return min; } Call: listMin on {1, 2, 3, 4} Call: listMin on {2, 1, 3, 4} Call: listMin on {2, 3, 1, 4} Call: listMin on {2, 3, 4, 1}

Expected: 1 Actual: 2

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2 Yes

{1, 2} 1 Yes {2, 1} 1 Yes

{1, 2, 3} 1 Yes {2, 1, 3} 1 Yes {2, 3, 1} 1 Yes

{1, 2, 3, 4} 1 Yes {2, 1, 3, 4} 1 Yes {2, 3, 1, 4} 1 Yes {2, 3, 4, 1} 1 No

?

Page 35: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

35

int listMin (int [] arr) { if (arr.length == 0) return -1; int min = arr[0]; if (arr.length > 1) if (arr[1] < min) min = arr[1]; if (arr.length > 2) if (arr[2] < min) min = arr[2]; return min; } Call: listMin on {2, 3, 4, 1}

Expected: 1 Actual: 2

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2 Yes

{1, 2} 1 Yes {2, 1} 1 Yes

{1, 2, 3} 1 Yes {2, 1, 3} 1 Yes {2, 3, 1} 1 Yes

{1, 2, 3, 4} 1 Yes {2, 1, 3, 4} 1 Yes {2, 3, 1, 4} 1 Yes {2, 3, 4, 1} 1 No

Page 36: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

36

int listMin (int [] arr) { if (arr.length == 0) return -1; int min = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] < min) min = arr[i]; } return min; }

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2 Yes

{1, 2} 1 Yes {2, 1} 1 Yes

{1, 2, 3} 1 Yes {2, 1, 3} 1 Yes {2, 3, 1} 1 Yes

{1, 2, 3, 4} 1 Yes {2, 1, 3, 4} 1 Yes {2, 3, 1, 4} 1 Yes {2, 3, 4, 1} 1 Yes

Page 37: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: listMin

37

int listMin (int [] arr) { if (arr.length == 0) return -1; int min = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] < min) min = arr[i]; } return min; }

Call: listMin on {2, 3, 4, 1}

Expected: 1 Actual: 1

Input Expected Value

Pass

{} -1 Yes {1} 1 Yes {2} 2 Yes

{1, 2} 1 Yes {2, 1} 1 Yes

{1, 2, 3} 1 Yes {2, 1, 3} 1 Yes {2, 3, 1} 1 Yes

{1, 2, 3, 4} 1 Yes {2, 1, 3, 4} 1 Yes {2, 3, 1, 4} 1 Yes {2, 3, 4, 1} 1 Yes

?

Page 38: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount

Problem specification: Given a sale amount, compute the discount on the

sale based on the following schedule: Less than $100 – no discount $100 - $999.99 – 10% discount $1000 - $4999.99 – 20% discount >=$5000 – 25% discount

Method header (signature): public static double computeDiscount(double sale)

38

Page 39: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Test Plan: computeDiscount

What are our boundary conditions? Less than $100 – no discount $100 - $999.99 – 10% discount $1000 - $4999.99 – 20% discount >=$5000 – 25% discount

39

Page 40: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Test Plan: computeDiscount

Test Plan

40

Input Expected Value

0.00 0.00 50.00 0.00 99.99 0.00 100.00 10.00 500.00 50.00 999.99 99.999

1000.00 200.00 3000.00 600.00 4999.99 999.998 5000.00 1250.00 10000.00 2500.00

Page 41: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount

Code initially: double cD (double sale)

{

return 0;

}

(Assume we call all previous tests in our example)

Call: cD on 0.00 Expected: 0.00 Actual: 0.00

41

?

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 -- 99.99 0.00 --

100.00 10.00 -- 500.00 50.00 -- 999.99 99.999 -- 1000.00 200.00 -- 3000.00 600.00 -- 4999.99 999.998 -- 5000.00 1250.00 --

10000.00 2500.00 --

Page 42: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount

Code initially: double cD (double sale)

{

return 0;

}

Call: cD on 50.00 on 99.99

Expected: 0.00 Actual: 0.00

42

?

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 Yes 99.99 0.00 Yes

100.00 10.00 -- 500.00 50.00 -- 999.99 99.999 -- 1000.00 200.00 -- 3000.00 600.00 -- 4999.99 999.998 -- 5000.00 1250.00 --

10000.00 2500.00 --

Page 43: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount

Code initially: double cD (double sale)

{

return 0;

}

Call: cD on 100.00 Expected: 10.00 Actual: 0.00

43

?

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 Yes 99.99 0.00 Yes

100.00 10.00 No 500.00 50.00 -- 999.99 99.999 -- 1000.00 200.00 -- 3000.00 600.00 -- 4999.99 999.998 -- 5000.00 1250.00 --

10000.00 2500.00 --

Page 44: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount

Code initially: double cD (double sale)

{

return 0;

}

Call: cD on 100.00 Expected: 10.00 Actual: 0.00

44

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 Yes 99.99 0.00 Yes

100.00 10.00 No 500.00 50.00 -- 999.99 99.999 -- 1000.00 200.00 -- 3000.00 600.00 -- 4999.99 999.998 -- 5000.00 1250.00 --

10000.00 2500.00 --

Page 45: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount

double cD (double sale)

{

if (sale<100.00)

return 0.0;

else

return 0.1*sale;

}

45

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 Yes 99.99 0.00 Yes

100.00 10.00 Yes 500.00 50.00 -- 999.99 99.999 -- 1000.00 200.00 -- 3000.00 600.00 -- 4999.99 999.998 -- 5000.00 1250.00 --

10000.00 2500.00 --

Page 46: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount

double cD (double sale)

{

if (sale<100.00)

return 0.0;

else

return 0.1*sale;

}

Call: cD on 100.00 Expected: 10.00 Actual: 10.00

46

?

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 Yes 99.99 0.00 Yes

100.00 10.00 Yes 500.00 50.00 -- 999.99 99.999 -- 1000.00 200.00 -- 3000.00 600.00 -- 4999.99 999.998 -- 5000.00 1250.00 --

10000.00 2500.00 --

Page 47: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount

double cD (double sale)

{

if (sale<100.00)

return 0.0;

else

return 0.1*sale;

}

Call: cD on 500.00 Expected: 50.00 Actual: 50.00

Call: cD on 999.00 Expected: 99.999 Actual: 99.999

47

?

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 Yes 99.99 0.00 Yes

100.00 10.00 Yes 500.00 50.00 Yes 999.99 99.999 Yes 1000.00 200.00 -- 3000.00 600.00 -- 4999.99 999.998 -- 5000.00 1250.00 --

10000.00 2500.00 --

Page 48: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount

double cD (double sale)

{

if (sale<100.00)

return 0.0;

else

return 0.1*sale;

}

Call: cD on 1000.00 Expected: 200.00 Actual: 100.00

48

?

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 Yes 99.99 0.00 Yes

100.00 10.00 Yes 500.00 50.00 Yes 999.99 99.999 Yes 1000.00 200.00 No 3000.00 600.00 -- 4999.99 999.998 -- 5000.00 1250.00 --

10000.00 2500.00 --

Page 49: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount

double cD (double sale)

{

if (sale<100.00)

return 0.0;

else

return 0.1*sale;

}

49

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 Yes 99.99 0.00 Yes

100.00 10.00 Yes 500.00 50.00 Yes 999.99 99.999 Yes 1000.00 200.00 No 3000.00 600.00 -- 4999.99 999.998 -- 5000.00 1250.00 --

10000.00 2500.00 --

Page 50: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount

double cD (double sale)

{

if (sale<100.00)

return 0.0;

else if (sale <1000.00)

return 0.1*sale;

else

return 0.2*sale;

}

50

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 Yes 99.99 0.00 Yes

100.00 10.00 Yes 500.00 50.00 Yes 999.99 99.999 Yes 1000.00 200.00 Yes 3000.00 600.00 -- 4999.99 999.998 -- 5000.00 1250.00 --

10000.00 2500.00 --

Page 51: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount

double cD (double sale)

{

if (sale<100.00)

return 0.0;

else if (sale <1000.00)

return 0.1*sale;

else

return 0.2*sale;

}

Call: cD on 1000.00 Expected: 200.00 Actual: 200.00

51

?

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 Yes 99.99 0.00 Yes

100.00 10.00 Yes 500.00 50.00 Yes 999.99 99.999 Yes 1000.00 200.00 Yes 3000.00 600.00 -- 4999.99 999.998 -- 5000.00 1250.00 --

10000.00 2500.00 --

Page 52: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount double cD (double sale)

{

if (sale<100.00)

return 0.0;

else if (sale <1000.00)

return 0.1*sale;

else

return 0.2*sale;

}

Call: cD on 3000.00 Expected: 600.00 Actual: 600.00

Call: cD on 4999.99 Expected: 999.998 Actual: 999.998

52

?

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 Yes 99.99 0.00 Yes

100.00 10.00 Yes 500.00 50.00 Yes 999.99 99.999 Yes 1000.00 200.00 Yes 3000.00 600.00 Yes 4999.99 999.998 Yes 5000.00 1250.00 --

10000.00 2500.00 --

Page 53: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount double cD (double sale)

{

if (sale<100.00)

return 0.0;

else if (sale <1000.00)

return 0.1*sale;

else

return 0.2*sale;

}

Call: cD on 5000.00 Expected: 1250.00 Actual: 1000.00

53

?

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 Yes 99.99 0.00 Yes

100.00 10.00 Yes 500.00 50.00 Yes 999.99 99.999 Yes 1000.00 200.00 Yes 3000.00 600.00 Yes 4999.99 999.998 Yes 5000.00 1250.00 No

10000.00 2500.00 --

Page 54: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount double cD (double sale)

{

if (sale<100.00)

return 0.0;

else if (sale <1000.00)

return 0.1*sale;

else

return 0.2*sale;

}

54

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 Yes 99.99 0.00 Yes

100.00 10.00 Yes 500.00 50.00 Yes 999.99 99.999 Yes 1000.00 200.00 Yes 3000.00 600.00 Yes 4999.99 999.998 Yes 5000.00 1250.00 No

10000.00 2500.00 --

Page 55: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount double cD (double sale)

{

if (sale<100.00)

return 0.0;

else if (sale <1000.00)

return 0.1*sale;

else if (sale<5000.00)

return 0.2*sale;

else

return 0.25*sale;

}

55

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 Yes 99.99 0.00 Yes

100.00 10.00 Yes 500.00 50.00 Yes 999.99 99.999 Yes 1000.00 200.00 Yes 3000.00 600.00 Yes 4999.99 999.998 Yes 5000.00 1250.00 Yes

10000.00 2500.00 --

Page 56: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount double cD (double sale)

{

if (sale<100.00)

return 0.0;

else if (sale <1000.00)

return 0.1*sale;

else if (sale<5000.00)

return 0.2*sale;

else

return 0.25*sale;

}

Call: cD on 5000.00 Expected: 1250.00 Actual: 1250.00

56

?

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 Yes 99.99 0.00 Yes

100.00 10.00 Yes 500.00 50.00 Yes 999.99 99.999 Yes 1000.00 200.00 Yes 3000.00 600.00 Yes 4999.99 999.998 Yes 5000.00 1250.00 Yes

10000.00 2500.00 --

Page 57: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD Example: computeDiscount double cD (double sale)

{

if (sale<100.00)

return 0.0;

else if (sale <1000.00)

return 0.1*sale;

else if (sale<5000.00)

return 0.2*sale;

else

return 0.25*sale;

}

Call: cD on 10000.00 Expected: 2500.00 Actual: 2500.00

57

?

Input Expected Value

Pass?

0.00 0.00 Yes 50.00 0.00 Yes 99.99 0.00 Yes

100.00 10.00 Yes 500.00 50.00 Yes 999.99 99.999 Yes 1000.00 200.00 Yes 3000.00 600.00 Yes 4999.99 999.998 Yes 5000.00 1250.00 Yes

10000.00 2500.00 Yes

Page 58: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

TDD in Eclipse Eclipse provides a framework for testing

methods via TDD The JUnit framework Note that methods must be declared as “public” to

work with this framework

To use JUnit in Eclipse: Create a new Java class Create a stub for the method to be written Create a new JUnit class associated with the Java

class 58

Page 59: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Using JUnit

59

Page 60: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Using JUnit

60

Page 61: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Using JUnit

61

Page 62: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Using JUnit

62

Page 63: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Using JUnit

Once you have your JUnit test class defined, you must code up your tests Tests are coded using various “assert” methods –

“assertEquals” is most common

63

Page 64: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Using JUnit – listMin Example

public class ListMinTest {

public void testListMin() { fail("Not yet implemented");

}

}

64

Page 65: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Using JUnit – listMin Example

public class ListMinTest {

public void testListMin() { int [] list = {}; assertEquals(-1,ListMin.listMin(list));

}

}

65

Page 66: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

66

Page 67: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

67

Page 68: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

68

Page 69: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

69

Page 70: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

70

Page 71: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Testing methods In general, set up your test cases to use the

asserts available to JUnit: Test to see if a return value is null or not null

assertNull assertNotNull

Test to see if a return value is true or false assertTrue assertFalse

Test to see if a return value exactly equals a test value assertEquals

71

Page 72: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Testing methods

Special warning about testing methods returning type double: You must give an additional parameter when

using assertEquals with a double value: assertEquals(value1, value2, delta)

The “delta” value is a small value you select for

your test If the two numbers are within that delta, it is true assertEquals(10.0, 10.001, 0.01) == true

72

Page 73: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Testing classes

Suppose we want to use JUnit to test an entire class that we’ve written? How do we go about doing this? We do the same things we do when we test

methods, but we test each instance method of a class

73

Page 74: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Testing classes

The process for testing classes is: Create a JUnit test class for our class Select multiple methods to test

For each of these methods, determine our test cases Write our JUnit method to use these test cases

74

Page 75: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

75

Page 76: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

76

Page 77: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

77

Page 78: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Testing classes

Testing classes is a bit more involved than testing static methods But only a bit We need to consider that methods interoperate

with each other How do we test getId and setId independently of each

other? The two are tied together and so a bug in one will possibly

show up in the other We try to isolate our tests as much as possible, but the

reality is that once we get to something as complex as a class, there will be overlap in our JUnit code

78

Page 79: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Testing classes

Let’s consider some test cases for the setId method Remember our requirements:

IDs must be exactly 10 digits in length IDs must be all digits – no alphabetic or other characters If setId gets an invalid ID, the id should be left alone and

setId should return false If setId gets a valid ID, the id should be changed to the

new id and setID should return true

79

Page 80: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Testing classes One test case for our plan – empty String setID should return false and leave the old ID

alone

How would we implement that as a JUnit test? Why not do this in a getId() test?

Because we’re testing the behavior of setId(), and so we should put our test there

Note that this could find a bug in getId – so don’t get too much tunnel vision when debugging using JUnit

80

Input Expected Value

“” false && (old.getId() == new.getId())

Page 81: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

81

Page 82: CSE 2123: Testing & Test Casesweb.cse.ohio-state.edu/cse2123/sp2018/slides/Lecture09_Testing.pdfBlack Box Testing If the actual output does not match our expectations, open the box

Your Turn

Using the description of setID on the previous slide, come up with a test plan for the setID method Your test plan should include boundary conditions

82