summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/unittest/main.py74
-rw-r--r--Lib/unittest/test/test_program.py17
2 files changed, 32 insertions, 59 deletions
diff --git a/Lib/unittest/main.py b/Lib/unittest/main.py
index c50066f..dc64575 100644
--- a/Lib/unittest/main.py
+++ b/Lib/unittest/main.py
@@ -1,6 +1,7 @@
"""Unittest main program"""
import sys
+import optparse
import os
from . import loader, runner
@@ -76,6 +77,7 @@ def _convert_name(name):
def _convert_names(names):
return [_convert_name(name) for name in names]
+
class TestProgram(object):
"""A command-line program that runs a set of tests; this is primarily
for making test modules conveniently executable.
@@ -142,33 +144,9 @@ class TestProgram(object):
self._do_discovery(argv[2:])
return
- import getopt
- long_opts = ['help', 'verbose', 'quiet', 'failfast', 'catch', 'buffer']
- try:
- options, args = getopt.getopt(argv[1:], 'hHvqfcb', long_opts)
- except getopt.error as msg:
- self.usageExit(msg)
- return
-
- for opt, value in options:
- if opt in ('-h','-H','--help'):
- self.usageExit()
- if opt in ('-q','--quiet'):
- self.verbosity = 0
- if opt in ('-v','--verbose'):
- self.verbosity = 2
- if opt in ('-f','--failfast'):
- if self.failfast is None:
- self.failfast = True
- # Should this raise an exception if -f is not valid?
- if opt in ('-c','--catch'):
- if self.catchbreak is None:
- self.catchbreak = True
- # Should this raise an exception if -c is not valid?
- if opt in ('-b','--buffer'):
- if self.buffer is None:
- self.buffer = True
- # Should this raise an exception if -b is not valid?
+ parser = self._getOptParser()
+ options, args = parser.parse_args(argv[1:])
+ self._setAttributesFromOptions(options)
if len(args) == 0 and self.module is None:
# this allows "python -m unittest -v" to still work for
@@ -196,14 +174,14 @@ class TestProgram(object):
self.test = self.testLoader.loadTestsFromNames(self.testNames,
self.module)
- def _do_discovery(self, argv, Loader=loader.TestLoader):
- # handle command line args for test discovery
- self.progName = '%s discover' % self.progName
- import optparse
+ def _getOptParser(self):
parser = optparse.OptionParser()
parser.prog = self.progName
parser.add_option('-v', '--verbose', dest='verbose', default=False,
help='Verbose output', action='store_true')
+ parser.add_option('-q', '--quiet', dest='quiet', default=False,
+ help='Quiet output', action='store_true')
+
if self.failfast != False:
parser.add_option('-f', '--failfast', dest='failfast', default=False,
help='Stop on first fail or error',
@@ -216,6 +194,28 @@ class TestProgram(object):
parser.add_option('-b', '--buffer', dest='buffer', default=False,
help='Buffer stdout and stderr during tests',
action='store_true')
+ return parser
+
+ def _setAttributesFromOptions(self, options):
+ # only set options from the parsing here
+ # if they weren't set explicitly in the constructor
+ if self.failfast is None:
+ self.failfast = options.failfast
+ if self.catchbreak is None:
+ self.catchbreak = options.catchbreak
+ if self.buffer is None:
+ self.buffer = options.buffer
+
+ if options.verbose:
+ self.verbosity = 2
+ elif options.quiet:
+ self.verbosity = 0
+
+
+ def _do_discovery(self, argv, Loader=loader.TestLoader):
+ # handle command line args for test discovery
+ self.progName = '%s discover' % self.progName
+ parser = self._getOptParser()
parser.add_option('-s', '--start-directory', dest='start', default='.',
help="Directory to start discovery ('.' default)")
parser.add_option('-p', '--pattern', dest='pattern', default='test*.py',
@@ -230,17 +230,7 @@ class TestProgram(object):
for name, value in zip(('start', 'pattern', 'top'), args):
setattr(options, name, value)
- # only set options from the parsing here
- # if they weren't set explicitly in the constructor
- if self.failfast is None:
- self.failfast = options.failfast
- if self.catchbreak is None:
- self.catchbreak = options.catchbreak
- if self.buffer is None:
- self.buffer = options.buffer
-
- if options.verbose:
- self.verbosity = 2
+ self._setAttributesFromOptions(options)
start_dir = options.start
pattern = options.pattern
diff --git a/Lib/unittest/test/test_program.py b/Lib/unittest/test/test_program.py
index d5d0f5a..9794868 100644
--- a/Lib/unittest/test/test_program.py
+++ b/Lib/unittest/test/test_program.py
@@ -131,23 +131,6 @@ class TestCommandLineArgs(unittest.TestCase):
FakeRunner.test = None
FakeRunner.raiseError = False
- def testHelpAndUnknown(self):
- program = self.program
- def usageExit(msg=None):
- program.msg = msg
- program.exit = True
- program.usageExit = usageExit
-
- for opt in '-h', '-H', '--help':
- program.exit = False
- program.parseArgs([None, opt])
- self.assertTrue(program.exit)
- self.assertIsNone(program.msg)
-
- program.parseArgs([None, '-$'])
- self.assertTrue(program.exit)
- self.assertIsNotNone(program.msg)
-
def testVerbosity(self):
program = self.program