Issue 10470: 'python -m unittest' launches test discovery by default.(If you need to pass options to test discovery the discover subcommand must still be specified explicitly.)

This commit is contained in:
Michael Foord 2010-11-21 21:28:01 +00:00
parent 47c74ac977
commit 086f30815c
5 changed files with 54 additions and 14 deletions

View File

@ -208,6 +208,10 @@ You can run tests with more detail (higher verbosity) by passing in the -v flag:
python -m unittest -v test_module python -m unittest -v test_module
When executed without arguments :ref:`unittest-test-discovery` is started::
python -m unittest
For a list of all the command-line options:: For a list of all the command-line options::
python -m unittest -h python -m unittest -h
@ -266,6 +270,12 @@ used from the command line. The basic command-line usage is::
cd project_directory cd project_directory
python -m unittest discover python -m unittest discover
.. note::
As a shortcut, ``python -m unittest`` is the equivalent of
``python -m unittest discover``. If you want to pass arguments to test
discovery the `discover` sub-command must be used explicitly.
The ``discover`` sub-command has the following options: The ``discover`` sub-command has the following options:
.. program:: unittest discover .. program:: unittest discover

View File

@ -109,7 +109,8 @@ class TestProgram(object):
sys.exit(2) sys.exit(2)
def parseArgs(self, argv): def parseArgs(self, argv):
if len(argv) > 1 and argv[1].lower() == 'discover': if ((len(argv) > 1 and argv[1].lower() == 'discover') or
(len(argv) == 1 and self.module is None)):
self._do_discovery(argv[2:]) self._do_discovery(argv[2:])
return return

View File

@ -5,6 +5,18 @@ import sys
import unittest import unittest
class TestableTestProgram(unittest.TestProgram):
module = '__main__'
exit = True
defaultTest = failfast = catchbreak = buffer = None
verbosity = 1
progName = ''
testRunner = testLoader = None
def __init__(self):
pass
class TestDiscovery(unittest.TestCase): class TestDiscovery(unittest.TestCase):
# Heavily mocked tests so I can avoid hitting the filesystem # Heavily mocked tests so I can avoid hitting the filesystem
@ -195,8 +207,7 @@ class TestDiscovery(unittest.TestCase):
test.test_this_does_not_exist() test.test_this_does_not_exist()
def test_command_line_handling_parseArgs(self): def test_command_line_handling_parseArgs(self):
# Haha - take that uninstantiable class program = TestableTestProgram()
program = object.__new__(unittest.TestProgram)
args = [] args = []
def do_discovery(argv): def do_discovery(argv):
@ -208,13 +219,27 @@ class TestDiscovery(unittest.TestCase):
program.parseArgs(['something', 'discover', 'foo', 'bar']) program.parseArgs(['something', 'discover', 'foo', 'bar'])
self.assertEqual(args, ['foo', 'bar']) self.assertEqual(args, ['foo', 'bar'])
def test_command_line_handling_discover_by_default(self):
program = TestableTestProgram()
program.module = None
args = []
def do_discovery(argv):
args.extend(argv)
program._do_discovery = do_discovery
program.parseArgs(['something'])
self.assertEqual(args, [])
program.parseArgs(['something'])
self.assertEqual(args, [])
def test_command_line_handling_do_discovery_too_many_arguments(self): def test_command_line_handling_do_discovery_too_many_arguments(self):
class Stop(Exception): class Stop(Exception):
pass pass
def usageExit(): def usageExit():
raise Stop raise Stop
program = object.__new__(unittest.TestProgram) program = TestableTestProgram()
program.usageExit = usageExit program.usageExit = usageExit
with self.assertRaises(Stop): with self.assertRaises(Stop):
@ -223,7 +248,7 @@ class TestDiscovery(unittest.TestCase):
def test_command_line_handling_do_discovery_calls_loader(self): def test_command_line_handling_do_discovery_calls_loader(self):
program = object.__new__(unittest.TestProgram) program = TestableTestProgram()
class Loader(object): class Loader(object):
args = [] args = []
@ -237,49 +262,49 @@ class TestDiscovery(unittest.TestCase):
self.assertEqual(Loader.args, [('.', 'test*.py', None)]) self.assertEqual(Loader.args, [('.', 'test*.py', None)])
Loader.args = [] Loader.args = []
program = object.__new__(unittest.TestProgram) program = TestableTestProgram()
program._do_discovery(['--verbose'], Loader=Loader) program._do_discovery(['--verbose'], Loader=Loader)
self.assertEqual(program.test, 'tests') self.assertEqual(program.test, 'tests')
self.assertEqual(Loader.args, [('.', 'test*.py', None)]) self.assertEqual(Loader.args, [('.', 'test*.py', None)])
Loader.args = [] Loader.args = []
program = object.__new__(unittest.TestProgram) program = TestableTestProgram()
program._do_discovery([], Loader=Loader) program._do_discovery([], Loader=Loader)
self.assertEqual(program.test, 'tests') self.assertEqual(program.test, 'tests')
self.assertEqual(Loader.args, [('.', 'test*.py', None)]) self.assertEqual(Loader.args, [('.', 'test*.py', None)])
Loader.args = [] Loader.args = []
program = object.__new__(unittest.TestProgram) program = TestableTestProgram()
program._do_discovery(['fish'], Loader=Loader) program._do_discovery(['fish'], Loader=Loader)
self.assertEqual(program.test, 'tests') self.assertEqual(program.test, 'tests')
self.assertEqual(Loader.args, [('fish', 'test*.py', None)]) self.assertEqual(Loader.args, [('fish', 'test*.py', None)])
Loader.args = [] Loader.args = []
program = object.__new__(unittest.TestProgram) program = TestableTestProgram()
program._do_discovery(['fish', 'eggs'], Loader=Loader) program._do_discovery(['fish', 'eggs'], Loader=Loader)
self.assertEqual(program.test, 'tests') self.assertEqual(program.test, 'tests')
self.assertEqual(Loader.args, [('fish', 'eggs', None)]) self.assertEqual(Loader.args, [('fish', 'eggs', None)])
Loader.args = [] Loader.args = []
program = object.__new__(unittest.TestProgram) program = TestableTestProgram()
program._do_discovery(['fish', 'eggs', 'ham'], Loader=Loader) program._do_discovery(['fish', 'eggs', 'ham'], Loader=Loader)
self.assertEqual(program.test, 'tests') self.assertEqual(program.test, 'tests')
self.assertEqual(Loader.args, [('fish', 'eggs', 'ham')]) self.assertEqual(Loader.args, [('fish', 'eggs', 'ham')])
Loader.args = [] Loader.args = []
program = object.__new__(unittest.TestProgram) program = TestableTestProgram()
program._do_discovery(['-s', 'fish'], Loader=Loader) program._do_discovery(['-s', 'fish'], Loader=Loader)
self.assertEqual(program.test, 'tests') self.assertEqual(program.test, 'tests')
self.assertEqual(Loader.args, [('fish', 'test*.py', None)]) self.assertEqual(Loader.args, [('fish', 'test*.py', None)])
Loader.args = [] Loader.args = []
program = object.__new__(unittest.TestProgram) program = TestableTestProgram()
program._do_discovery(['-t', 'fish'], Loader=Loader) program._do_discovery(['-t', 'fish'], Loader=Loader)
self.assertEqual(program.test, 'tests') self.assertEqual(program.test, 'tests')
self.assertEqual(Loader.args, [('.', 'test*.py', 'fish')]) self.assertEqual(Loader.args, [('.', 'test*.py', 'fish')])
Loader.args = [] Loader.args = []
program = object.__new__(unittest.TestProgram) program = TestableTestProgram()
program._do_discovery(['-p', 'fish'], Loader=Loader) program._do_discovery(['-p', 'fish'], Loader=Loader)
self.assertEqual(program.test, 'tests') self.assertEqual(program.test, 'tests')
self.assertEqual(Loader.args, [('.', 'fish', None)]) self.assertEqual(Loader.args, [('.', 'fish', None)])
@ -287,7 +312,7 @@ class TestDiscovery(unittest.TestCase):
self.assertFalse(program.catchbreak) self.assertFalse(program.catchbreak)
Loader.args = [] Loader.args = []
program = object.__new__(unittest.TestProgram) program = TestableTestProgram()
program._do_discovery(['-p', 'eggs', '-s', 'fish', '-v', '-f', '-c'], program._do_discovery(['-p', 'eggs', '-s', 'fish', '-v', '-f', '-c'],
Loader=Loader) Loader=Loader)
self.assertEqual(program.test, 'tests') self.assertEqual(program.test, 'tests')

View File

@ -695,6 +695,7 @@ Anthony Roach
Mark Roberts Mark Roberts
Jim Robinson Jim Robinson
Andy Robinson Andy Robinson
Mark Roddy
Kevin Rodgers Kevin Rodgers
Giampaolo Rodola Giampaolo Rodola
Mike Romberg Mike Romberg

View File

@ -32,6 +32,9 @@ Core and Builtins
Library Library
------- -------
- Issue #10470: 'python -m unittest' will now run test discovery by default,
when no extra arguments have been provided.
- Issue #3709: BaseHTTPRequestHandler will buffer the headers and write to - Issue #3709: BaseHTTPRequestHandler will buffer the headers and write to
output stream only when end_headers is invoked. This is a speedup and an output stream only when end_headers is invoked. This is a speedup and an
internal optimization. Patch by endian. internal optimization. Patch by endian.