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

Preview

Test, report and fix

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

Fix one bug, break another

You fix a bug in the cancel code. The cancel tests pass. You are happy.

Then somebody joins the waiting list and everything stops. Your fix changed something another part needed.

Good testing finds this early. There is more than one kind of test, and each finds a different kind of fault.

Key words

WordWhat it meansExample
Module testingTesting one part of the program on its own.test next_in_line alone
Integration testingTesting that parts work together as one system.cancel, then promote
Automated testingTests that a program runs for you.the Run Tests button
User testingWatching real people use the solution.a desk worker tries it
Regression testingRunning old tests again after a change, to check nothing broke.run all tests after a fix
FunctionA named piece of code that does one job.cancel()
PromoteMove a person from the waiting list into a booking.Aisha is promoted when Sam cancels
Bug reportA clear note about a fault: what, where, how to see it.Promote leaves a name in the list
Change logA list of what changed, when and why.v3: fixed promote
Test dataThe values you type in to test. Some are good and some are bad on purpose.hour 12, hour 21

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

Four kinds of test on the waiting list

KindHillside exampleWho or what runs it
Modulenext_in_line gives the first name for a slotThe developer
IntegrationA cancel gives the court to the first person waitingThe developer
AutomatedAll the tests run when you press one buttonA program
UserA desk worker tries to join Sam to the listA real person

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

Try it: a tiny automated test

Code: python
def next_in_line(waiting, court, hour):
    for one in waiting:
        if one["court"] == court and one["hour"] == hour:
            return one["member"]
    return None


def test_first_name():
    waiting = [{"member": "Sam", "court": 1, "hour": 10}]
    assert next_in_line(waiting, 1, 10) == "Sam"


def test_nobody():
    assert next_in_line([], 1, 10) is None


for test in (test_first_name, test_nobody):
    test()
    print(test.__name__, "passed")

Quick check: which test?

Have a go

Quick check: which test?

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Five kinds of test data

KindHillside example (hours 9 to 20 are open)Expected
ValidHour 12Booked
Valid extreme (the edge that still works)Hours 9 and 20Booked
InvalidHour 30Refused
Invalid extreme (just past the edge)Hours 8 and 21Refused
Erroneous (the wrong kind of data)The word "ten"Refused with a clear message

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

Try it: run the five kinds of data

Code: python
def book(bookings, member, court, hour):
    if hour < 9 or hour > 20 or court not in (1, 2):
        return False
    bookings.append({"member": member, "court": court, "hour": hour})
    return True


tests = [("normal", 12), ("lowest valid", 9), ("highest valid", 20),
         ("too low", 8), ("too high", 21), ("wrong type", "ten")]
for kind, hour in tests:
    try:
        print(kind, hour, "->", book([], "Sam", 1, hour))
    except TypeError:
        print(kind, hour, "-> CRASH: text cannot be compared with a number")

# The last test finds a fault. How would you fix it?

The lesson goes on

6 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.