Edexcel International Advanced Level Computer Science, point 9.3.1
Functionality means the program does what it is supposed to do, accurately. You must be able to identify errors, correct them, produce accurate outcomes, and refine solutions to meet requirements. The specification refers to the Good Programming Practice Guide (GPPG).
a. Testing and debugging
Testing
Testing is running a program with chosen data to check that it gives the expected results.
- Know the requirements: what must it do?
- Plan tests: choose test data and work out the expected result before running.
- Run the tests and record the actual result.
- Compare: any difference is a failure to investigate.
Test data
| Type | Meaning | Example (an age from 11 to 19) |
|---|---|---|
| Normal | Typical valid data | 15 |
| Boundary | On and next to the limits | 10, 11, 19, 20 |
| Erroneous (invalid) | Data that should be rejected | -5, "abc", blank |
A test plan table:
| Test | Data | Expected | Actual | Pass? |
|---|---|---|---|---|
| Normal age | 15 | Accepted | Accepted | Yes |
| Lower boundary | 11 | Accepted | Accepted | Yes |
| Below boundary | 10 | Rejected | Accepted | No |
| Text | "abc" | Error message | Crash | No |
Automated tests can be written in code (for example with assert):
def is_valid_age(age):
return 11 <= age <= 19
assert is_valid_age(11) is True
assert is_valid_age(19) is True
assert is_valid_age(10) is False
assert is_valid_age(20) is False
print("All tests passed")
Types of error
| Error | Description | Found by |
|---|---|---|
| Syntax | Breaks the language rules: the program won't run | The interpreter or IDE (6.3.1) |
| Runtime | Crashes while running (divide by zero, index out of range) | Running with test data |
| Logic | Runs, but gives wrong results | Testing against expected results (hardest to find) |
Debugging
Debugging is finding and fixing the cause of an error.
- Read the error message: it gives the type and line number.
- Trace by hand with a trace table (5.1.2).
- Add
printstatements to show values at key points. - Use the IDE's debugger: breakpoints, stepping, variable watch (6.3.1).
- Isolate the fault: test the subprogram on its own, or comment out parts.
- Narrow down by checking the middle of the program (like a binary search).
- Fix one thing at a time, then re-test (and re-run earlier tests, to check nothing else broke: regression testing).
Example: this should return the largest item, but fails for negatives.
def largest(numbers):
# fault: assumes at least one value above 0
biggest = 0
for n in numbers:
if n > biggest:
biggest = n
return biggest
print(largest([-5, -2, -9])) # 0 (expected -2)
Fix: start with the first item, biggest = numbers[0].
b. Reviewing and refining
Reviewing is checking the solution against the requirements and for quality, and refining is improving it.
Review questions
- Does it meet every requirement? (Make a checklist.)
- Does it handle invalid and unusual data?
- Is the output accurate and in the right form?
- Is the code readable and well structured (9.2)?
- Is it efficient enough?
- Is the user experience clear?
Ways to review
- Compare with the requirements line by line.
- Desk-check (read through and trace by hand).
- Peer review: someone else reads your code and gives feedback.
- User feedback: someone tries it.
- Run the full test plan again.
Refining
- Fix the errors found.
- Improve names, structure and comments.
- Add missing validation or features.
- Remove repeated code (refactor into a subprogram).
- Improve efficiency where needed.
- Re-test after every change.
This is an iterative cycle: test, find errors, fix, review, refine, test again.
Worked example
Task: the requirement is: "A car park charges nothing for stays under 1 hour, £2 for 1 hour up to 4 hours, and £5 for 4 hours or more. Stays below 0 or above 24 hours are rejected." Test this code and refine it.
def charge(hours):
if hours > 4:
return 5
elif hours > 1:
return 2
else:
return 0
Test plan:
| Data (hours) | Type | Expected | Actual |
|---|---|---|---|
| 0.5 | normal | 0 | 0 |
| 2 | normal | 2 | 2 |
| 6 | normal | 5 | 5 |
| 1 | boundary | 2 | 0 |
| 4 | boundary | 5 | 2 |
| 25 | erroneous | rejected | 5 |
| -1 | erroneous | rejected | 0 |
Errors found:
- Logic error:
>should be>=(1 hour and 4 hours are included in the higher charge). - Missing validation: stays outside 0 to 24 hours aren't rejected.
Refined code:
def charge(hours):
if not 0 <= hours <= 24: # guard clause (9.2.3)
raise ValueError("hours must be between 0 and 24")
if hours >= 4:
return 5
if hours >= 1:
return 2
return 0
assert charge(4) == 5
assert charge(1) == 2
assert charge(0.9) == 0
Re-test: all the boundary tests now pass, and invalid stays are rejected. The solution meets the requirements.
Exam tip: choose test data in all three types, and say what each tests, with expected results worked out first. Boundary values find the commonest errors. When asked to correct code, state the error, the type (syntax, runtime or logic) and the fix. Re-test after changes.
Check yourself
What are the three types of test data?
Normal, boundary and erroneous (invalid).
Which type of error is hardest to find, and why?
A logic error, because the program runs without an error message but gives wrong results.
Give an example of a runtime error
Any one of: dividing by zero; an index out of range; converting text that isn't a number with int(); opening a file that doesn't exist.
What is a desk-check?
Reading through the code and tracing it by hand (often with a trace table), without running it, to find errors.
What does reviewing a solution involve?
Checking it against the requirements and for quality (accuracy, readability, efficiency), then refining it and re-testing.