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.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Types of error

ErrorWhen it appearsExample
Syntax errorBefore the program runs: the code breaks Python's grammarA missing colon after `if x > 3`, unmatched brackets, wrong indentation
Run-time error (exception)While running: an operation is impossibleNameError (misspelt variable), AttributeError (method that does not exist for that type), TypeError, ValueError, IndexError, KeyError, ZeroDivisionError
Logic errorThe program runs but gives the wrong resultUsing < instead of <=, the wrong starting value, an off-by-one range

Test data and test plans

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 passed

Debugging, reviewing and refining

Worked example

A function should count marks of 50 or more, but count_passes([50, 49, 75]) returns 1. Find and fix the error.

  1. ```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.
  2. The boundary value 50 is not counted: a logic error in the comparison.
  3. 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.

Key idea: Syntax errors stop the program running; run-time errors raise exceptions (NameError, AttributeError, TypeError...); logic errors give wrong results. Test normal, boundary and erroneous data against expected results; use asserts; fix one thing at a time and retest; then review and refine.

Check yourself

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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`

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.