- Use test data to check whether a program works correctly
- Recognise syntax, runtime and logic errors in Python programs
- Fix errors by reading messages, tracing values and testing again
- I can choose normal, extreme and exceptional test data
- I can explain the difference between syntax, runtime and logic errors
- I can record a test, expected result, actual result and fix
These questions check the ideas you need before testing a program.
1. Which type of error stops Python understanding the code before it runs?
2. A program runs but gives the wrong answer. What kind of error is most likely?
3. Why should expected results be written down before running a test?
Key vocabulary
Testing, Common Errors and Fixing Them
Testing is planned checking
Testing is not just running a program once and hoping it works. A useful test has four parts: the test data, the expected result, the actual result, and a decision about whether the program passed. The expected result should be written before the program is run. That stops you from changing the answer to match a broken program.
Choosing test data
Good test data covers the different kinds of input the program should handle. Normal data is ordinary data that should be accepted. Extreme data is right on the edge of a condition, such as 12 and 13 when the rule changes at age 13. Exceptional data is data the program may not be able to use, such as text where a number is expected.
Three common error types
A syntax error means Python cannot understand the code, often because a colon, bracket or indentation is missing. A runtime error happens while the program is running, for example when trying to convert the word "ten" into an integer. A logic error is when the program runs but the algorithm is wrong, such as using > when the rule needs >=.
Fix, then test again
Fixing code is a cycle. Read the message or output carefully, identify the likely line, make one sensible change, then run the same test again. If you make lots of changes at once, you may not know which change fixed the problem. A clear test table is evidence that you can test and refine a solution.
Worked examples
A cinema program charges a child price if age is under 13.
if score >= 5
print("Pass")if score >= 5:.A discount should apply when total is 20 or more, but the code says if total > 20:.
if total >= 20:.A program should print "Pass" for marks of 50 or above. It uses if mark > 50:. Choose three tests and explain the bug.
Use 49, 50 and 51. 49 should fail, 50 should pass, 51 should pass. The condition should be mark >= 50 because 50 is included in the pass range.
For assessment evidence, your test table should show the test data, expected result, actual result and whether a fix was needed. This maps directly to N3 O1.3 and N4 O2.3.
Use these questions to practise identifying errors and recording tests.
1. Which error type is caused by missing a colon after if score >= 5? TYPE 1
2. A program divides by zero while running. What kind of error is this? TYPE 1
3. For a rule that changes at age 13, which pair is best for extreme testing? TYPE 1
4. Explain why if mark > 50: is wrong if 50 is meant to be a pass. TYPE 2 N3
mark >= 50.5. Make a test table for a program that charges 2.00 if age is under 13 and 5.00 otherwise. Include three tests. TYPE 2 N4
6. Practical: Create sdd7_testing.py. Write a small program with one selection rule, test it using normal and extreme data, then record any fix you made. TYPE 3
Suggested timing: 55 minutes. Emphasise boundary testing and expected-vs-actual language.
Assessment standards covered: N3 O1.3; N4 O2.3.