Skip to content

2.3.2 Testing

GCSE · Paper 2 · Computational thinking, algorithms and programming · Section 2.3 · 15 flashcards · 10 practice questions

Questions

2.3.2 Testing

Nobody writes a perfect program first time. Testing is how a programmer finds the mistakes before the users do. This topic covers why we test, when we test, the two kinds of error, the four kinds of test data, and how to write a test plan. Code is in the OCR Exam Reference Language.

The purpose of testing

Definition
Testing means running a program with chosen data to check that it works as it should and to find any errors.

Testing has three main purposes.

* To find errors (bugs) so they can be fixed.
* To check that the program does what it was designed to do.
* To check that the program is robust, which means it copes with unexpected data without crashing.

Testing can show that errors exist. It cannot prove there are none, so a programmer chooses test data carefully.

Iterative and final testing

Definition
Iterative testing is testing the program again and again while it is being written. After each part is built it is tested, any errors are fixed, and the part is tested again.
Definition
Final testing, also called terminal testing, is testing the whole program at the end of development, once it is finished, to check it meets the requirements.
Iterative testingFinal (terminal) testing
WhenDuring developmentAt the end of development
What is testedSmall parts such as one sub programThe complete program
WhyErrors are found early, when they are easier to fixTo check everything works together and meets the requirements

Types of error

Syntax errors

Definition
A syntax error is a mistake that breaks the rules of the programming language. The program will not run or cannot be translated.

Typical syntax errors are a misspelt keyword, a missing then, or a missing endif. The translator reports them, so they are usually easy to find.

pseudocode
if score >= 50
    print("Pass")
endif

The word then is missing at the end of the first line. The rules of the language are broken.

Logic errors

Definition
A logic error is a mistake in the design of the code. The program runs, but it gives an unexpected or wrong output.

The computer cannot spot a logic error, because the code follows the rules. Only testing shows it. A common cause is the wrong comparison, such as > when >= is needed.

pseudocode
if score > 50 then
    print("Pass")
else
    print("Fail")
endif

If the pass mark is 50 or more, a score of exactly 50 prints Fail. The program runs, but the output is wrong.

Worked example: find and fix

This program should print the total of the numbers 1 to 10.

pseudocode
total = 0
for i = 1 to 9
    total = total + i
next i
print(total)

It prints 45, but the answer should be 55. The loop stops at 9 and misses the number 10. This is a logic error. The fix is to change line 2 to for i = 1 to 10.

Test data

Test data is the input you give a program when you test it. There are four kinds. They are all different, so learn each one.

TypeWhat it isExample for "a whole number from 1 to 10"Should the program accept it?
NormalTypical data that is allowed6Yes
BoundaryThe right type of data, on the very edge of what is allowed1 and 10Yes
InvalidThe right type of data, but not allowed0, 11 or 250No
ErroneousThe wrong type of data"six" or "abc"No
Warning
Invalid and erroneous are not the same. The number 250 is invalid for a range of 1 to 10, because it is a whole number but outside the range. The word "six" is erroneous, because it is the wrong data type.

Choosing test data

  • 1.
    Read the rule. Find the data type and the limits.
  • 2.
    Pick a normal value from the middle of the range.
  • 3.
    Pick the boundary values, the lowest and the highest allowed. Mistakes with < and <= show up here.
  • 4.
    Pick invalid values, such as one just below the lowest and one just above the highest.
  • 5.
    Pick erroneous data, such as text where a number is needed.
  • 6.
    Work out the expected result for each before you run it.

Test plans

Definition
A test plan is a table, written before testing, that lists each test, the data used and the result you expect. You then run the tests and record what actually happens.

This program accepts the number of items an order can hold, a whole number from 1 to 20.

TestPurposeTest dataType of dataExpected resultActual resultPass or fail
1Typical number12NormalAccepted
2Lowest allowed1BoundaryAccepted
3Highest allowed20BoundaryAccepted
4Just below the range0InvalidRejected with an error message
5Just above the range21InvalidRejected with an error message
6Wrong data typetenErroneousRejected with an error message

The expected result comes from the design, not from the program. After running a test, write the actual result. If the two differ, the test fails and there is an error to fix.

Exam Tip
When you complete a test plan, the expected result for normal and boundary data is "accepted". For invalid and erroneous data it is "rejected" or "an error message is shown". Write what the program does, not just "works".

Refining algorithms

Definition
Refining an algorithm means improving it after testing: fixing errors, making it more robust, and making it easier to read or quicker to run.

The cycle goes round like this. Test the program, find a fault, fix it, then test again. Run the old tests too, to check the fix did not break something that used to work.

* Fix syntax errors found by the translator.
* Fix logic errors by tracing the code with a trace table to see where the values go wrong.
* Add validation if invalid or erroneous data caused a failure.
* Tidy the code with better names, indentation and comments.

Key Point
Testing and refining go together. Every error you find leads to a change, and every change needs testing again.

Exam technique

* State or give: one short fact, such as "A syntax error breaks the rules of the language."
* Describe or explain: say what the idea is, then why it matters. For testing, link it to finding errors early or checking the requirements.
* Compare iterative and final testing: write about both, use a word such as "whereas", and mention when each happens.
* Complete a test plan: name the type of each piece of test data and give the expected result. Check that normal, boundary, invalid and erroneous are not mixed up.
* Find and fix errors: give the line number, say what is wrong, say whether it is a syntax or a logic error, and write the corrected line in the reference language.