How to run the unittest test from & ldquo; python test & rdquo;


I'm trying to figure out how to get python test to run the equivalent of python -m unittest discover. I don't want to use a script and I don't want to use any external test tools (like nose or py.test). It's OK if the solution only works on python 2.7.

In, I think I need to add something to the test_suite and/or test_loader fields in config, but I can't seem to find a combination that works correctly:

config = {
    'name': name,
    'version': version,
    'url': url,
    'test_suite': '???',
    'test_loader': '???',

Is this possible using only unittest built into python 2.7?

FYI, my project structure looks like this:

  tests/ <- I want to delete this

Update: This is possible with unittest2 but I want find something equivalent using only unittest


unittest2 includes a very basic setuptools compatible test collector. Specify test_suite = 'unittest2.collector' in your This starts test discovery with the default parameters from the directory containing, so it is perhaps most useful as an example (see unittest2/

For now, I'm just using a script called, but I'm hoping I can get rid of this by moving to a solution that only uses python test.

Here's the I'm hoping to remove:

import unittest

if __name__ == '__main__':

    # use the default shared TestLoader instance
    test_loader = unittest.defaultTestLoader

    # use the basic test runner that outputs to sys.stderr
    test_runner = unittest.TextTestRunner()

    # automatically discover all tests in the current dir of the form test*.py
    # NOTE: only works for python 2.7 and later
    test_suite ='.')

    # run the test suite

From Building and Distributing Packages with Setuptools (emphasis mine):


A string naming a unittest.TestCase subclass (or a package or module containing one or more of them, or a method of such a subclass), or naming a function that can be called with no arguments and returns a unittest.TestSuite.

Hence, in you would add a function that returns a TestSuite:

import unittest
def my_test_suite():
    test_loader = unittest.TestLoader()
    test_suite ='tests', pattern='test_*.py')
    return test_suite

Then, you would specify the command setup as follows: