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

Free lesson

OSP task 2 (3 of 3): Test, report and fix

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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.hour 12, hour 21

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

Match the test to what it finds

Have a go

Loading…

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

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")

Make a free account to save your work and track your progress.

Check: which kind of test?

Have a go

Check: which kind of 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

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 test in tests:
    kind = test[0]
    hour = test[1]
    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?

Make a free account to save your work and track your progress.

Task 2 practice: fix the join

Task 2 practice. The parts of the waiting list each pass their own tests. But cancel_and_promote, which joins them, has two faults. Run the tests, read the messages and find both. Fix the function. Then run all tests again. Press Run Tests to check your work.

Code: python
def is_free(bookings, court, hour):
    for one in bookings:
        if one["court"] == court and one["hour"] == hour:
            return False
    return True


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


def cancel(bookings, member, court, hour):
    for one in bookings:
        if one["member"] == member and one["court"] == court and one["hour"] == hour:
            bookings.remove(one)
            return True
    return False


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 leave_waiting_list(waiting, member, court, hour):
    for one in waiting:
        if one["member"] == member and one["court"] == court and one["hour"] == hour:
            waiting.remove(one)
            return True
    return False


def cancel_and_promote(bookings, waiting, member, court, hour):
    # Cancel the member's booking. If somebody is waiting for that court and hour,
    # give the court to the first person and take them off the waiting list.
    # Give back the name of the person who got the court, or None.
    # The parts above each pass their own tests. The problem is where they join.
    cancel(bookings, member, court, hour)
    promoted = next_in_line(waiting, court, hour)
    if promoted is not None:
        book(bookings, promoted, court, hour)
    return promoted

Make a free account to save your work, or press Start lab to work through the whole lesson.

A bug report lets anybody see the fault

With a good bug report, anybody can make the fault happen again. It says what you did, what you expected and what happened.

It also says how serious it is. Keep it short. Do not just write "it is broken".

A bug report, and a change log line

FieldExample
TitlePromoted member stays on the waiting list
StepsSam cancels court 1 at 10. Aisha is first in the line.
ExpectedAisha gets the court and leaves the list
ActualAisha gets the court but is still on the list
Severity (how serious it is)High: the same person can be promoted again
CauseApplication error: the code did not remove the name
Change logv4: cancel_and_promote now removes the promoted member from the list
ToldWho was told the fix is ready, and on which day: the desk manager
Lesson learnedTest the join between two parts, not only each part

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

After a fix: run everything

Important

Run the test that failed. Then run every other test too. This is regression testing. Write the result in your change log. A fix is not finished until the old tests still pass.

Task 2 practice: report and log

Write a bug report for each of the two faults you fixed. Use the fields Title, Steps, Expected, Actual, Severity and Cause. Then write a change log with two lines, one for each fix, each with a version number. Last, write two sentences about the regression check you ran after the fixes.

A teacher marks this in a college. Nothing you write here is kept: it goes when you leave the page.

Make a free account to save your work, or press Start lab to work through the whole lesson.

Key words crossword

Have a go

Loading…

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

Final check

Have a go

Final check

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

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.