Are unit tests kept in the same file as the code, a separate file in the same directory, or in an entirely different directory?
The usual project layout is to have a separate directory with tests, with the tests also subdivided by what they are testing.
I always place my unit tests in a subdirectory to the related code called test.
For example: /libs/authentication, the tests would be placed in /libs/authentication/tests
for each project there is a test project
Example naming
main project
- Company.Project.Area
main project testing
- Company.Project.Area.Test
I prefer to keep them in a seperate directory, usually called either "unittests" or just "tests". I then play games in the Makefile to have to automatically handle this directory, if it exists.
It's a little bit of a pain to set up, but I personally prefer not to have the unit tests cluttering up the functional code. This way they are "close" enough to be obvious, but not in your face all the time.
We keep a separate directory with a parallel class hierarchy. The unit test class name being Test[ClassNameUnderTest]. Should multiple test classes be needed, they are postfixed with an _ and additional text.
I keep a separate test source tree that mimics the package structure of my source tree.
Example:
/src/main/java/com/xyz/MyClass.java
/src/test/java/com/xyz/MyClassTest.java
With this structure you can test package level methods.
This question has already been asked for a while ago (with a lot more in-depth answers):