Does unittest allow single case/suite testing through "setup.py test"?

setup.py test

setup.py test is not that flexible, but here's an alternative:

The unittest module can run specific test methods

From the Documentation on unittest

The unittest module can be used from the command line to run tests from modules, classes or even individual test methods:

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method

You can pass in a list with any combination of module names, and fully qualified class or method names.

You can run tests with more detail (higher verbosity) by passing in the -v flag:

python -m unittest -v test_module

For a list of all the command-line options:

python -m unittest -h

The setup.py test runner is rather limited; it only supports letting you specify a specific module. The documentation for the command-line switches is given when you use the --help switch:

python setup.py test --help
Common commands: (see '--help-commands' for more)
  [ ... cut ... ]

Options for 'test' command:
  --test-module (-m)  Run 'test_suite' in specified module
  --test-suite (-s)   Test suite to run (e.g. 'some_module.test_suite')

  [ ... more cut ... ]

so python setup.py test -m your.package.tests.test_module would limit running the tests from the test_module.py file only.

All the test command does, really, is make sure your egg has been built already, extract the test_suite value from setup() metadata, configure a test loader that understands about zipped eggs, then run the unittest.main() function.

If you need to run a single test only, have already built your egg, are not running this with a zipped egg, then you can also just use the unittest command line interface, which does pretty much everything else:

python -m unittest yourpackage.tests.TestClass.test_method

would instruct unittest to only run a very specific test method.


You guys are all wrong, setup.py test can be used with the -s option the same way python -m unittest does:

cd root_of_your_package
python setup.py test -s tests.TestClass.test_method