 |
Testing and Debugging
|
Reading: Chapter 7
Categories of errors
Compilation errors
-
Syntax error: an error in the "grammar" of the program.
-
missing semicolon
-
omitting commas in argument lists
-
not matching parentheses
-
Semantic error: an error in the meaning of the program.
-
using a variable before it has been assigned a value
-
not declaring a variable
-
using variables/values of the wrong type
-
e.g., using a String where an int is expected
Runtime errors
-
Division by zero, sending a message to an object that has no value
Logic errors
-
Errors that aren't easily apparent, e.g., not solving the problem correctly
-
e.g., writing some code that increments a value by two rather than doubles
it
-
Compilation errors are discovered at compilation time, and so it is easy
to find these errors, and the cause. Runtime and logic errors are much
more difficult to uncover.
Test Drivers
-
A test driver is a program that tests a class.
-
Testing is a critical aspect of developing software
-
after deployment, it is harder to do testing
-
if writing a class that others will use, you need to know that it works
correctly
-
creating test cases is best done when the class is fresh in your mind
class Date {
...
public void testDriver () {
Date d1 = new Date (31,12,1998, true);
System.out.print ("The next date should be 1/1/1999: ");
d1.nextDay (); d1.print ();
Date d2 = new Date (28,2, 2000, true);
System.out.print ("The next date should be 3/1/2000: ");
d2.nextDay (); d2.print ();
Date d3 = new Date (28, 2, 1996, true);
System.out.print ("The next date should be 2/29/1996: ");
d3.nextDay (); d3.print ();
if (d1.lessThan (d3)) {
System.out.println ("There is an error in lessThan.");
} else {
System.out.println ("lessThan works for:");
d1.print ();
d3.print ();
}
...
}
}
What to test:
-
Test all behaviors (methods)
-
Find a logical order, (e.g., test constructor before anything else, test
setting methods before more complex methods)
-
Make sure each statement has been executed at least once
-
e.g., in Date, test for equal and not equal dates, leap years, different
months, ...
-
Look for and test special cases
-
Don't worry about test efficiency
Debugging
-
Once an error is discovered, it can be difficult to track down
-
e.g., we know lessThan is wrong, but where in lessThan does it go wrong?
-
Tracking down the location of an error and fixing it is called debugging.
-
Put trace statements in that print out values at various points, to track
down the error more finely
-
e.g., output the value that the months, days and years have, and find out
which branch of the if is wrong
Last modified: 2/21/99