Test Naming Conventions
Problem
You want to define a naming convention for your tests.
Solution
Prefix 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.
Discussion
Consistent 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.[25] 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.
Tip
Another popular convention is to place all test classes in a parallel directory structure. This allows you to use the same Java package names for your tests, while keeping the source files separate. To be honest, we do not like this approach because you must look in two different directories to find files. Ant can easily exclude tests from your build even if they reside in the same source directory.
Finally, a consistent naming convention makes it easier ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access