Running unittest with typical test directory structure Running unittest with typical test directory structure python python

Running unittest with typical test directory structure


The best solution in my opinion is to use the unittest command line interface which will add the directory to the sys.path so you don't have to (done in the TestLoader class).

For example for a directory structure like this:

new_project├── antigravity.py└── test_antigravity.py

You can just run:

$ cd new_project$ python -m unittest test_antigravity

For a directory structure like yours:

new_project├── antigravity│   ├── __init__.py         # make it a package│   └── antigravity.py└── test    ├── __init__.py         # also make test a package    └── test_antigravity.py

And in the test modules inside the test package, you can import the antigravity package and its modules as usual:

# import the packageimport antigravity# import the antigravity modulefrom antigravity import antigravity# or an object inside the antigravity modulefrom antigravity.antigravity import my_object

Running a single test module:

To run a single test module, in this case test_antigravity.py:

$ cd new_project$ python -m unittest test.test_antigravity

Just reference the test module the same way you import it.

Running a single test case or test method:

Also you can run a single TestCase or a single test method:

$ python -m unittest test.test_antigravity.GravityTestCase$ python -m unittest test.test_antigravity.GravityTestCase.test_method

Running all tests:

You can also use test discovery which will discover and run all the tests for you, they must be modules or packages named test*.py (can be changed with the -p, --pattern flag):

$ cd new_project$ python -m unittest discover$ # Also works without discover for Python 3$ # as suggested by @Burrito in the comments$ python -m unittest

This will run all the test*.py modules inside the test package.


The simplest solution for your users is to provide an executable script (runtests.py or some such) which bootstraps the necessary test environment, including, if needed, adding your root project directory to sys.path temporarily. This doesn't require users to set environment variables, something like this works fine in a bootstrap script:

import sys, ossys.path.insert(0, os.path.dirname(__file__))

Then your instructions to your users can be as simple as "python runtests.py".

Of course, if the path you need really is os.path.dirname(__file__), then you don't need to add it to sys.path at all; Python always puts the directory of the currently running script at the beginning of sys.path, so depending on your directory structure, just locating your runtests.py at the right place might be all that's needed.

Also, the unittest module in Python 2.7+ (which is backported as unittest2 for Python 2.6 and earlier) now has test discovery built-in, so nose is no longer necessary if you want automated test discovery: your user instructions can be as simple as python -m unittest discover.


I generally create a "run tests" script in the project directory (the one that is common to both the source directory and test) that loads my "All Tests" suite. This is usually boilerplate code, so I can reuse it from project to project.

run_tests.py:

import unittestimport test.all_teststestSuite = test.all_tests.create_test_suite()text_runner = unittest.TextTestRunner().run(testSuite)

test/all_tests.py (from How do I run all Python unit tests in a directory?)

import globimport unittestdef create_test_suite():    test_file_strings = glob.glob('test/test_*.py')    module_strings = ['test.'+str[5:len(str)-3] for str in test_file_strings]    suites = [unittest.defaultTestLoader.loadTestsFromName(name) \              for name in module_strings]    testSuite = unittest.TestSuite(suites)    return testSuite

With this setup, you can indeed just include antigravity in your test modules. The downside is you would need more support code to execute a particular test... I just run them all every time.


matomo