Menu

Testing

Refining after Testing

Refining after testing means using a failed test to find an error, fixing the algorithm, then running the same test again. A test only helps if you compare the expected result with the actual result: when they differ the test has failed, and the difference points to where the error is likely to be.

For example, a cookery class takes bookings for groups of 2 to 6 people. A test with 7 people has the expected result "rejected", because 7 is outside the valid range. If the actual result is "accepted", the program accepts data it should reject. That failed test shows the upper limit in the condition is wrong.

people = int(input("Enter number of people: "))
if people >= 2 and people <= 8:
    print("Booking accepted")
else:
    print("Booking rejected")

Output when the user types 7:

Enter number of people: 7
Booking accepted

The code lets 7 through because its upper limit is 8. Refine the algorithm by changing the upper limit to 6, so the condition reads people >= 2 and people <= 6. Then run the same test again: this time 7 is rejected, which matches the expected result, so the error has really been fixed rather than just altered. Rerun the earlier tests too, such as 4 people, because an edit can break something that used to work; a pass on every test shows the algorithm has improved without spoiling what was already correct.

Practise this