Every non-trivial program contains errors when it is first written. Professionals reduce how many they make, then use a planned mix of testing methods to expose the rest before users meet them. Learn the three types of error, how to locate and correct them, and when to use each testing method.
A syntax error breaks the grammar rules of the language, so the translator cannot translate the code. A logic error is a mistake in the algorithm: the program runs but gives the wrong result. A run-time error happens while the program is running and makes it crash or hang, for example dividing by zero or using an array index outside its bounds. Faults are avoided by good design and programming practice and exposed by testing: dry runs, walkthroughs, white-box and black-box testing, stub and integration testing, then alpha, beta and acceptance testing.
The type of an error depends on when and how it shows itself (Fig. 1). You must be able to locate an error in code, say which type it is and correct it.
| Type | What it is | Found by | Examples |
|---|---|---|---|
| Syntax | A statement does not follow the rules of the language | The compiler or interpreter (and IDE), before or as the code is translated | Misspelt keyword, missing ENDIF or NEXT, unmatched bracket, missing THEN |
| Logic | The algorithm is wrong; the program runs but the output is incorrect | Only by testing with known expected results, or a dry run | > instead of >=, wrong loop limits, total not set to 0, wrong formula |
| Run-time | An error during execution that the program cannot handle, so it crashes or hangs | Running the program with data that triggers it | Division by zero, array index out of range, reading past the end of a file, infinite loop |
FUNCTION CountPasses(Marks : ARRAY[1:30] OF INTEGER) RETURNS INTEGER
DECLARE Index, Passes : INTEGER
Passes ← 0
FOR Index ← 0 TO 30
IF Marks[Index] > 50 THEN
Passes ← Passes + 1
ENDIF
ENDFOR
RETURN Passes
ENDFUNCTION
ENDFOR is not a keyword: a FOR loop must end with NEXT. This is a syntax error; correct it to NEXT Index.FOR Index ← 0 TO 30 uses Marks[0], which is outside the array bounds 1 to 30. This crashes the program while it runs: a run-time error. Correct it to FOR Index ← 1 TO 30.Marks[Index] > 50 does not count a mark of exactly 50 as a pass. The program runs but returns the wrong count: a logic error. Correct it to Marks[Index] >= 50.A program that crashes is not a logic error. If it stops while running (divide by zero, index out of range), it is a run-time error; if it finishes but gives the wrong answer, it is a logic error.
Each program in the lab has one logic error. Run it, compare the output with what it should be, then find the line that is wrong: a dry run with a trace table is the quickest way.
N > 0 before dividing, or validating N on inputFour quick checks. Each correct answer earns XP and records this skill on your profile.
A program runs and finishes but outputs the wrong total. What type of error is this?
Which error is reported by the compiler or interpreter when it translates the code?
In the lab, which is the quickest way to find the wrong line in a program that runs but gives the wrong answer?
What is a stub?
No single method finds every fault, so a test strategy uses several, at different points in development. Learn what each one is and when it is used.
| Method | What it is |
|---|---|
| Dry run | Working through an algorithm or code by hand, recording variable values in a trace table, without running it on a computer. |
| Walkthrough | The programmer talks a group of peers through the design or code line by line, so others can spot errors. |
| White-box | Testing with knowledge of the code: test data is chosen to make every path through the code (every branch and loop) execute. |
| Black-box | Testing without looking at the code: inputs are chosen from the specification and the actual outputs are compared with the expected outputs. |
| Stub | A module not yet written is replaced by a simple dummy (stub) that, for example, outputs a message or returns a fixed value, so the modules that call it can be tested. |
| Integration | Modules that have been tested separately are combined and tested together, to check they work together and pass data correctly. |
| Alpha | Testing of a nearly complete program by the developer's own staff (in-house), before it is released to anyone outside. |
| Beta | A nearly finished version is released to a limited group of real users outside the company, who use it in normal conditions and report faults. |
| Acceptance | The client or end users test the finished program to check it meets the requirements specification, before they accept it and sign it off. |
// stubs used while the real modules are still being written
PROCEDURE OutputAverage(Total : INTEGER)
OUTPUT "OutputAverage called, Total = ", Total
ENDPROCEDURE
FUNCTION GetMark() RETURNS INTEGER
RETURN 75
ENDFUNCTION
Alpha and beta are not about version numbers. Alpha testing is done in-house by the developer's staff; beta testing is done by real users outside the company.
Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.
A program uses Total / Count and stops with an error when Count is 0. What type of error is this?
Which describes black-box testing?
Which testing is carried out by a limited group of real users outside the developer's company?
Modules A and B each passed their own tests, but the program fails when A calls B. Which testing should have found this?
This skill is now recorded on your profile. Keep the chain going.