Information Technology Topic

Testing and Debugging Programs

  1. Read notes
  2. Flashcards
  3. Quiz
  4. Confident

Study focus - 3 things

  1. Testing means running the program with test data and comparing its output with results predicted beforehand; debugging is finding and fixing the errors that testing shows
  2. Good test data has normal values, boundary values (at the edges of what is allowed or where a decision changes) and invalid values, and the predicted result for each is worked out before the run
  3. A dry run with a trace table follows the program by hand, and the three kinds of error show up differently: syntax errors before the run, run-time errors as the program stops, logic errors as wrong answers

Section 1Testing and debugging

A program that compiles is not necessarily a program that works. Testing is running the program with chosen test data and comparing the results with the results you predicted. Debugging is finding the cause of any error that testing shows and fixing it. In the steps of building an application, the program is tested and debugged before it is documented and handed over. There are two things to check. The design (the algorithm, flowchart or pseudocode) can be followed by hand before any code is typed. The coded program is then run with test data. Here's the trick: testing can only show that an error exists. Debugging is the separate job of finding where it is.

Section 2Test data: normal, boundary and invalid

Test data is a set of values chosen to run the program with, and the correct result for each value is worked out BEFORE the program is run. That predicted result is what the real output is compared with. There are three kinds of test data. Take a program that reads a mark from 0 to 100 and shows Pass for 50 or more: - Normal data is an ordinary valid value, such as 65 (Pass) or 20 (Fail). - Boundary data sits on an edge: the limits of the allowed range, 0 and 100, and either side of the point where the decision changes, 49 (Fail) and 50 (Pass). - Invalid data should not be accepted at all, such as 105 or -3. The predicted result is that the program rejects it. Values just outside the limits, such as -1 and 101, are invalid data too, and they make good tests of the range check. Easy to mix up: boundary data is still valid data, it just sits on an edge. Invalid data is outside what the program should accept. Why not just one normal test? Because many errors live at the edges. A single mark of 65 would pass even a program with the wrong comparison.

Section 3A test plan for a small program

A test plan lists each test value, its kind, the predicted result and the actual result. Here is the Pascal-style program PassOrFail: program PassOrFail; var mark : integer; begin readln(mark); if mark >= 50 then writeln('Pass') else writeln('Fail') end. Test 65, normal: predicted Pass, actual Pass. Test 20, normal: predicted Fail, actual Fail. Test 50, boundary: predicted Pass, actual Pass. Test 49, boundary: predicted Fail, actual Fail. Test 0 and 100, boundary: predicted Fail and Pass, actual Fail and Pass. Test 105, invalid: predicted rejected, actual Pass. The last test fails. The program is correct for sensible marks but has no check for impossible ones, so 105 is accepted. A test that fails is useful: it points at a weakness to fix. If the comparison had been mistyped as mark > 50, the test with 65 would still pass. Only the boundary test 50 would show Fail where Pass was predicted.

Section 4The dry run and the trace table

A dry run is following the program by hand, line by line, without running it on the computer. The values of the variables are written in a trace table, one row at the start and one after each pass of a loop. Here is a Pascal-style program to dry run: program Doubling; var value : integer; steps : integer; begin value := 3; steps := 0; while value < 40 do begin value := value * 2; steps := steps + 1 end; writeln('Steps: ', steps); writeln('Value: ', value) end. Start: value 3, steps 0 Pass 1: value 6, steps 1 Pass 2: value 12, steps 2 Pass 3: value 24, steps 3 Pass 4: value 48, steps 4 Then 48 < 40 is FALSE and the loop ends. The program displays Steps: 4 and Value: 48. A dry run needs no computer, so it can be used on the design before the code exists, and it shows the real values rather than the ones you hoped for.

Section 5Syntax, run-time and logic errors

Errors show themselves in three different ways. A syntax error breaks the rules of the language, for example a missing then or := written as =. The compiler finds it before the program runs, and the program will not run until it is fixed. Watch out: the line the compiler reports is not always the line with the mistake, because it may only notice the problem a little later. A run-time error appears while the program is running and usually stops it. The line is written correctly, but it cannot be carried out. Dividing by zero is one example (Free Pascal error 200), and using an array index outside its range is another (error 201, when range checking is on). A logic error lets the program run to the end with no message, but the answer is wrong. The compiler cannot see it because the code is legal. Only testing against predicted results, or a dry run, shows it. Examples in Pascal-style code: if mark >= 50 writeln('Pass') is a syntax error (then is missing). average := total div count with count equal to 0 is a run-time error. for i := 1 to 4 do when five marks must be added is a logic error.

Section 6Debugging techniques

Once testing shows that something is wrong, work towards the cause. - Read the error message. It names the kind of error and gives a line number to start from. - Do a dry run with a trace table, and compare each row with what you expected. The first row that is wrong is close to the bug. - Add temporary writeln lines that display the variables at chosen points, so you can see what the program is really doing. Remove them afterwards. - Use the step-through facility of the programming environment, if there is one, to run one line at a time. - Check the prediction as well. Sometimes the program is right and the arithmetic used to predict the result was wrong. - Change one thing at a time, then run all the tests again. A fix can break something that used to work. Here's the trick: if the output is wrong, ask which line first makes a variable hold a value you did not expect.

Section 7Finding a logic error: the sales total

A tuck shop in Portmore wants the total of four sales. The sales are 250, 400, 150 and 300, so the predicted total is 250 + 400 + 150 + 300 = 1100. This Pascal-style program is run: program SalesTotal; var sale : integer; total : integer; count : integer; begin total := 0; for count := 1 to 3 do begin readln(sale); total := total + sale end; writeln('Total: ', total) end. It displays Total: 800. That does not match the predicted 1100, so testing has found an error. There is no error message, so it is not a syntax or run-time error. It is a logic error. A dry run shows why: count 1, sale 250, total 250; count 2, sale 400, total 650; count 3, sale 150, total 800. The loop stops after three passes and the fourth sale, 300, is never read. The fix is to change 3 to 4 in the loop heading. Run the test again: the program now displays Total: 1100.

Examples

A teacher in Kingston uses the PassOrFail program in the notes, which shows Pass for a mark of 50 or more and Fail otherwise. The marks must be from 0 to 100. She tests it with these values: 65, 50, 49, 0, 100 and 105. (a) Classify each value as normal, boundary or invalid. (b) State the predicted result for each value. (c) State what the program actually displays for each value. (d) Explain what the result for 105 shows about the program.

(a) 65 is normal. 50 and 49 are boundary values, either side of the pass mark. 0 and 100 are boundary values, the limits of the range. 105 is invalid, because it is above 100. (b) 65: Pass. 50: Pass. 49: Fail. 0: Fail. 100: Pass. 105: rejected, because it is not a valid mark. (c) The program displays Pass for 65, Pass for 50, Fail for 49, Fail for 0, Pass for 100 and Pass for 105. (d) For 105 the actual result, Pass, does not match the predicted result, rejected. The program has no check on the range, so it accepts a mark that is impossible. The program works for valid marks but needs a validation step added.

A tuck shop in Portmore wants the total of four sales: 250, 400, 150 and 300. The SalesTotal program in the notes, with for count := 1 to 3 do, is run with those four values typed in. (a) State the predicted total. (b) State the total the program displays. (c) Complete a trace table showing count, sale and total after each pass. (d) State the kind of error, with a reason, and say how to fix and retest it.

(a) 250 + 400 + 150 + 300 = 1100. (b) The program displays Total: 800. (c) Pass 1: count 1, sale 250, total 250. Pass 2: count 2, sale 400, total 650. Pass 3: count 3, sale 150, total 800. The loop ends after pass 3, so the sale of 300 is never read. (d) It is a logic error. The program compiled and ran to the end with no message, but the answer does not match the prediction. The loop runs one pass too few. Change the heading to for count := 1 to 4 do and run the test again. The program then displays Total: 1100, which matches the prediction.

Now let's water it

Session runs notes to flashcards to quiz back-to-back.

Where this comes from