Where do the Python unit tests go? Where do the Python unit tests go? python python

Where do the Python unit tests go?


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:

  1. In the same directory as module.py.
  2. In ../tests/test_module.py (at the same level as the code directory).
  3. In tests/test_module.py (one level under the code directory).

I prefer #1 for its simplicity of finding the tests and importing them. Whatever build system you're using can easily be configured to run files starting with test_. Actually, the default unittest pattern used for test discovery is test*.py.


Only 1 test file

If there has only 1 test files, putting it in a top-level directory is recommended:

module/    lib/        __init__.py        module.py    test.py

Run the test in CLI

python test.py

Many test files

If has many test files, put it in a tests folder:

module/    lib/        __init__.py        module.py    tests/        test_module.py        test_module_function.py
# test_module.pyimport unittestfrom lib import moduleclass TestModule(unittest.TestCase):    def test_module(self):        passif __name__ == '__main__':    unittest.main()

Run the test in CLI

# In top-level /module/ folderpython -m tests.test_modulepython -m tests.test_module_function

Use unittest discovery

unittest discovery will find all test in package folder.

Create a __init__.py in tests/ folder

module/    lib/        __init__.py        module.py    tests/        __init__.py        test_module.py        test_module_function.py

Run the test in CLI

# In top-level /module/ folder# -s, --start-directory (default current directory)# -p, --pattern (default test*.py)python -m unittest discover

Reference

Unit test framework


A common practice is to put the tests directory in the same parent directory as your module/package. So if your module was called foo.py your directory layout would look like:

parent_dir/  foo.py  tests/

Of course there is no one way of doing it. You could also make a tests subdirectory and import the module using absolute import.

Wherever you put your tests, I would recommend you use nose to run them. Nose searches through your directories for tests. This way, you can put tests wherever they make the most sense organizationally.


matomo