Java Reference
In-Depth Information
There are two possibilities for the filing status and three tax brackets for each
status. That makes six test cases. Then we want to test error conditions, such as a
negative income. That makes seven test cases. For the first six, we need to
compute manually what answer we expect. For the remaining one, we need to
know what error reports we expect. We write down the test cases and then start
coding.
Should you really test seven inputs for this simple program? You certainly should.
Furthermore, if you find an error in the program that wasn't covered by one of the
test cases, make another test case and add it to your collection. After you fix the
known mistakes, run all test cases again. Experience has shown that the cases that
you just tried to fix are probably working now, but that errors that you fixed two or
three iterations ago have a good chance of coming back! If you find that an error
keeps coming back, that is usually a reliable sign that you did not fully understand
some subtle interaction between features of your program.
It is always a good idea to design test cases before starting to code. There are two
reasons for this. Working through the test cases gives you a better understanding of
the algorithm that you are about to program. Furthermore, it has been noted that
programmers instinctively shy away from testing fragile parts of their code. That
seems hard to believe, but you will often make that observation about your own
work. Watch someone else test your program. There will be times when that
person enters input that makes you very nervous because you are not sure that your
program can handle it, and you never dared to test it yourself. This is a well-known
phenomenon, and making the test plan before writing the code offers some
protection.
A DVANCED T OPIC 5.6: Logging
Sometimes you run a program and you are not sure where it spends its time. To get
a printout of the program flow, you can insert trace messages into the program,
such as this one:
public double getTax()
{
. . .
if (status = = SINGLE)
{
Search WWH ::




Custom Search