You are on page 1of 11

Introduction to Programming

FSC1331/FDN1120
Topic 11 : Testing and Debugging

Topic’s / Unit’s Learning Outcome:


1. Program Errors
2. Program Testing
3. Debugging

17/01/2016 1
10/06/2015 4
11.1 Program Errors

• Most large programs contain errors.


• Creating error-free code is very difficult.
• There are three types of program errors:
– syntax errors
– run-time errors
– logic errors

17/01/2016 2
11.1 Program Errors

• A syntax error is a violation of the rules of the


programming language.

printf(“Hello”)

*Missing semicolon

17/01/2016 3
11.1 Program Errors

• A logic error in a program is any code that causes


incorrect output/results even though the program
runs to completion.

If (age > 18)


printf(“Below 18”)

*Wrong output

17/01/2016 4
11.1 Program Errors

• A run-time error is an error that results from using


invalid operand values for an operation.

Average = Total / 0;

*Infinite result

17/01/2016 5
11.2 Program Testing

• Testing is the process of finding errors ("bugs") in a


program.
• Testing can increase your confidence that a program
is error-free.
• Testing can find the presence of errors, but, in
general, cannot prove the absence of errors.
• Testing small programs is much easier than testing
large programs.

17/01/2016 6
11.2 Program Testing

• White-box testing indicates that we can “see” or


examine the code as we develop test cases
• Black-box testing indicates that we cannot examine
the code as we devise test cases

Testing Error?

Yes
Debug

17/01/2016 7
11.3 Debugging

• Debugging is the process of locating and correcting


errors in a program.
• Debugging is problem-solving and often can be very
challenging.
• Thinking carefully about your program is often the
best first step when debugging.

17/01/2016 8
11.3 Debugging

• You can help minimize your time spent debugging


by:
– Starting with a good program design,
– Coding carefully, and
– Testing your program as you write.
• The development of good debugging skills comes
from experience. Therefore, make sure you learn
from your mistakes.

17/01/2016 9
11.3 Debugging

• It often helps to have someone else look at your


code when debugging.
• General rule for all debugging:
– "When in doubt, print it out"
• A symbolic debugger is a tool for debugging code
that allows you to examine your program's behavior
as it executes.

17/01/2016 10
11.3 Debugging

• Debugging techniques
– Incremental testing
– Sanity checks “confirm/validate/accuracy of logic codes
and to continue testing”
– Boolean constants for turning debugging code on or off
“two states/value”
– Error variables for controlling program behaviour after
errors “measure deviation”
– Traceback techniques

17/01/2016 11

You might also like