GCSE Computer Science · public explanation

Testing and Defensive Programming

Choose test data and explain validation, authentication and other safeguards. Use worked GCSE examples to connect checks with the errors they prevent.

Read the explanation and worked examples, then choose your exam board to continue with activities and practice in your account.

OCR J277On the specificationOCR J277 2.3.1–2.3.2

Apply testing and defensive programming using OCR Exam Reference Language or a studied language only when requested.

AQA 8525 (2027+)On the specificationAQA 8525 (2027+) 3.2.11

Apply testing and defensive programming in the precise response format printed on the paper.

Predict, run, compare, repair and retest

A group booking must accept an integer quantity from 2 to 6 inclusive. Predict the required result before running the implemented rule. The original rule accepts quantities greater than 2 and at most 6, so input 2 has expected result Accept but actual result Reject. That mismatch reveals an excluded endpoint: do not rewrite the expectation to hide the fault.

Repair the lower comparison to include 2, then make a new test record. Inputs 2, 4 and 6 are accepted; 7 is rejected. Preserve the original failure separately. These results support the tested cases, not a claim that every possible input or the complete system works.

In a separate fresh saving run, start with no records. An accepted quantity creates one booking record; a rejected quantity creates none. Input 7 therefore leaves the record count at zero. Input 2 creates one record for the request, not two records for two tickets.

Range validation and authentication answer different questions. A permitted quantity passes the range rule. Authentication checks the claimed identity using the supplied username and its associated password. Two requests can both contain the allowed quantity 2 while only one supplied account pair matches. This comparison explains authentication conceptually; a production system needs appropriate password protection.

InputExpectedOriginal actualRepaired actual
2AcceptRejectAccept
4AcceptAcceptAccept
6AcceptNot run in the original recordAccept
7RejectRejectReject

One named limit makes a change easier to maintain

Suppose both desk and mobile booking checks use the same named constant, MAX_TICKETS, whose value is 6. The name explains what the number means; reusing that single definition avoids keeping separate copies of the limit in agreement.

For a new eight-seat session, a developer changes the definition to 8 before the next run. This is an edit to the program's definition, not a constant changing during execution. Both checks now use the new limit. Retest both interfaces: integer quantity 7 should be accepted and 9 rejected under the updated requirement.

This improves maintainability: the program is easier to understand and change consistently. It does not authenticate a user, prove that an input is factually true, or guarantee that the comparisons are correct. A reused wrong rule can affect both interfaces. A named constant helps keep the rule consistent; validation and authentication still have their own separate purposes.

A test plan makes success observable

An expected result is the output required by the specification; an actual result is what the program produces when run. Plan each input and its expectation before execution, then record the actual output and compare them. A mismatch is evidence to investigate, not a reason to rewrite the expectation.

In this worked example, quiz scores are already validated integers from 0 to 100. A score of 50 or higher must produce Pass; a lower score must produce Fail. The faulty condition score > 50 excludes exactly 50. Replacing it with score >= 50 includes equality. Keep the faulty run's observations and record the repaired run separately.

A syntax error breaks the language's grammatical rules, such as a missing required closing parenthesis. A logic error permits execution but produces unintended behaviour. Completing (score > 50) makes that expression grammatical; it does not repair the excluded equality case. The condition is still false at 50.

Rerun the failing case and the previously matching cases after a repair. At 49, an actual output of Fail is correct: it matches the expected Fail. A program's output and the test's success are different things. Several matching tests provide evidence, not a guarantee that every possible behaviour is correct.

InputExpectedActual with > 50Original test checkActual after >= 50 repair
72PassPassMatchPass
50PassFailMismatchPass
49FailFailMatchFail

OCR focus

OCR: testing stages and four data descriptions

A club accepts integer ages 11 to 16 inclusive. Choose inputs to probe different rules, and predict each outcome from the requirement before running the program. A rejected input can be a successful test when rejection is what the requirement demands.

In current OCR J277 v3.1 terminology, normal data should be accepted without causing errors. A typical interior value such as 14 is a useful normal test; an accepted endpoint is not forbidden normal data. Boundary describes the edge-testing purpose: 11 and 16 inspect the included limits. Also check the outside neighbours. Invalid data has the correct type but should be rejected, whereas erroneous data has the wrong type. The text 'eleven' is not the integer 11; this example does not perform text-to-number conversion.

Iterative testing checks modules during development. If an unfinished age validator incorrectly rejects 16, repair it, rerun that case and check earlier cases such as 11 and 14. Keep the original mismatch as evidence rather than replacing it with the rerun result.

Final or terminal testing checks the completed program. In a fresh run with no records, age 16 should be accepted, create one permit and show confirmation. A separate fresh run with age 17 should reject, create no permit and explain the allowed range. A validator-only check would not reveal a later storage mistake. Several matching tests are evidence, not proof of all possible behaviour.

Older past papers retain their own published mark-scheme terminology. For example, the OCR 2024 high-jump question's Erroneous row accepts out-of-range numbers as well as wrong-type data; do not apply a newer label rule retroactively.

Integer age 11 to 16 inclusiveChosen OCR test purposeExpected behaviour
14NormalAccept
11 and 16BoundaryAccept
10 and 17InvalidReject: outside the permitted range
'eleven' (text)ErroneousReject: incorrect data type

AQA focus

AQA: normal, boundary and erroneous data

A festival booking accepts an integer quantity from 4 to 9 inclusive. Predict each expected outcome from that requirement before observing the program. A typical interior value such as 6 is useful normal data: it should be accepted.

AQA boundary (extreme) testing includes the values at and immediately outside both limits. Here choose 3, 4, 9 and 10. Their expected outcomes are Reject, Accept, Accept and Reject. Boundary is the testing purpose, not a promise that the input is accepted. Test all four to expose a missing included endpoint or a wrongly admitted neighbour.

Erroneous input is unsuitable for the stated requirement. Quantity 0 is far below this allowed range; the word 'ten' is text where an integer is required. Both should be rejected in this example. There is no text-to-number conversion here. Outside boundary neighbours are also unsuitable inputs: the descriptions can overlap, so justify the particular behaviour being tested instead of assuming every example has only one possible label.

A program's rejection is not automatically a failed test. If input 3 produces Reject, it matches the requirement. If input 4 produces Reject, it reveals a problem because the included endpoint should be accepted. Keep expected and actual outcomes separate, investigate a mismatch and rerun the tests after correction.

Typed inputChosen AQA test purposeExpected outcome
6 (integer)Normal: typical interior valueAccept
3 (integer)Boundary: just below the minimumReject
4 (integer)Boundary: included minimumAccept
9 (integer)Boundary: included maximumAccept
10 (integer)Boundary: just above the maximumReject
0 (integer)Erroneous: outside the required rangeReject
'ten' (text)Erroneous: wrong data typeReject

Continue with your exam board

The course combines lesson videos, interactive activities, topic questions and original exam mocks. Sign in to use the learning tools and save your progress; your account’s course access applies.

Independent learning resources. Not endorsed by AQA or OCR. These public introductions complement the full course and the official specification.