views:

397

answers:

7

Are unit tests kept in the same file as the code, a separate file in the same directory, or in an entirely different directory?

+1  A: 

The usual project layout is to have a separate directory with tests, with the tests also subdivided by what they are testing.

Ants Aasma
+11  A: 

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

alexn
A: 

for each project there is a test project

Example naming

main project

  • Company.Project.Area

main project testing

  • Company.Project.Area.Test
Konstantinos
+3  A: 

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.

Chris Arguin
A: 

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.

Jim Rush
A: 

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.

BacMan
+2  A: 

This question has already been asked for a while ago (with a lot more in-depth answers):

Rene Saarsoo