Free delivery, or not
A shop promises free delivery on orders of £50 or more. An assistant writes the rule, and it looks right. A customer spends exactly £50 and is charged £4.
One test with £50 would have caught it in a second. This lesson is about choosing that test yourself.
By the end of this lesson
Note
I can test AI code with my own cases.
Key words
| Word | What it means | Example |
|---|---|---|
| Test case | one input and the result you expect | |
| Expected result | the right answer, worked out from the requirement | |
| assert | a Python line that stops if something is false | assert delivery_charge(20) == 4 |
| Boundary | the exact value where a rule changes | 50 |
| Edge case | an unusual input, such as empty or zero |
The cells students fill in are left blank in this preview.
A test case pairs input and answer
A test case is one input and the result you expect. Work out the expected result from the requirement, before you run the code. If you read the answer from the code first, you only test that the code agrees with itself.
In Python, assert checks one case. If the check is false, the program stops and says which one failed.
Try it: test the delivery rule
# Requirement: delivery costs 4 pounds.
# Orders of 50 pounds or more get free delivery.
# The assistant wrote this:
def delivery_charge(total):
if total > 50:
return 0
return 4
# Our own test cases, written from the requirement.
assert delivery_charge(20) == 4, "normal case"
assert delivery_charge(80) == 0, "normal case, free"
assert delivery_charge(50) == 0, "boundary: exactly 50"
print("All cases pass")
# Press Run. Which case fails? Fix the function, then run again.
Choose normal, boundary and odd cases
Good cases come in three kinds. A normal case is a value people use every day. A boundary is the exact value where the rule changes, and the values just either side. An edge case is odd: empty, zero, negative or very large.
Assistants are good at the normal case. Their faults hide at the boundary and the edges.
Worked example: cases for free delivery
| Kind | Order total | Expected charge | Why this case |
|---|---|---|---|
| Normal | £20 | £4 | A small everyday order |
| Normal | £80 | £0 | A large everyday order |
| Boundary | £50 | £0 | Exactly where the rule changes |
| Boundary | £49.99 | £4 | Just below the change |
| Edge | £0 | £4 | An empty basket: ask the team |
The cells students fill in are left blank in this preview.
Try it: add your own cases
# Requirement: a password needs 8 or more characters.
def long_enough(password):
return len(password) >= 8
assert long_enough("sunflower1") == True, "normal, long"
assert long_enough("cat") == False, "normal, short"
# Add three cases of your own under this line:
# one with exactly 8 characters, one with 7, and an empty one.
print("All cases pass")
A common mistake
Warning
Many people think they can ask the assistant to write the tests as well, and be done. Tests from the same source share its misunderstanding. Write the expected results yourself, from the requirement, whenever the code matters.