mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Adds a verbosity keyword argument to unittest.main plus a minor fix allowing you to specify test modules / classes
from the command line. Closes issue 5995. Michael Foord
This commit is contained in:
parent
b1d45856c2
commit
5d31e057c5
4 changed files with 58 additions and 16 deletions
|
@ -91,6 +91,31 @@ need to derive from a specific class.
|
||||||
`python-mock <http://python-mock.sourceforge.net/>`_ and `minimock <http://blog.ianbicking.org/minimock.html>`_
|
`python-mock <http://python-mock.sourceforge.net/>`_ and `minimock <http://blog.ianbicking.org/minimock.html>`_
|
||||||
Tools for creating mock test objects (objects simulating external resources).
|
Tools for creating mock test objects (objects simulating external resources).
|
||||||
|
|
||||||
|
Command Line Interface
|
||||||
|
----------------------
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
.. versionchanged:: 27
|
||||||
|
In earlier versions it was only possible to run individual test methods and not modules
|
||||||
|
or classes.
|
||||||
|
|
||||||
.. _unittest-minimal-example:
|
.. _unittest-minimal-example:
|
||||||
|
|
||||||
Basic example
|
Basic example
|
||||||
|
@ -178,7 +203,6 @@ The above examples show the most commonly used :mod:`unittest` features which
|
||||||
are sufficient to meet many everyday testing needs. The remainder of the
|
are sufficient to meet many everyday testing needs. The remainder of the
|
||||||
documentation explores the full feature set from first principles.
|
documentation explores the full feature set from first principles.
|
||||||
|
|
||||||
|
|
||||||
.. _organizing-tests:
|
.. _organizing-tests:
|
||||||
|
|
||||||
Organizing test code
|
Organizing test code
|
||||||
|
@ -1408,7 +1432,7 @@ Loading and running tests
|
||||||
subclasses to provide a custom ``TestResult``.
|
subclasses to provide a custom ``TestResult``.
|
||||||
|
|
||||||
|
|
||||||
.. function:: main([module[, defaultTest[, argv[, testRunner[, testLoader[, exit]]]]]])
|
.. function:: main([module[, defaultTest[, argv[, testRunner[, testLoader[, exit, [verbosity]]]]]]])
|
||||||
|
|
||||||
A command-line program that runs a set of tests; this is primarily for making
|
A command-line program that runs a set of tests; this is primarily for making
|
||||||
test modules conveniently executable. The simplest use for this function is to
|
test modules conveniently executable. The simplest use for this function is to
|
||||||
|
@ -1417,6 +1441,12 @@ Loading and running tests
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
||||||
|
You can run tests with more detailed information by passing in the verbosity
|
||||||
|
argument::
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main(verbosity=2)
|
||||||
|
|
||||||
The *testRunner* argument can either be a test runner class or an already
|
The *testRunner* argument can either be a test runner class or an already
|
||||||
created instance of it. By default ``main`` calls :func:`sys.exit` with
|
created instance of it. By default ``main`` calls :func:`sys.exit` with
|
||||||
an exit code indicating success or failure of the tests run.
|
an exit code indicating success or failure of the tests run.
|
||||||
|
@ -1432,4 +1462,4 @@ Loading and running tests
|
||||||
This stores the result of the tests run as the ``result`` attribute.
|
This stores the result of the tests run as the ``result`` attribute.
|
||||||
|
|
||||||
.. versionchanged:: 2.7
|
.. versionchanged:: 2.7
|
||||||
The ``exit`` parameter was added.
|
The ``exit`` and ``verbosity`` parameters were added.
|
||||||
|
|
|
@ -3280,19 +3280,22 @@ class Test_TestProgram(TestCase):
|
||||||
|
|
||||||
runner = FakeRunner()
|
runner = FakeRunner()
|
||||||
|
|
||||||
try:
|
oldParseArgs = TestProgram.parseArgs
|
||||||
oldParseArgs = TestProgram.parseArgs
|
def restoreParseArgs():
|
||||||
TestProgram.parseArgs = lambda *args: None
|
|
||||||
TestProgram.test = test
|
|
||||||
|
|
||||||
program = TestProgram(testRunner=runner, exit=False)
|
|
||||||
|
|
||||||
self.assertEqual(program.result, result)
|
|
||||||
self.assertEqual(runner.test, test)
|
|
||||||
|
|
||||||
finally:
|
|
||||||
TestProgram.parseArgs = oldParseArgs
|
TestProgram.parseArgs = oldParseArgs
|
||||||
|
TestProgram.parseArgs = lambda *args: None
|
||||||
|
self.addCleanup(restoreParseArgs)
|
||||||
|
|
||||||
|
def removeTest():
|
||||||
del TestProgram.test
|
del TestProgram.test
|
||||||
|
TestProgram.test = test
|
||||||
|
self.addCleanup(removeTest)
|
||||||
|
|
||||||
|
program = TestProgram(testRunner=runner, exit=False, verbosity=2)
|
||||||
|
|
||||||
|
self.assertEqual(program.result, result)
|
||||||
|
self.assertEqual(runner.test, test)
|
||||||
|
self.assertEqual(program.verbosity, 2)
|
||||||
|
|
||||||
|
|
||||||
class FooBar(unittest.TestCase):
|
class FooBar(unittest.TestCase):
|
||||||
|
|
|
@ -1524,7 +1524,8 @@ Examples:
|
||||||
"""
|
"""
|
||||||
def __init__(self, module='__main__', defaultTest=None,
|
def __init__(self, module='__main__', defaultTest=None,
|
||||||
argv=None, testRunner=TextTestRunner,
|
argv=None, testRunner=TextTestRunner,
|
||||||
testLoader=defaultTestLoader, exit=True):
|
testLoader=defaultTestLoader, exit=True,
|
||||||
|
verbosity=1):
|
||||||
if isinstance(module, basestring):
|
if isinstance(module, basestring):
|
||||||
self.module = __import__(module)
|
self.module = __import__(module)
|
||||||
for part in module.split('.')[1:]:
|
for part in module.split('.')[1:]:
|
||||||
|
@ -1535,7 +1536,7 @@ Examples:
|
||||||
argv = sys.argv
|
argv = sys.argv
|
||||||
|
|
||||||
self.exit = exit
|
self.exit = exit
|
||||||
self.verbosity = 1
|
self.verbosity = verbosity
|
||||||
self.defaultTest = defaultTest
|
self.defaultTest = defaultTest
|
||||||
self.testRunner = testRunner
|
self.testRunner = testRunner
|
||||||
self.testLoader = testLoader
|
self.testLoader = testLoader
|
||||||
|
@ -1566,6 +1567,7 @@ Examples:
|
||||||
return
|
return
|
||||||
if len(args) > 0:
|
if len(args) > 0:
|
||||||
self.testNames = args
|
self.testNames = args
|
||||||
|
self.module = None
|
||||||
else:
|
else:
|
||||||
self.testNames = (self.defaultTest,)
|
self.testNames = (self.defaultTest,)
|
||||||
self.createTests()
|
self.createTests()
|
||||||
|
@ -1598,4 +1600,5 @@ main = TestProgram
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
sys.modules['unittest'] = sys.modules['__main__']
|
||||||
main(module=None)
|
main(module=None)
|
||||||
|
|
|
@ -428,6 +428,12 @@ Library
|
||||||
|
|
||||||
- Issue #3379: unittest.main now takes an optional exit argument. If False main
|
- Issue #3379: unittest.main now takes an optional exit argument. If False main
|
||||||
doesn't call sys.exit allowing it to be used from the interactive interpreter.
|
doesn't call sys.exit allowing it to be used from the interactive interpreter.
|
||||||
|
|
||||||
|
- Issue #5995: unittest.main now takes an optional verbosity argument allowing
|
||||||
|
test modules to be run with a higher than default verbosity.
|
||||||
|
|
||||||
|
- Issue 5995: A fix to allow you to run "python -m unittest test_module" or
|
||||||
|
"python -m unittest test_module.TestClass" from the command line.
|
||||||
|
|
||||||
- Issue #5728: unittest.TestResult has new startTestRun and stopTestRun methods;
|
- Issue #5728: unittest.TestResult has new startTestRun and stopTestRun methods;
|
||||||
called immediately before and after a test run.
|
called immediately before and after a test run.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue