Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do you put your unit test?

Tags:

unit-testing

People also ask

Where should I put unit tests?

Regression testing Still, even if that's your only reason for unit testing, I think putting the unit tests in a separate library is the best choice: It gives you a clearer separation of concerns. If you put unit tests in your production library, it will blur the distinction between production code and test code.

Where do I put unit tests in Python?

For a file module.py , the unit test should normally be called test_module.py , following Pythonic naming conventions. There are several commonly accepted places to put test_module.py : In the same directory as module.py . In ../tests/test_module.py (at the same level as the code directory).


I favour keeping unit-tests in separate source files in the same directory as production code (#3).

Unit tests are not second-class citizens, their code must maintained and refactored just like production code. If you keep your unit tests in a separate directory, the next developer to change your production code may miss that there are unit tests for it and fail to maintain the tests.

In C++, I tend to have three files per class:

MyClass.h
MyClass.cpp
t_MyClass.cpp

If you're using Vim, then my toggle_unit_tests plug-in for toggling between source and unit test files may prove useful.


The current best practice is to separate the unit tests into their own directory, #1. All of the "convention over configuration" systems do it like this, eg. Maven, Rails, etc.

I think your alternatives are interesting and valid, and the tool support is certainly there to support them. But it's just not that popular (as far as I know). Some people object to having tests interspersed with production code. But it makes sense to me that if you always write unit tests, that they be located right with your code. It just seems simpler.


I always go for #1. While it's nice that they are close together. My reasons are as followed:

  • I feel there's a difference between the core codebase and the unittests. I need a real separation.
  • End-users rarely need to look at unittests. They are just interested in the API. While it's nice that unittests provide a separate view on the code, in practice I feel it won't be used to understand it better. (more descriptive documentation + examples do).
  • Because end-users rarely need unittests, I don't want to confuse them with more files and/or methods.
  • My coding standards are not half as strict for unittests as they are for the core library. This is maybe just my opinion, but I don't care as much for coding standards in my tests.

Hope this helps.