Run unittest from Python program via command line option

The Python unittest module contains its own test discovery function, which you can run from the command line:

$ python -m unittest discover

To run this command from within your module, you can use the subprocess module:

#!/usr/bin/env python

import sys
import subprocess

# ...
# the rest of your module's code
# ...

if __name__ == '__main__':
    if '--unittest' in sys.argv:
        subprocess.call([sys.executable, '-m', 'unittest', 'discover'])

If your module has other command-line options you probably want to look into argparse for more advanced options.


Perhaps this is what you're looking for. Implement a load_tests function in test_prog.py and use the following code in prog.py to load and run the tests:

import unittest
import test.test_prog
suite = unittest.TestLoader().loadTestsFromModule(test.test_prog)
unittest.TextTestRunner().run(suite)

You must make sure that you consistently follow some naming conventions (which you seem to be doing):

  1. All tests are named with the same prefix (test_ is the norm), followed by the name of the module you wish to test.

    prog.py => test_prog.py

  2. Tests reside in test/ directory.

Then you can do something like this:

prog.py

import sys
...
... do module stuff here...
...

if __name__ == "__main__":

    # Check if we want to run the tests for this file
    if "--unittest" in sys.argv:
        import unittest

        test_filename = 'test_' + __file__
        test_directory = 'test'

        suite = unittest.TestLoader().discover(test_directory, pattern=test_filename)
        unittest.TextTestRunner(verbosity=2).run(suite)

What we are doing, is:

  • Checking the command arguments to see if --unittest is present (since that's the only time you want to run the tests).

  • If it is, then we create the test_prog.py - following the naming conventions we have set.

  • Then we pass that to the TestLoader().discover function.

    discover(...) starts at the specified directory and finds all test modules (recursing into subdirectories ) that match the pattern provided.

    In our case, it will look inside the test/ directory for any module named test_prog.py. When it does, it loads it and creates a TestSuite with the TestCases that we want to run.

  • Lastly, we manually test unittest to run the suite obtained in the previous step.


Normally, unittest will do all of this for us in the background, but since we are trying to run a specific test module, we have to tell exactly how and where to get it from.

Also, note that you will have to do this for every file where you want to do this at.