I’m giving an internal presentation today on mock objects and I prepared a slide on the “Qualities of a Good Unit Test.” I actually couldn’t find much in the way of resources on the web for this, so I made up the list below. I’d love to hear from the rest of you what your best practices are for unit tests.
Unit Tests Should Be Atomic
A unit test should only test a small piece of functionality. This falls inline with the idea of getting into a rhythm of “write a little test, write a little code.” A litmus test is to ask yourself if any part of the unit test could stand alone in a separate unit test. Another feedback loop for your unit testing quality is the amount of time you spend with the debugger. If your unit tests are coarse, a test failure is more difficult to find. If the unit test exercises a small amount of code, the test failure cause can usually be spotted very quickly.
Order Independent and Isolated
There should never be an order dependency, intentional or not, between unit tests. Problems arise when one unit test leaves some kind of dirty data laying around. Testing against a database or some sort of static cache is a common culprit. The theme of the day was mocking (actually Episode III), so put evil stateful things behind nice, deterministic mocks or stubs.
A unit test can, and should, be a valuable form of documentation. At best, a unit test should explain the intended usage and function of a class. At worst, the unit test should still be easy to debug in the case of a regression failure. Excessive data setup can obfuscate a unit test beyond any hope of comprehension.
Easy to Setup
How easily can I set up an environment to run the tests? If the answer is “not very,” I might never get the tests running. As much as possible, decouple unit tests from external dependencies like databases and web services by using mocks or stubs. When you do test against an external dependency, the dependency better be setup correctly by the automated build.
You’re never going to run the test just once. For the sake of constant feedback, successful continuous integration, and your sanity, make sure your tests run quickly. Thirty minute build and unit test cycles are nothing but friction. Just to keep in the mock rut, mock things that make network calls to speed up your testing. I like to segregate connected tests into a seperate assembly. This way you can quickly run the majority of your unit tests on demand without the lag from network calls.
There’s surely some obvious things I’m missing, but I’m short on sleep (and a midnight movie awaits) at the moment so that’s all I’ve got.