There are several best practices that unit testing frameworks should follow. These seemingly minor improvements in the TestCalculator program hightlight three rules that(in our experience) all unit testing frameworks should observe:
- Each unit test must run independently of all other unit tests.
- Errors must be detected and reported test by test.
- It must be easy to define which unit tests will run.
The "slightly better" test program comes close to following these rules but still falls short. For example, in order to for each unit test to be truly independent, each should run in a different classloader instance.
Adding a class is also only slightly better. You can now add new unit tests by adding a new method and then adding a corresponding try/catch block to main.
A definite step up, but still short of what you would want in a real unit test suite. The most obvious problem is that large try/catch blocks are known to be maintenance nightmares. You could easily leave a unit test out and never know it wasn't running!
It would be nice if you could just add new test methods and be done with it. But how would the program know which methods to run?
Well, you could have simple registration procedure. A registration method would at least inventory which tests are running.
Another approach would be to use Java's reflection and introspection capabilities. A program could look at itself and decide to run whatever methods are named in a certain way - like those that begin with the letters test, for example.
Making it easy to add tests(the third rul in our earlier list) sounds like another good rule for a unit testing framework.
The support code to realize this rule(via registration or introspection) would not be trivial, but it would be worthwhile. There would be a lot of work up front, but that effort would pay off each time you added a new test.
Happily, the JUnit team has saved you the trouble. The JUnit framework already supports registering or introspecting methods. It also supports using a different classloader instance for each test, and reports all errors on a case-by-case basis.