GoalChoose normal and boundary test data, write expected results and use them to find bugs in a program.
New words
test data · the values that we give to a program to check that it worksexpected result · the answer that a correct program should giveboundary value · a value at the edge of a rule, such as 120 when the rule is 120 or moretest table · a table that lists the test data, the expected result and the actual result
Explanation
A program that works with one number may fail with another. To test a program properly we use a range of data. Normal data is a typical value that is well inside the rule. Boundary data is a value on the edge of the rule and the values just next to it. Many bugs hide at the edges, for example when we write > instead of ≥. Before we run a test, we write the expected result in a test table, using the rule and not the program. Then we run the program and write the actual result. If they are different, there is a bug. We find it, fix it and run all the tests again.
Kind of test
Rule: 120 or more
Expected
normal
150
yes
normal
80
no
boundary
119
no
boundary
120
yes
boundary
121
yes
Worked examples
The rule says that a child can ride if they are 120 cm tall or taller. This program follows the rule with the test value 120. The condition 120 ≥ 120 is true, so it shows yes.
height
Expected
Actual (with >)
Result
150
yes
yes
pass
80
no
no
pass
120
yes
no
FAIL
121
yes
yes
pass
A programmer wrote > instead of ≥. Two normal tests pass, so the bug is not noticed. Only the boundary test at 120 shows it: the child should be allowed but the program says no.
items
Expected
Why this test
2
no discount
normal, well below the rule
4
no discount
just below the edge
5
discount
on the edge
6
discount
just above the edge
A shop gives a discount for 5 or more items. The boundary values are 4, 5 and 6. A good test plan checks the edge, and both of its sides.
Class activity
Find the secret rule (no computer): Player A secretly chooses one of two rules, ‘more than 12’ or ‘12 or more’, and writes it down. Player B may give the machine only three numbers, and A says yes or no for each. Which three numbers should B choose to find out the rule? Play again with different rules, for example ‘under 30’ and ‘30 or under’. Write a test table for each game. What did you notice about the number on the edge? If you have a computer or tablet, build the ride program in a micro:bit simulator and run your test table.
Practice
1
A program shows pass if mark ≥ 50, and fail if not. Write three boundary tests with the expected results.
49: fail. 50: pass. 51: pass. (The value 50 is on the edge, and 49 and 51 are next to it.)
2
The rule is that a club is for ages 12 and over. Trace the program with age 12. Is it right? If not, fix it.
The condition age > 12 with age = 12 is false, so it shows no. The rule says that 12 is allowed, so the expected result is yes. The program has a bug. Change > to ≥. Then 12 ≥ 12 is true and it shows yes.
3
A program should show yes when score ≥ 10. A pupil tested it with the three values below and all were correct. Is this enough? What would you add?
score
Expected
Actual
3
no
no
25
yes
yes
80
yes
yes
No. None of the values is near the edge. A program that used > 10 would pass these three tests too. Add the boundary tests 9 (no), 10 (yes) and 11 (yes).
4
List test data for the rule ‘children under 5 get free entry’. Include normal and boundary values.
Normal: 2 (free) and 30 (not free). Boundary: 4 (free), 5 (not free) and 6 (not free). The edge is at 5, because a child of 5 is not under 5.