Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Testing AI code with your own cases

After this lesson: I can test AI code with my own cases.

50 min lessonNo commands

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

WordWhat it meansExample
Test caseone input and the result you expect
Expected resultthe right answer, worked out from the requirement
asserta Python line that stops if something is falseassert delivery_charge(20) == 4
Boundarythe exact value where a rule changes50
Edge casean 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

Code: python
# 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

KindOrder totalExpected chargeWhy this case
Normal£20£4A small everyday order
Normal£80£0A large everyday order
Boundary£50£0Exactly where the rule changes
Boundary£49.99£4Just below the change
Edge£0£4An empty basket: ask the team

The cells students fill in are left blank in this preview.

Try it: add your own cases

Code: python
# 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.

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.