All subjects · Computer Science · WCP04 Advanced Practical Programming and Problem-solving
Testing, debugging, reviewing and refining
Edexcel IAL Computer Science · WCP04 Advanced Practical Programming and Problem-solving
The first question on the Unit 4 sample paper asks you to fix syntax, logic, attribute and name errors in a given program. Systematic testing and debugging are how you find them quickly.
Types of error
| Error | When it appears | Example |
|---|---|---|
| Syntax error | Before the program runs: the code breaks Python's grammar | A missing colon after `if x > 3`, unmatched brackets, wrong indentation |
| Run-time error (exception) | While running: an operation is impossible | NameError (misspelt variable), AttributeError (method that does not exist for that type), TypeError, ValueError, IndexError, KeyError, ZeroDivisionError |
| Logic error | The program runs but gives the wrong result | Using < instead of <=, the wrong starting value, an off-by-one range |
Test data and test plans
- Normal data: typical valid values that should be accepted.
- Boundary data: values at the edges of the valid range, and just outside.
- Erroneous data: invalid values (wrong type, out of range, empty) that should be rejected cleanly.
A test plan lists each test, its data, the expected result, the actual result and any action taken. Writing the expected result before running the test stops you accepting whatever appears.
Testing in code
assert condition raises AssertionError if the condition is false, so a set of asserts is a quick automatic test that can be rerun after every change.
def median(values):
ordered = sorted(values)
mid = len(ordered) // 2
if len(ordered) % 2 == 1:
return ordered[mid]
return (ordered[mid - 1] + ordered[mid]) / 2
assert median([3, 1, 2]) == 2 # odd length
assert median([4, 1, 3, 2]) == 2.5 # even length
assert median([7]) == 7 # boundary: one item
assert median([5, 5, 5, 5]) == 5 # all equal
print("all tests passed")Output:
all tests passedDebugging, reviewing and refining
- Reproduce the error with the smallest input that causes it.
- Read the traceback's last line and the line number; inspect the variables there with print statements or a debugger breakpoint.
- Trace the logic by hand with a trace table for logic errors.
- Fix one thing at a time, then rerun all the tests (a fix can break something else).
- Review the working code against the requirements and clean-code rules, then refine it: remove repetition, improve names, simplify conditions, and test again.
Worked example
A function should count marks of 50 or more, but count_passes([50, 49, 75]) returns 1. Find and fix the error.
- ```python def count_passes(marks): count = 0 for m in marks: if m > 50: count = count + 1 return count print(count_passes([50, 49, 75])) ``` Prints 1.
- The boundary value 50 is not counted: a logic error in the comparison.
- Change `m > 50` to `m >= 50`. Now it prints 2.
Answer: Testing with a boundary value exposed a logic error; the program ran without crashing, so only the wrong output revealed it.
Check yourself
- Which exception does this program raise?
```python
total = 10
print(totl + 5)
```
- SyntaxError
- NameError
- TypeError
- AttributeError
Show answer
NameError. totl was never defined, so Python raises NameError.
- Which exception does this program raise?
```python
word = "exam"
print(word.uppercase())
```
- NameError
- ValueError
- SyntaxError
- AttributeError
Show answer
AttributeError. Strings have no method called uppercase (it is upper), so Python raises AttributeError.
- Which exception does this program raise?
```python
age = "17"
print(age + 1)
```
- ValueError
- TypeError
- NameError
- IndexError
Show answer
TypeError. A str cannot be added to an int: TypeError. Convert with int(age) first.
- Which exception does this program raise?
```python
stock = {"pen": 4}
print(stock["ink"])
```
- IndexError
- NameError
- KeyError
- AttributeError
Show answer
KeyError. The dictionary has no key 'ink', so Python raises KeyError. Use get() to supply a default.
- A program to find the average of a list runs without any error message but prints the wrong value. What type of error is this?
- Logic error
- Syntax error
- Run-time error
- Name error
Show answer
Logic error. The program runs to completion, so there is no syntax or run-time error; the algorithm itself is wrong.
- A program accepts ages from 11 to 18. Which is erroneous test data?
- 11
- 18
- 'twelve'
- 15
Show answer
'twelve'. A non-numeric string should be rejected. 11 and 18 are boundary data; 15 is normal data.
- After fixing a bug, why should you rerun all your earlier tests, not only the one that failed?
- The fix might have broken something that worked before
- Python forgets the earlier results
- It makes the program faster
- Tests can only be run in order
Show answer
The fix might have broken something that worked before. Rerunning the full set (regression testing) catches new faults introduced by a change.
- What does this program print?
```python
def is_even(n):
return n % 2 == 1
try:
assert is_even(4), "4 should be even"
print("passed")
except AssertionError as e:
print("failed:", e)
```
Show answer
failed: 4 should be even. is_even has a logic error (== 1 tests for odd), so is_even(4) is False and the assert fails with its message. Output: `failed: 4 should be even`
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Computer Science specification, Issue 1 (March 2026)
- Pearson Edexcel IAL Computer Science Unit 4 Programming Language Subset (PLS)
- Pearson Edexcel IAL Computer Science sample assessment materials (Unit 3 and Unit 4)
Last updated 8 November 2026. Spotted a mistake? Tell us.