summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMichael Foord <fuzzyman@voidspace.org.uk>2010-11-21 21:28:01 (GMT)
committerMichael Foord <fuzzyman@voidspace.org.uk>2010-11-21 21:28:01 (GMT)
commit086f30815c0f674326fe78ba83689d0a2a96b8d7 (patch)
tree3c9ae9c0afa50d255f093cf730cafefbd5751efd /Lib
parent47c74ac97798950d3fbe973d0b9b9f292ae76eab (diff)
downloadcpython-086f30815c0f674326fe78ba83689d0a2a96b8d7.zip
cpython-086f30815c0f674326fe78ba83689d0a2a96b8d7.tar.gz
cpython-086f30815c0f674326fe78ba83689d0a2a96b8d7.tar.bz2
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.)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/unittest/main.py3
-rw-r--r--Lib/unittest/test/test_discovery.py51
2 files changed, 40 insertions, 14 deletions
diff --git a/Lib/unittest/main.py b/Lib/unittest/main.py
index 6acff8f..8473a33 100644
--- a/Lib/unittest/main.py
+++ b/Lib/unittest/main.py
@@ -109,7 +109,8 @@ class TestProgram(object):
sys.exit(2)
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:])
return
diff --git a/Lib/unittest/test/test_discovery.py b/Lib/unittest/test/test_discovery.py
index aa16b17..7c24bf8 100644
--- a/Lib/unittest/test/test_discovery.py
+++ b/Lib/unittest/test/test_discovery.py
@@ -5,6 +5,18 @@ import sys
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):
# Heavily mocked tests so I can avoid hitting the filesystem
@@ -195,8 +207,7 @@ class TestDiscovery(unittest.TestCase):
test.test_this_does_not_exist()
def test_command_line_handling_parseArgs(self):
- # Haha - take that uninstantiable class
- program = object.__new__(unittest.TestProgram)
+ program = TestableTestProgram()
args = []
def do_discovery(argv):
@@ -208,13 +219,27 @@ class TestDiscovery(unittest.TestCase):
program.parseArgs(['something', 'discover', '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):
class Stop(Exception):
pass
def usageExit():
raise Stop
- program = object.__new__(unittest.TestProgram)
+ program = TestableTestProgram()
program.usageExit = usageExit
with self.assertRaises(Stop):
@@ -223,7 +248,7 @@ class TestDiscovery(unittest.TestCase):
def test_command_line_handling_do_discovery_calls_loader(self):
- program = object.__new__(unittest.TestProgram)
+ program = TestableTestProgram()
class Loader(object):
args = []
@@ -237,49 +262,49 @@ class TestDiscovery(unittest.TestCase):
self.assertEqual(Loader.args, [('.', 'test*.py', None)])
Loader.args = []
- program = object.__new__(unittest.TestProgram)
+ program = TestableTestProgram()
program._do_discovery(['--verbose'], Loader=Loader)
self.assertEqual(program.test, 'tests')
self.assertEqual(Loader.args, [('.', 'test*.py', None)])
Loader.args = []
- program = object.__new__(unittest.TestProgram)
+ program = TestableTestProgram()
program._do_discovery([], Loader=Loader)
self.assertEqual(program.test, 'tests')
self.assertEqual(Loader.args, [('.', 'test*.py', None)])
Loader.args = []
- program = object.__new__(unittest.TestProgram)
+ program = TestableTestProgram()
program._do_discovery(['fish'], Loader=Loader)
self.assertEqual(program.test, 'tests')
self.assertEqual(Loader.args, [('fish', 'test*.py', None)])
Loader.args = []
- program = object.__new__(unittest.TestProgram)
+ program = TestableTestProgram()
program._do_discovery(['fish', 'eggs'], Loader=Loader)
self.assertEqual(program.test, 'tests')
self.assertEqual(Loader.args, [('fish', 'eggs', None)])
Loader.args = []
- program = object.__new__(unittest.TestProgram)
+ program = TestableTestProgram()
program._do_discovery(['fish', 'eggs', 'ham'], Loader=Loader)
self.assertEqual(program.test, 'tests')
self.assertEqual(Loader.args, [('fish', 'eggs', 'ham')])
Loader.args = []
- program = object.__new__(unittest.TestProgram)
+ program = TestableTestProgram()
program._do_discovery(['-s', 'fish'], Loader=Loader)
self.assertEqual(program.test, 'tests')
self.assertEqual(Loader.args, [('fish', 'test*.py', None)])
Loader.args = []
- program = object.__new__(unittest.TestProgram)
+ program = TestableTestProgram()
program._do_discovery(['-t', 'fish'], Loader=Loader)
self.assertEqual(program.test, 'tests')
self.assertEqual(Loader.args, [('.', 'test*.py', 'fish')])
Loader.args = []
- program = object.__new__(unittest.TestProgram)
+ program = TestableTestProgram()
program._do_discovery(['-p', 'fish'], Loader=Loader)
self.assertEqual(program.test, 'tests')
self.assertEqual(Loader.args, [('.', 'fish', None)])
@@ -287,7 +312,7 @@ class TestDiscovery(unittest.TestCase):
self.assertFalse(program.catchbreak)
Loader.args = []
- program = object.__new__(unittest.TestProgram)
+ program = TestableTestProgram()
program._do_discovery(['-p', 'eggs', '-s', 'fish', '-v', '-f', '-c'],
Loader=Loader)
self.assertEqual(program.test, 'tests')