How can I force python setup.py test
to use the unittest2
package for testing instead of the built-in unittest
package?
Asked
Active
Viewed 1,465 times
4

argentpepper
- 4,202
- 3
- 33
- 45
1 Answers
2
Let's assume you have a directory called tests
which contains an __init__.py
file which defines a function called suite
which returns a test suite.
My solution is to replace the default python setup.py test
command with my own test
command which uses unittest2
:
from setuptools import Command
from setuptools import setup
class run_tests(Command):
"""Runs the test suite using the ``unittest2`` package instead of the
built-in ``unittest`` package.
This is necessary to override the default behavior of ``python setup.py
test``.
"""
#: A brief description of the command.
description = "Run the test suite (using unittest2)."
#: Options which can be provided by the user.
user_options = []
def initialize_options(self):
"""Intentionally unimplemented."""
pass
def finalize_options(self):
"""Intentionally unimplemented."""
pass
def run(self):
"""Runs :func:`unittest2.main`, which runs the full test suite using
``unittest2`` instead of the built-in :mod:`unittest` module.
"""
from unittest2 import main
# I don't know why this works. These arguments are undocumented.
return main(module='tests', defaultTest='suite',
argv=['tests.__init__'])
setup(
name='myproject',
...,
cmd_class={'test': run_tests}
)
Now running python setup.py test
runs my custom test
command.

argentpepper
- 4,202
- 3
- 33
- 45
-
3However, using this solution requires manually installing `unittest2`, since `python setup.py test` no longer automatically installs packages from the `tests_require` list in `setup()`. – argentpepper Apr 12 '12 at 05:39