Menu

Errors in algorithms

Testing Programs

Testing a program means running it with inputs you choose in advance and comparing its actual output with the expected output you worked out by hand. You calculate what should come out in advance, then run the program and compare the two. A mismatch means there is a fault to find, and comparing actual with expected is the first stage of debugging.

The rule for this ride is that you must be at least 140 cm tall.

# checks whether someone is tall enough for the ride
height = int(input("Height in cm: "))
if height > 140:
    print("Allowed on")
else:
    print("Too short")

Output when the user types 140:

Height in cm: 140
Too short

Entering 165 gives the answer you expected, so that test passes. Entering 140, the lowest allowed height, does not, so a fault is revealed: the condition should accept 140 itself, using >=. The passing test on its own told you nothing about it.

A number line of heights from 100 to 200 cm compares the rule (at least 140 is allowed on) with the program (over 140). Typing 140 gives Too short instead of Allowed on, exposing the fault; typing 165 passes and shows nothing.

Choosing a range of test data

Vary the inputs on purpose rather than picking them at random. For a program that checks a username, which must be between 6 and 12 characters long and contain letters and digits only:

Type of test dataExample inputWhy it is used
Normal dataplayerone (9 characters)An ordinary length the program should accept.
Boundary dataabc123 (6 characters) and longusername (12 characters)The shortest and longest allowed lengths, where length checks are most likely to be wrong.
Invalid dataabc12 (5 characters), thisiswaytoolong (16 characters) or bad!nameValues the program should reject: too short, too long, or a symbol that is not allowed.

Why one test proves very little

A runtime error often appears for just one input: the program works again and again, then stops on the value that asks for something impossible. A logic error can give the right answer for one value and the wrong answer for another, as the ride program does. One successful test proves only that the program works for that one input.

What testing cannot find

Testing can never reveal a syntax error. A program that breaks the rules of the language will not run at all, so there is nothing to test: the compiler or interpreter reports the mistakes before it starts. Those are found and fixed first, and testing only ever begins on a program that already runs.

Practise this