mirror of
https://github.com/python/cpython.git
synced 2025-08-22 01:35:16 +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"""
|
||||
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue