diff options
Diffstat (limited to 'Lib/unittest/main.py')
-rw-r--r-- | Lib/unittest/main.py | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/Lib/unittest/main.py b/Lib/unittest/main.py index 3e659ab..2f488e1 100644 --- a/Lib/unittest/main.py +++ b/Lib/unittest/main.py @@ -9,9 +9,9 @@ from .signals import installHandler __unittest = True - -FAILFAST = " -f, --failfast Stop on first failure\n" -CATCHBREAK = " -c, --catch Catch control-C and display results\n" +FAILFAST = " -f, --failfast Stop on first failure\n" +CATCHBREAK = " -c, --catch Catch control-C and display results\n" +BUFFEROUTPUT = " -b, --buffer Buffer stdout and stderr during test runs\n" USAGE_AS_MAIN = """\ Usage: %(progName)s [options] [tests] @@ -20,7 +20,7 @@ Options: -h, --help Show this message -v, --verbose Verbose output -q, --quiet Minimal output -%(failfast)s%(catchbreak)s +%(failfast)s%(catchbreak)s%(buffer)s Examples: %(progName)s test_module - run tests from test_module %(progName)s test_module.TestClass - run tests from @@ -34,7 +34,7 @@ Alternative Usage: %(progName)s discover [options] Options: -v, --verbose Verbose output -%(failfast)s%(catchbreak)s -s directory Directory to start discovery ('.' default) +%(failfast)s%(catchbreak)s%(buffer)s -s directory Directory to start discovery ('.' default) -p pattern Pattern to match test files ('test*.py' default) -t directory Top level directory of project (default to start directory) @@ -50,7 +50,7 @@ Options: -h, --help Show this message -v, --verbose Verbose output -q, --quiet Minimal output -%(failfast)s%(catchbreak)s +%(failfast)s%(catchbreak)s%(buffer)s Examples: %(progName)s - run default set of tests %(progName)s MyTestSuite - run suite 'MyTestSuite' @@ -68,12 +68,12 @@ class TestProgram(object): USAGE = USAGE_FROM_MODULE # defaults for testing - failfast = catchbreak = None + failfast = catchbreak = buffer = None def __init__(self, module='__main__', defaultTest=None, argv=None, testRunner=None, testLoader=loader.defaultTestLoader, exit=True, - verbosity=1, failfast=None, catchbreak=None): + verbosity=1, failfast=None, catchbreak=None, buffer=None): if isinstance(module, str): self.module = __import__(module) for part in module.split('.')[1:]: @@ -87,6 +87,7 @@ class TestProgram(object): self.failfast = failfast self.catchbreak = catchbreak self.verbosity = verbosity + self.buffer = buffer self.defaultTest = defaultTest self.testRunner = testRunner self.testLoader = testLoader @@ -97,11 +98,14 @@ class TestProgram(object): def usageExit(self, msg=None): if msg: print(msg) - usage = {'progName': self.progName, 'catchbreak': '', 'failfast': ''} + usage = {'progName': self.progName, 'catchbreak': '', 'failfast': '', + 'buffer': ''} if self.failfast != False: usage['failfast'] = FAILFAST if self.catchbreak != False: usage['catchbreak'] = CATCHBREAK + if self.buffer != False: + usage['buffer'] = BUFFEROUTPUT print(self.USAGE % usage) sys.exit(2) @@ -111,9 +115,9 @@ class TestProgram(object): return import getopt - long_opts = ['help', 'verbose', 'quiet', 'failfast', 'catch'] + long_opts = ['help', 'verbose', 'quiet', 'failfast', 'catch', 'buffer'] try: - options, args = getopt.getopt(argv[1:], 'hHvqfc', long_opts) + options, args = getopt.getopt(argv[1:], 'hHvqfcb', long_opts) for opt, value in options: if opt in ('-h','-H','--help'): self.usageExit() @@ -129,6 +133,10 @@ class TestProgram(object): 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.defaultTest is None: # createTests will load tests from self.module self.testNames = None @@ -164,6 +172,10 @@ class TestProgram(object): parser.add_option('-c', '--catch', dest='catchbreak', default=False, help='Catch ctrl-C and display results so far', action='store_true') + if self.buffer != False: + parser.add_option('-b', '--buffer', dest='buffer', default=False, + help='Buffer stdout and stderr during tests', + action='store_true') 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', @@ -184,6 +196,8 @@ class TestProgram(object): 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 @@ -203,9 +217,10 @@ class TestProgram(object): if isinstance(self.testRunner, type): try: testRunner = self.testRunner(verbosity=self.verbosity, - failfast=self.failfast) + failfast=self.failfast, + buffer=self.buffer) except TypeError: - # didn't accept the verbosity or failfast arguments + # didn't accept the verbosity, buffer or failfast arguments testRunner = self.testRunner() else: # it is assumed to be a TestRunner instance |