Phase 1: Prepare
Step 1 Warm-up recall
From M7.L2: why should an except name its error?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- write a test with assert (Foundation);
- choose normal and edge cases (Core);
- read a failed test (Core).
Phase 2: Teach
Step 3 A familiar example
A teacher checks a marking scheme by marking a paper whose total she already knows. A test does this for a function: the answer is known before the code is run.
Step 4 Clear explanation
A test runs your code on an input whose correct answer you worked out by hand. assert stops with an AssertionError if the answer is wrong.
def grade(mark):
if mark >= 70:
return "A"
elif mark >= 50:
return "B"
return "C"
assert grade(85) == "A"
assert grade(70) == "A"
assert grade(69) == "B"
assert grade(50) == "B"
assert grade(49) == "C"
print("All tests passed")All tests passedChoose cases on purpose. Include a normal value, the boundaries (70 and 69, 50 and 49) as in Module 2, and any odd input such as an empty list or zero. A failed assert shows AssertionError and the line of the test that failed, which tells you the input that broke it.
Passing tests do not prove a program is perfect; they show it is right for the cases you thought of. So the best tests include the cases that have already caused trouble.
Step 5 Worked example (4 marks)
A function double(n) returns n + 2 by mistake. Which of these asserts fail: double(2) == 4, double(3) == 6, double(0) == 0?
- double(2) gives 4, so the first test passes by chance. (1 mark)
- double(3) gives 5, not 6, so it fails. (1 mark)
- double(0) gives 2, not 0, so it fails. (1 mark)
- Answer: the second and third fail; the first hides the fault. (1 mark)
Go deeper: the professor's notes
Typical.
Boundary.
Empty or zero.
Cases
| Input | Expected |
|---|---|
| grade(70) | A |
| grade(69) | B |
At work: A function
You need to trust a new function.
- List three cases.
- Write the asserts.
What a careful practitioner decides. Choose edge cases on purpose.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. The expected answer of a test should be:
Know the answer before the run.
2. A failed assert raises:
The test shows the failure.
3. Good test cases include:
Edges are where faults hide.
4. A passing test suite shows the code is:
Only for the cases tried.
Phase 4: Close
Step 7 Recap
- A test compares the code with an answer worked out by hand.
- assert stops when the answer is wrong.
- Test normal values, boundaries and odd inputs.
Step 8 Exit ticket (ungraded)
Write three assert lines for a function is_even, including zero.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Write five tests, including two boundaries, for a function that gives a delivery fee by weight.
In industry: Apollo 11: an alarm that did not stop the landing
During the lunar descent of 20 July 1969 the guidance computer showed 1201 and 1202 alarms because it had too many jobs for its time. The software was written to restart and keep the most important jobs, and ground control judged it safe to continue.
Link to this lesson. Plan for overload and failure ahead of time: a program that degrades safely is better than one that stops.
Source: Apollo 11 Lunar Surface Journal; MIT Instrumentation Laboratory memorandum of 4 August 1969 as summarised by ScienceBlog
Common mistakes
| Mistake | Why it fails |
|---|---|
| Copying the output as the expected answer | Work it out by hand first. |
| Testing only easy cases | Add boundaries and odd inputs. |
| Stopping at the first pass | Think of how it could still be wrong. |
Limits. Larger projects use test tools that run many tests and report them; the idea is the same.