Writing good unit tests, Part 1: Follow your GUTs

Best practices and tools for high-quality test code

1 2 3 4 5 Page 4
Page 4 of 5

Automation

Prepare your tests for automation. Automated tests form the basis of regression testing. You should aim for fully automated tests that can be run without effort or manual intervention. Tools like Ant and Maven 2 can support this task. Gerard Meszaros's book xUnit Test Patterns contains a large section about the importance and realization of automated tests. (I'll write more about this impressive book in the second half of this article.)

Mutation testing

Test your tests with a mutation testing tool. Kent Beck says, "Why just think your tests are good when you can know for sure? Sometimes Jester tells me my tests are airtight, but sometimes the changes it finds come as a bolt out of the blue. Highly recommended." Jester, a tool created by Ivan Moore, finds code that is not covered by tests. Jester will make some change to your code and run your tests; if the tests pass, Jester displays a message saying what it changed. Jester includes a script for generating Web pages that show the changes made that did not cause the tests to fail. The code coverage tools listed in Table 1 indicate code that is not executed by the test suites. In contrast, Jester can give more information about the sort of test that is missing, by showing how the code can be modified and still pass your tests. With Jester, the time-consuming role of a "project saboteur" can be significantly reduced.

Because Jester's setup procedure and usage are not so easy, Moore implemented a follow-up, called simple-jester, which is available for download at Sourceforge. Other open source candidates in the area of mutation testing are MuJava and Jumble. (See the Resources section to learn more about tools for mutation testing.)

Goals

You should set goals for code quality. Anyone on the team who writes test code -- including both developers and pure testers -- must know the quality goals that the team is aiming for. Establish and broadcast a best practice guide that collects all the quality-related items that are important (and agreed) for your team and project. You can't manage what you can't measure, and you need concrete quality criteria that can be checked later on.

Static code analyzers

If you need to improve the quality of your test code, you can find hints about where to look by using static code analyzers. That's the same procedure that you would use for production code. The list of popular open source Java code analyzers at Java-Source.net will get you started; there's also Enerjy, which is now freeware and not mentioned at the URL above. Commercial candidates include KlocWork, Coverity, and Bauhaus Suite. (There are more candidates, of course, but some are especially related to design and architecture, which is not the focus when looking at unit test code.)

In general, I believe that even pure developers should have a basic understanding of systematic test design methods and how to use them. Identifying and implementing the right test cases is the key for effective and efficient testing on every testing level, but it's particularly true for unit testing! To get started -- that is, for a developer to get some practical experience in using test design methods -- it can be useful to collaborate or pair with a good tester.

1 2 3 4 5 Page 4
Page 4 of 5