cse 2123: testing & test...
TRANSCRIPT
1
CSE 2123: Testing & Test Cases
Jeremy Morris
Software Lifecyle – Waterfall Model
2
Requirements Specification &
Implement (Code)
Design
Testing
Maintenance/Fix
Test Driven Model: Discover Design
3
Requirements Specification &
Implement (Code)
Test Plan/Testing
Maintenance/Fix
Find Design
Test Driven Model: Test/Refine Design
4
Requirements Specification &
Implement (Code)
Test Plan/Testing
Maintenance/Fix
Candidate Design
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
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
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
Test Plan: listMin
Categorize inputs by array size Array size 0 Array size 1:
Array size 2:
8
m
m
m
Test Plan: listMin
Categorize inputs by array size Array size 3:
9
m
m
m
Test Plan: listMin
Categorize inputs by array size Array size 4:
10
m
m
m
m
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
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
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
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 -
?
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 -
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 -
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 -
?
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 -
?
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 -
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 -
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 -
?
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 -
?
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 -
?
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 -
?
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 -
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 -
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 -
?
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 -
?
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 -
?
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 -
?
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 -
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; }
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
?
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
?
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
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
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
?
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
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
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
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 --
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 --
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 --
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 --
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 --
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 --
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 --
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 --
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 --
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 --
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 --
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 --
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 --
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 --
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 --
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 --
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
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
Using JUnit
59
Using JUnit
60
Using JUnit
61
Using JUnit
62
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
Using JUnit – listMin Example
public class ListMinTest {
public void testListMin() { fail("Not yet implemented");
}
}
64
Using JUnit – listMin Example
public class ListMinTest {
public void testListMin() { int [] list = {}; assertEquals(-1,ListMin.listMin(list));
}
}
65
66
67
68
69
70
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
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
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
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
75
76
77
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
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
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())
81
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