[ Team LiB ] |
4.11 Test Naming Conventions4.11.1 ProblemYou want to define a naming convention for your tests. 4.11.2 SolutionPrefix each test case classname with a consistent word, such as "Test" or "UnitTest". Put test cases in the same directory as the classes they are testing. 4.11.3 DiscussionConsistent naming conventions serve two purposes. First, they make your code more maintainable. Second, consistency facilitates automation. The tests in this chapter are prefixed with "Test", resulting in names like TestGame, TestPerson, and TestAccount. These correspond to the Game, Person, and Account classes, respectively. Writing one test case per class makes it very easy to glance at a directory and see which tests are available. Putting "Test" at the beginning of the filenames makes sorting easier, in our opinion, particularly when your IDE provides some sort of jump-to functionality.[8] All tests will be grouped together and easy to identify. On the other hand, putting "Test" at the end of the filenames does make it easier to identify which classes do not have tests.
Finally, a consistent naming convention makes it easier to locate tests when using Ant for your builds. You might want to exclude all of your tests when you create a production build of your software. You can do this in Ant as follows: <javac destdir="${dir.build}">
<src path="${dir.src}"/>
<!-- see how easy it is to exclude the tests from a build! -->
<exclude name="**/Test*.java"/>
</javac>
4.11.4 See AlsoRecipe 3.16 shows how to run tests using Ant based on naming conventions. Recipe 3.14 shows how to exclude test classes from a build. |
[ Team LiB ] |