Java Reference
In-Depth Information
Maxim 3. Test 0, 1, and many
When thinking about testing a loop, we interpret maxim 3 to mean that we
should provide test cases in which a loop will execute 0 iterations, 1 iteration, and
more than 1 iteration. If a program deals with an array or list of some sort, we
should consider test cases for which the array (or list) has 0 elements, 1 element,
and many elements.
Structural testing involves one new, extremely important, maxim:
Maxim 6. Develop test cases that provide test coverage .
A group of test cases provides test coverage if exercising them will cause
every single part of the program to be executed at least once. When developing
test cases using the functional approach, we cannot look at the method body, so
there is no way to know whether complete test coverage has been achieved. So,
maxim 6 is an important addition.
Here is an example that actually happened to one of us. We wrote the func-
tion of Fig. 14.3. This method is so simple that we thought we might skip testing
it. But we tested it anyway, thoroughly, trying as test cases the arguments 2
through 9 . All these test cases were necessary because we had to make sure that
each return statement was executed at least once. It was only when looking at the
output of the testing phase that we noticed the typo: we had misspelled forty .
As another example of test coverage, look at the function in Fig. 14.4. To
provide test coverage, the then-part of each if-statement has to be executed. So,
we look at calls of method anglicize with arguments (say) 125 , 50 , 15 , and 5 .
Approaches to testing
In Secs. 14.1.3 and 14.1.4, we introduced two approaches to exercising test
cases: the use of a test driver , which is a method that is written to perform the
tests, and the use of JUnit, an application that has been written to facilitate test-
ing. There are other ways to exercise test cases, some of which we explore here.
Use DrJava's Interactions pane
In some cases, it is sufficient to test a method (as soon as it is written) using
DrJava's Interactions pane. Suppose the method is static . Then you can write
many calls on it in the Interactions pane and see the answer immediately.
The advantage of using the Interactions pane is that it can be done quickly
and without the overhead of writing a test driver or writing a subclass of class
TestCases (in order to use JUnit).
The disadvantage of using the Interactions pane is that it is not easily repeat-
able. If we make a change in the method and want to test it again, we must man-
ually type in the calls to the method and check the answers. Using a test driver
or JUnit takes more time in the beginning, but, in the end, it saves time because
the same tests can be repeated whenever we want.
Search WWH ::

Custom Search