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
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
| Iterative testing | Final (terminal) testing | |
|---|---|---|
| When | During development | At the end of development |
| What is tested | Small parts such as one sub program | The complete program |
| Why | Errors are found early, when they are easier to fix | To check everything works together and meets the requirements |
Types of error
Syntax errors
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.
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
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.
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.
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.
| Type | What it is | Example for "a whole number from 1 to 10" | Should the program accept it? |
|---|---|---|---|
| Normal | Typical data that is allowed | 6 | Yes |
| Boundary | The right type of data, on the very edge of what is allowed | 1 and 10 | Yes |
| Invalid | The right type of data, but not allowed | 0, 11 or 250 | No |
| Erroneous | The wrong type of data | "six" or "abc" | No |
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
This program accepts the number of items an order can hold, a whole number from 1 to 20.
| Test | Purpose | Test data | Type of data | Expected result | Actual result | Pass or fail |
|---|---|---|---|---|---|---|
| 1 | Typical number | 12 | Normal | Accepted | ||
| 2 | Lowest allowed | 1 | Boundary | Accepted | ||
| 3 | Highest allowed | 20 | Boundary | Accepted | ||
| 4 | Just below the range | 0 | Invalid | Rejected with an error message | ||
| 5 | Just above the range | 21 | Invalid | Rejected with an error message | ||
| 6 | Wrong data type | ten | Erroneous | Rejected 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.
Refining algorithms
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.
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.