AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Test it properly

Week 12 · 1.8 Testing · 3 lessons
2

Write the test plan

Computing 9: Topic 1.8, pp. 135–154
GoalWrite a test plan with expected output worked out before the test, fill in the actual output, and find a fault from the results.
New words
test plan · a table that lists the test data to use and the output expected for each testexpected output · what the program should do, worked out from the requirements before testingactual output · what the program really does when it is run with the test data
Explanation

A test plan decides what to test before you start. Each row shows the kind of data, the input and the expected output. Work out the expected output from the requirements, not from the program. If you run the program first and copy its answer, a wrong program will seem right. Then run the program, or trace it by hand, and write down the actual output. If the actual output is the same as the expected output, the test passes. If not, the test fails and the program has an error. The plan should include normal, invalid and, when there is a range, extreme data. A plan with many tests of the same kind is weaker than one with a mix.

Worked examples
TestType of dataInputExpected outputActual outputPass / fail
1Normal12Locker found
2Normal30Locker found
3Extreme1Locker found
4Extreme36Locker found
5Invalid0No such locker
6Invalid37No such locker
7Invalid−5No such locker
A test plan for the locker program. The requirements say that numbers 1 to 36 are accepted and all other numbers are rejected. The plan has normal, extreme and invalid data. The expected outputs are worked out from these requirements. The last two columns are empty, because nothing has been run yet.
TestType of dataInputExpected outputActual outputPass / fail
1Normal12Locker foundLocker foundpass
2Normal30Locker foundLocker foundpass
3Extreme1Locker foundNo such lockerFAIL
4Extreme36Locker foundLocker foundpass
5Invalid0No such lockerNo such lockerpass
6Invalid37No such lockerNo such lockerpass
The plan after testing a faulty version, in which the first condition is locker > 1 instead of locker >= 1. Normal data and the high extreme pass. Only test 3, the extreme value 1, fails, because the program prints No such locker. Without the extreme data, this error could be missed, because all the normal tests pass.
Python
1locker = int(input("Locker number? "))
2if locker > 1 and locker <= 36:
3    print("Locker found")
4else:
5    print("No such locker")
Output
No such locker
Reading the code that was tested. This is the faulty version. The condition locker > 1 is false when the user types 1, so the program takes the else branch. The error is a logic error: the program runs without any message but gives the wrong answer. The fix is to change > to >=.
Class activity

Plan and test (no computer). Take the sticker rule in the first practice task (1 to 5 stickers are allowed; the program prints OK or Not allowed). Write the program on paper first. Then make a test plan with seven tests: two normal, the two extreme values, and three invalid. Write the expected outputs before you look at the program. Trace your program by hand for each test and fill in the actual output and pass / fail. Ask a partner to change one comparison in the program secretly, for example < for <=, and trace again to see which test now fails.

Practice
1
The requirement is: a pupil may choose from 1 to 5 stickers, and the program prints OK or Not allowed. Complete the expected output for each test.
TestType of dataInputExpected output
1Normal3?
2Extreme1?
3Extreme5?
4Invalid0?
5Invalid6?
2
A learner writes the program shown, with the upper condition stickers < 5. What is the actual output for each test input 3, 1, 5, 0 and 6? Which test fails? What kind of error is it?
Python
1stickers = int(input("How many stickers? "))
2if stickers >= 1 and stickers < 5:
3    print("OK")
4else:
5    print("Not allowed")
3
A program saves a score if it is from 0 to 40 inclusive, and prints Score saved. Otherwise it prints Not a valid score. Write a test plan with at least two normal tests, both extreme values and two invalid tests.
4
Why must you work out the expected output before you run the program?