Phase 1: Prepare
Step 1 Warm-up recall
From M8.L1: why work an example by hand before coding?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- build one function and test it alone (Foundation);
- join functions in a main program (Core);
- keep a working copy as you go (Core).
Phase 2: Teach
Step 3 A familiar example
A builder checks each wall is level before adding the next storey. A program is built the same way, so a fault is found where it was made.
Step 4 Clear explanation
Follow the plan, one function at a time.
def needs_reorder(qty, level):
"""True when qty is at or below level (whole numbers)."""
return qty <= level
assert needs_reorder(3, 5) == True
assert needs_reorder(5, 5) == True
assert needs_reorder(9, 5) == False
print("needs_reorder passes")needs_reorder passesThe boundary case 5 and 5 is tested on purpose. Once each function passes alone, join them in a short main function that reads like the plan:
def main():
rows, bad = load_rows("stock.csv")
names = make_report(rows)
print(len(names), "to reorder:", names)
print(bad, "bad rows skipped")
main()Keep a working copy each time something passes, so a later mistake can be undone. Add features one at a time, and rerun every earlier test after each change. If the whole program fails, return to the last working copy and add the new part again in a smaller step.
Step 5 Worked example (4 marks)
Your function make_report passes its tests. You add a sort of the names and the tests now fail. What is the sensible move?
- The sort is the only change since the last pass. (1 mark)
- Restore the working copy, or undo the sort. (1 mark)
- Add the sort again in a smaller step and rerun the tests. (1 mark)
- Answer: return to the working copy and retry in small steps. (1 mark)
Go deeper: the professor's notes
One function.
Alone.
Then main.
Order
| Step | Rule |
|---|---|
| test | before joining |
| copy | after each pass |
At work: A new feature
You want to add sorting.
- Add it alone.
- Rerun all tests.
What a careful practitioner decides. Never add two things before a retest.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. You should join functions together:
Test each first.
2. A main function should:
It calls the pieces in order.
3. Keeping a working copy lets you:
Step back safely.
4. After adding a feature you should rerun:
Nothing else must break.
Phase 4: Close
Step 7 Recap
- Build and test one function at a time.
- Join them in a main function that reads like the plan.
- Keep working copies and rerun all tests after each change.
Step 8 Exit ticket (ungraded)
Write needs_reorder for the stock plan and three asserts, including the boundary.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Build the first two functions of your capstone and test each alone.
In industry: Heathrow Terminal 5: tested, but not the same
BAA's chairman told MPs the baggage system had been tested over six months and 20 times in a fully loaded condition. The Transport Committee still criticised British Airways for poor staff training and system testing.
Link to this lesson. Testing a system is not the same as testing the whole operation with real people, real timings and real conditions.
Source: Flight International, 2008; Building, 3 November 2008
Common mistakes
| Mistake | Why it fails |
|---|---|
| Writing everything, then testing | Test as you go. |
| No working copy | Save a copy after each pass. |
| Adding two features at once | One at a time, then retest. |
Limits. Teams use version control tools to keep working copies; the habit is the same.