Maxims and aphorisms . A maxim is a general truth, fundamental principal, or rule of conduct.
A synonym of maxim is aphorism , which is a concise statement of a principle
or a terse formulation of a truth or sentiment.
It is impossible to test a method that is not well specified. You can read the
method body, but how do you know that it does what it is supposed to do? The
first job of a tester who encounters such a method is to find the person who wrote
the method, find out what it is supposed to do, and then clarify the documenta-
tion. Really, they have no other choice.
We assume throughout, then, that methods and classes are well specified.
Five maxims for creating test cases
There are two steps in testing a program: (1) test cases have to be generated; (2)
the program has to be tested with the test cases. The following maxims will guide
you not only in generating test cases and testing but also in programming itself.
Their application will help you write good comments for methods and variables.
Maxim 1. Test early and often.
The sooner you test a method, the sooner you will find and fix bugs. In fact,
as soon as you have written a method specification, you should write an
example of a call to that method. This means that you will be thinking about
how the method might be used even before you write the method body. (And
it will help you write better documentation.)
Maxim 2. Test only one thing at a time.
If you test several things at once and the test fails, you will have a harder
time figuring out why. Also, when testing several things at once, it is easier
to lose track of what you are testing and harder to make sure that you have
covered all the cases.
Maxim 3. Test 0, 1, and many.
This maxim can be applied to the size of both the input and the result. Many
usually means 2 or 3, although 5 or 10 is not unreasonable, depending on
what is being tested. Why 0? Well, for example, when implementing code
involving a list of items, it is easy to forget that the list might be empty (has
0 items). Why 1? It is the smallest “typical” case. If a bug is detected with
size 1, it is usually easier to debug that case than on a longer list.
Maxim 4. Test null, beginning, middle, and end.
When implementing a method, it is easy to overlook what might happen if a
parameter is null . Test at the beginning and end of the input because it is
easy to forget about the two extremes while writing code. Test in the middle
because it is the “regular” case.