Is there a consensus about the best place to put Python unittests?
Should the unittests be included within the same module as the functionality being tested (executed wh
tests/
subdirectory in your package for larger projects.It's a matter of what works best for the project you're creating.
Sometimes the libraries you're using determine where tests should go, as is the case with Django (where you put your tests in models.py
, tests.py
or a tests/
subdirectory in your apps).
If there are no existing constraints, it's a matter of personal preference. For a small set of modules, it may be more convenient to put the unittests in the files you're creating.
For anything more than a few modules I create the tests separately in a tests/
directory in the package. Having testing code mixed with the implementation adds unnecessary noise for anyone reading the code.