Phase 1: Prepare
Step 1 Warm-up recall
From M2.L3: why does the order of elif checks matter?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- identify the boundaries in a decision program (Foundation);
- choose test values on and around each boundary (Core);
- write a test table of inputs and expected outputs (Core).
Phase 2: Teach
Step 3 A familiar example
A ticket office charges by age. The two likeliest mistakes are at the edges: is a five-year-old a child or free, and is an 18-year-old an adult? Testing the edges finds them.
Step 4 Clear explanation
Most decision bugs sit at boundaries, the exact values where the answer changes. The common slip is using > where you meant >=, which changes the result for one value only.
age = int(input("Age: "))
if age < 5:
print("Free")
elif age < 18:
print("Child")
else:
print("Adult")The boundaries are 5 and 18. Test on each boundary and just below it. Write the expected output before you run the program, so you are checking the program, not copying it.
| Age tested | Expected output | Why this test |
|---|---|---|
| 4 | Free | just below the first boundary |
| 5 | Child | on the first boundary |
| 17 | Child | just below the second boundary |
| 18 | Adult | on the second boundary |
If the program prints something different from the expected column, you have found a bug while it is cheap to fix.
Step 5 Worked example (4 marks)
Using the ticket program above, what is printed for age 18 and for age 17?
- Age 18: 18 < 5 is False; 18 < 18 is False; so else runs. (1 mark)
- Output for 18: Adult. (1 mark)
- Age 17: 17 < 5 is False; 17 < 18 is True. (1 mark)
- Answer: Adult for 18 and Child for 17. (1 mark)
Go deeper: the professor's notes
n - 1.
n.
n + 1.
Tests
| Value | Expected |
|---|---|
| 365 | not leap day |
| 366 | last day |
At work: A year counter
Days left can be 365, 366 or 367.
- Write the three expected outputs.
- Run all three.
What a careful practitioner decides. Always test the exact limit value.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. A boundary value is:
Edges are where bugs hide.
2. For age >= 18, the best values to test are:
Test both sides.
3. When should you write the expected output?
Predict first.
4. Changing >= to > changes the result for:
Only the edge value moves.
Phase 4: Close
Step 7 Recap
- Bugs hide at boundaries.
- Test on and just below each boundary.
- Write expected outputs before running.
Step 8 Exit ticket (ungraded)
List the test ages you would use for a program with boundaries at 12 and 65.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Write the test table for a program that prints Pass at 50 or more.
In industry: Zune: day 366
On 31 December 2008 a clock routine in the 30GB Zune tested days > 366 inside a loop. On day 366 the days equalled 366, so the leap-year branch was skipped and the loop never ended. Microsoft said a clock driver handled the last day of a leap year badly.
Link to this lesson. The value that equals the limit is the one the code is most likely to get wrong. Test it first.
Source: Microsoft engineering blog, Hello w0rld0; TechCrunch, 31 Dec 2008
Common mistakes
| Mistake | Why it fails |
|---|---|
| Testing only typical values | Add the boundary values. |
| Writing expected output after running | Predict first or you will copy the bug. |
| Mixing up > and >= | Decide whether the boundary belongs to the upper or lower case. |
Limits. Testing shows the presence of bugs; it cannot prove there are none.