mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
Refactor unittest command line handling to always use optparse
This commit is contained in:
parent
518cf94ad7
commit
5f99ced092
2 changed files with 32 additions and 59 deletions
|
@ -1,6 +1,7 @@
|
||||||
"""Unittest main program"""
|
"""Unittest main program"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import optparse
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from . import loader, runner
|
from . import loader, runner
|
||||||
|
@ -76,6 +77,7 @@ def _convert_name(name):
|
||||||
def _convert_names(names):
|
def _convert_names(names):
|
||||||
return [_convert_name(name) for name in names]
|
return [_convert_name(name) for name in names]
|
||||||
|
|
||||||
|
|
||||||
class TestProgram(object):
|
class TestProgram(object):
|
||||||
"""A command-line program that runs a set of tests; this is primarily
|
"""A command-line program that runs a set of tests; this is primarily
|
||||||
for making test modules conveniently executable.
|
for making test modules conveniently executable.
|
||||||
|
@ -142,33 +144,9 @@ class TestProgram(object):
|
||||||
self._do_discovery(argv[2:])
|
self._do_discovery(argv[2:])
|
||||||
return
|
return
|
||||||
|
|
||||||
import getopt
|
parser = self._getOptParser()
|
||||||
long_opts = ['help', 'verbose', 'quiet', 'failfast', 'catch', 'buffer']
|
options, args = parser.parse_args(argv[1:])
|
||||||
try:
|
self._setAttributesFromOptions(options)
|
||||||
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?
|
|
||||||
|
|
||||||
if len(args) == 0 and self.module is None:
|
if len(args) == 0 and self.module is None:
|
||||||
# this allows "python -m unittest -v" to still work for
|
# 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.test = self.testLoader.loadTestsFromNames(self.testNames,
|
||||||
self.module)
|
self.module)
|
||||||
|
|
||||||
def _do_discovery(self, argv, Loader=loader.TestLoader):
|
def _getOptParser(self):
|
||||||
# handle command line args for test discovery
|
|
||||||
self.progName = '%s discover' % self.progName
|
|
||||||
import optparse
|
|
||||||
parser = optparse.OptionParser()
|
parser = optparse.OptionParser()
|
||||||
parser.prog = self.progName
|
parser.prog = self.progName
|
||||||
parser.add_option('-v', '--verbose', dest='verbose', default=False,
|
parser.add_option('-v', '--verbose', dest='verbose', default=False,
|
||||||
help='Verbose output', action='store_true')
|
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:
|
if self.failfast != False:
|
||||||
parser.add_option('-f', '--failfast', dest='failfast', default=False,
|
parser.add_option('-f', '--failfast', dest='failfast', default=False,
|
||||||
help='Stop on first fail or error',
|
help='Stop on first fail or error',
|
||||||
|
@ -216,6 +194,28 @@ class TestProgram(object):
|
||||||
parser.add_option('-b', '--buffer', dest='buffer', default=False,
|
parser.add_option('-b', '--buffer', dest='buffer', default=False,
|
||||||
help='Buffer stdout and stderr during tests',
|
help='Buffer stdout and stderr during tests',
|
||||||
action='store_true')
|
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='.',
|
parser.add_option('-s', '--start-directory', dest='start', default='.',
|
||||||
help="Directory to start discovery ('.' default)")
|
help="Directory to start discovery ('.' default)")
|
||||||
parser.add_option('-p', '--pattern', dest='pattern', default='test*.py',
|
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):
|
for name, value in zip(('start', 'pattern', 'top'), args):
|
||||||
setattr(options, name, value)
|
setattr(options, name, value)
|
||||||
|
|
||||||
# only set options from the parsing here
|
self._setAttributesFromOptions(options)
|
||||||
# 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
|
|
||||||
|
|
||||||
start_dir = options.start
|
start_dir = options.start
|
||||||
pattern = options.pattern
|
pattern = options.pattern
|
||||||
|
|
|
@ -131,23 +131,6 @@ class TestCommandLineArgs(unittest.TestCase):
|
||||||
FakeRunner.test = None
|
FakeRunner.test = None
|
||||||
FakeRunner.raiseError = False
|
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):
|
def testVerbosity(self):
|
||||||
program = self.program
|
program = self.program
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue