How do I run all Python unit tests in a directory?


I have a directory that contains my Python unit tests. Each unit test module is of the form test_*.py. I am attempting to make a file called that will, you guessed it, run all files in the aforementioned test form and return the result. I have tried two methods so far; both have failed. I will show the two methods, and I hope someone out there knows how to actually do this correctly.

For my first valiant attempt, I thought “If I just import all my testing modules in the file, and then call this unittest.main() doodad, it will work, right?” Well, turns out I was wrong.

import glob
import unittest

testSuite = unittest.TestSuite()
test_file_strings = glob.glob('test_*.py')
module_strings = [str[0:len(str)-3] for str in test_file_strings]

if __name__ == "__main__":

This did not work, the result I got was:

$ python 

Ran 0 tests in 0.000s


For my second try, I though, ok, maybe I will try to do this whole testing thing in a more “manual” fashion. So I attempted to do that below:

import glob
import unittest

testSuite = unittest.TestSuite()
test_file_strings = glob.glob('test_*.py')
module_strings = [str[0:len(str)-3] for str in test_file_strings]
[__import__(str) for str in module_strings]
suites = [unittest.TestLoader().loadTestsFromName(str) for str in module_strings]
[testSuite.addTest(suite) for suite in suites]
print testSuite 

result = unittest.TestResult()
print result

#Ok, at this point I have a result
#How do I display it as the normal unit test command line output?
if __name__ == "__main__":

This also did not work, but it seems so close!

$ python 
<unittest.TestSuite tests=[<unittest.TestSuite tests=[<unittest.TestSuite tests=[<test_main.TestMain testMethod=test_respondes_to_get>]>]>]>
<unittest.TestResult run=1 errors=0 failures=0>

Ran 0 tests in 0.000s


I seem to have a suite of some sort, and I can execute the result. I am a little concerned about the fact that it says I have only run=1, seems like that should be run=2, but it is progress. But how do I pass and display the result to main? Or how do I basically get it working so I can just run this file, and in doing so, run all the unit tests in this directory?

Asked By: Stephen Cagle



You could use a test runner that would do this for you. nose is very good for example. When run, it will find tests in the current tree and run them.


Here’s some code from my pre-nose days. You probably don’t want the explicit list of module names, but maybe the rest will be useful to you.

testmodules = [

suite = unittest.TestSuite()

for t in testmodules:
        # If the module defines a suite() function, call it to get the suite.
        mod = __import__(t, globals(), locals(), ['suite'])
        suitefn = getattr(mod, 'suite')
    except (ImportError, AttributeError):
        # else, just load all the test cases from the module.

Answered By: Ned Batchelder

Well by studying the code above a bit (specifically using TextTestRunner and defaultTestLoader), I was able to get pretty close. Eventually I fixed my code by also just passing all test suites to a single suites constructor, rather than adding them “manually”, which fixed my other problems. So here is my solution.

import glob
import unittest

test_files = glob.glob('test_*.py')
module_strings = [test_file[0:len(test_file)-3] for test_file in test_files]
suites = [unittest.defaultTestLoader.loadTestsFromName(test_file) for test_file in module_strings]
test_suite = unittest.TestSuite(suites)
test_runner = unittest.TextTestRunner().run(test_suite)

Yeah, it is probably easier to just use nose than to do this, but that is besides the point.

Answered By: Stephen Cagle

If you want to run all the tests from various test case classes and you’re happy to specify them explicitly then you can do it like this:

from unittest import TestLoader, TextTestRunner, TestSuite
from uclid.test.test_symbols import TestSymbols
from uclid.test.test_patterns import TestPatterns

if __name__ == "__main__":

    loader = TestLoader()
    tests = [
        for test in (TestSymbols, TestPatterns)
    suite = TestSuite(tests)

    runner = TextTestRunner(verbosity=2)

where uclid is my project and TestSymbols and TestPatterns are subclasses of TestCase.

Answered By: demented hedgehog

I have used the discover method and an overloading of load_tests to achieve this result in a (minimal, I think) number lines of code:

def load_tests(loader, tests, pattern):
''' Discover and load all unit tests in all files named ``*`` in ``./src/``
    suite = TestSuite()
    for all_test_suite in'src', pattern='*'):
        for test_suite in all_test_suite:
    return suite

if __name__ == '__main__':

Execution on fives something like

Ran 27 tests in 0.187s
Answered By: rds

With Python 2.7 and higher you don’t have to write new code or use third-party tools to do this; recursive test execution via the command line is built-in. Put an in your test directory and:

python -m unittest discover <test_directory>
# or
python -m unittest discover -s <directory> -p '*'

You can read more in the python 2.7
or python 3.x unittest documentation.

Update for 2021:

Lots of modern python projects use more advanced tools like pytest. For example, pull down matplotlib or scikit-learn and you will see they both use it.

It is important to know about these newer tools because when you have more than 7000 tests you need:

  • more advanced ways to summarize what passes, skipped, warnings, errors
  • easy ways to see how they failed
  • percent complete as it is running
  • total run time
  • ways to generate a test report
  • etc etc
Answered By: Travis Bear

I tried various approaches but all seem flawed or I have to makeup some code, that’s annoying. But there’s a convinient way under linux, that is simply to find every test through certain pattern and then invoke them one by one.

find . -name 'Test*py' -exec python '{}' ;

and most importantly, it definitely works.

Answered By: zinking

Based on the answer of Stephen Cagle I added support for nested test modules.

import fnmatch
import os
import unittest

def all_test_modules(root_dir, pattern):
    test_file_names = all_files_in(root_dir, pattern)
    return [path_to_module(str) for str in test_file_names]

def all_files_in(root_dir, pattern):
    matches = []

    for root, dirnames, filenames in os.walk(root_dir):
        for filename in fnmatch.filter(filenames, pattern):
            matches.append(os.path.join(root, filename))

    return matches

def path_to_module(py_file):
    return strip_leading_dots( 

def strip_extension(py_file):
    return py_file[0:len(py_file) - len('.py')]

def replace_slash_by_dot(str):
    return str.replace('\', '.').replace('/', '.')

def strip_leading_dots(str):
    while str.startswith('.'):
       str = str[1:len(str)]
    return str

module_names = all_test_modules('.', '*')
suites = [unittest.defaultTestLoader.loadTestsFromName(mname) for mname 
    in module_names]

testSuite = unittest.TestSuite(suites)
runner = unittest.TextTestRunner(verbosity=1)

The code searches all subdirectories of . for * files which are then loaded. It expects each * to contain a single class *Tests(unittest.TestCase) which is loaded in turn and executed one after another.

This works with arbitrary deep nesting of directories/modules, but each directory in between needs to contain an empty file at least. This allows the test to load the nested modules by replacing slashes (or backslashes) by dots (see replace_slash_by_dot).

Answered By: Peter Kofler

I use PyDev/LiClipse and haven’t really figured out how to run all tests at once from the GUI. (edit: you right click the root test folder and choose Run as -> Python unit-test

This is my current workaround:

import unittest

def load_tests(loader, tests, pattern):

if __name__ == '__main__':

I put this code in a module called all in my test directory. If I run this module as a unittest from LiClipse then all tests are run. If I ask to only repeat specific or failed tests then only those tests are run. It doesn’t interfere with my commandline test runner either (nosetests) — it’s ignored.

You may need to change the arguments to discover based on your project setup.

Answered By: Dunes

Because Test discovery seems to be a complete subject, there is some dedicated framework to test discovery :

More reading here :

Answered By: Bactisme

Here is my approach by creating a wrapper to run tests from the command line:

#!/usr/bin/env python3
import os, sys, unittest, argparse, inspect, logging

if __name__ == '__main__':
    # Parse arguments.
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument("-?", "--help",     action="help",                        help="show this help message and exit" )
    parser.add_argument("-v", "--verbose",  action="store_true", dest="verbose",  help="increase output verbosity" )
    parser.add_argument("-d", "--debug",    action="store_true", dest="debug",    help="show debug messages" )
    parser.add_argument("-h", "--host",     action="store",      dest="host",     help="Destination host" )
    parser.add_argument("-b", "--browser",  action="store",      dest="browser",  help="Browser driver.", choices=["Firefox", "Chrome", "IE", "Opera", "PhantomJS"] )
    parser.add_argument("-r", "--reports-dir", action="store",   dest="dir",      help="Directory to save screenshots.", default="reports")
    parser.add_argument('files', nargs='*')
    args = parser.parse_args()

    # Load files from the arguments.
    for filename in args.files:

    # See:
    def make_suite(tc_class):
        testloader = unittest.TestLoader()
        testnames = testloader.getTestCaseNames(tc_class)
        suite = unittest.TestSuite()
        for name in testnames:
            suite.addTest(tc_class(name, cargs=args))
        return suite

    # Add all tests.
    alltests = unittest.TestSuite()
    for name, obj in inspect.getmembers(sys.modules[__name__]):
        if inspect.isclass(obj) and name.startswith("FooTest"):

    # Set-up logger
    verbose = bool(os.environ.get('VERBOSE', args.verbose))
    debug   = bool(os.environ.get('DEBUG', args.debug))
    if verbose or debug:
        logging.basicConfig( stream=sys.stdout )
        root = logging.getLogger()
        root.setLevel(logging.INFO if verbose else logging.DEBUG)
        ch = logging.StreamHandler(sys.stdout)
        ch.setLevel(logging.INFO if verbose else logging.DEBUG)
        ch.setFormatter(logging.Formatter('%(asctime)s %(levelname)s: %(name)s: %(message)s'))

    # Run tests.
    result = unittest.TextTestRunner(verbosity=2).run(alltests)
    sys.exit(not result.wasSuccessful())

For sake of simplicity, please excuse my non-PEP8 coding standards.

Then you can create BaseTest class for common components for all your tests, so each of your test would simply look like:

from BaseTest import BaseTest
class FooTestPagesBasic(BaseTest):
    def test_foo(self):
        driver = self.driver
        driver.get(self.base_url + "/")

To run, you simply specifying tests as part of the command line arguments, e.g.:

./ -h tests/**/*.py
Answered By: kenorb

In case of a packaged library or application, you don’t want to do it. setuptools will do it for you.

To use this command, your project’s tests must be wrapped in a unittest test suite by either a function, a TestCase class or method, or a module or package containing TestCase classes. If the named suite is a module, and the module has an additional_tests() function, it is called and the result (which must be a unittest.TestSuite) is added to the tests to be run. If the named suite is a package, any submodules and subpackages are recursively added to the overall test suite.

Just tell it where your root test package is, like:

    # ...
    test_suite = 'somepkg.test'

And run python test.

File-based discovery may be problematic in Python 3, unless you avoid relative imports in your test suite, because discover uses file import. Even though it supports optional top_level_dir, but I had some infinite recursion errors. So a simple solution for a non-packaged code is to put the following in of your test package (see load_tests Protocol).

import unittest

from . import foo, bar

def load_tests(loader, tests, pattern):
    suite = unittest.TestSuite()

    return suite
Answered By: saaj

This is now possible directly from unittest:

import unittest
loader = unittest.TestLoader()
start_dir = 'path/to/your/test/files'
suite =

runner = unittest.TextTestRunner()
Answered By: slaughter98

In python 3, if you’re using unittest.TestCase:

  • You must have an empty (or otherwise) file in your test directory (must be named test/)
  • Your test files inside test/ match the pattern test_*.py.
    • They can be inside a subdirectory under test/. Those subdirs can be named as anything, but they all need to have an file in them

Then, you can run all the tests with:

python -m unittest

Done! A solution less than 100 lines. Hopefully another python beginner saves time by finding this.

Answered By: tmck-code

This BASH script will execute the python unittest test directory from ANYWHERE in the file system, no matter what working directory you are in: its working directory always be where that test directory is located.

ALL TESTS, independent $PWD

unittest Python module is sensitive to your current directory, unless you tell it where (using discover -s option).

This is useful when staying in the ./src or ./example working directory and you need a quick overall unit test:

dirname="`dirname $this_program`"
readlink="`readlink -e $dirname`"

python -m unittest discover -s "$readlink"/test -v

SELECTED TESTS, independent $PWD

I name this utility file: and use it like this: <test-python-filename-minus-dot-py-fileextension>
dirname="`dirname $this_program`"
readlink="`readlink -e $dirname`"

(cd "$dirname"/test; python -m unittest $1)

No need for a test/ file to burden your package/memory-overhead during production.

Answered By: John Greene

This is an old question, but what worked for me now (in 2019) is:

python -m unittest *

All my test files are in the same folder as the source files and they end with _test.

Answered By: Plasty Grove

I just created a file in my base test directory and added import statements for anything in my sub directories. Then discover is able to find all my tests in those directories by running it on

python -m unittest discover ./test -p '*.py'
# /test/
import unittest

from test.package1.mod1 import XYZTest
from test.package1.package2.mod2 import ABCTest

if __name__ == "__main__"
Answered By: Aaron

I have no package and as mentioned on this page, this is creating issue while issing dicovery. So, I used the following solution. All the test result will be put in a given output folder.

The given script is executing all the Unit Test of the project stored at the
path %relativePath2Src% currently fixed coded for the given project. 

    - Anaconda should be install
    - For the current user, an enviornment called "mtToolsEnv" should exists
    - xmlrunner Library should be installed

import sys
import os
import xmlrunner
from Repository import repository 

outputTestReportFolder=os.path.dirname(os.path.abspath(__file__))+r'test-reports' #subfolder in current file path

class UTTesting():
    Class tto run all the UT of the project
    def __init__(self):
        Initiate instance


        self.projectRepository = repository() 
        self.UTfile = [] #List all file
    def retrieveAllUT(self):
        Generate the list of UT file in the project


        #self.projectRepository.printAllFile() #debug
        for file2scan in self.projectRepository.devfile:
            if file2scan.endswith(""):
    def runUT(self,UTtoRun):
        Run a single UT

        UTtoRun : String
            File Name of the UT


        if UTtoRun in self.projectRepository.devfilepath:
            UTtoRunPath = os.path.join(UTtoRunFolderPath, UTtoRun)
        #set the correct execution context & run the test
        os.system(" cd " + UTtoRunFolderPath + 
                  " & " + pythonPath + " " + UTtoRunPath + " " + outputTestReportFolder )
    def runAllUT(self):
        Run all the UT contained in self
        The function "retrieveAllUT" sjould ahve been performed before


        for UTfile in self.UTfile:
if __name__ == "__main__":

In my project specific, I have a class that I used in other script. This might be an overkill for your usecase.

import os

class repository():
    Class that decribed folder and file in a repository 
    def __init__(self):
        Initiate instance


        self.devfile = [] #List all file
        self.devfilepath = {} #List all file paths

    def retriveAllFilePaths(self,pathrepo):
        Retrive all files and their path in the class

        pathrepo : Path used for the parsin


        for path, subdirs, files in os.walk(pathrepo):
            for file_name in files:
                self.devfilepath[file_name] = path
    def printAllFile(self):
        Display all file with paths

        def printAllFile : TYPE


        for file_loop in self.devfile:

In your test files, you need to have a main like this:

if __name__ == "__main__":
    import xmlrunner
    import sys
    if len(sys.argv) > 1:
        outputFolder = sys.argv.pop() #avoid conflic with unittest.main
        outputFolder = r'test-reports'
    print("Report will be created and store there: " + outputFolder)
Answered By: White

Encountered the same issue.

The solution is to add an empty to each folder and uses python -m unittest discover -s

Project Structure


And running the command

python -m unittest discover -s tests/domain

To get the expected outcome

Ran 1 test in 0.007s
Answered By: mondayrris
To me it sounds like you just want to have a seperate process. Sadly I'm not familiar with Python 2, but in Python 3 you'll want to use the multiprocessing module for this.

Since I'm unfamiliar with Python 2, here's how you'd call and execute a function foo of a file in an individual process from a file
def foo(x):`enter code here`
    print(x)`enter code here`
from one import foo
import multiprocessing

if __name__ == '__main__':
    p1 = multiprocessing.Process(target=foo, args=('my_argument',))
    p1.join()  # optional if you want to wait for the termination of p1
In this example foo just prints the value, but of course you can do whatever you want inside that function.

You can also use to run the unit tests.
Answered By: Prince Siboi