Kaydet (Commit) 8769fd8a authored tarafından Benjamin Peterson's avatar Benjamin Peterson

Merged revisions 79265-79266 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r79265 | michael.foord | 2010-03-21 20:01:34 -0500 (Sun, 21 Mar 2010) | 1 line

  -f/--failfast command line option for unittest. Issue 8074. Documentation still needed. Plus minor change to test_unittest to allow it to be run with python -m test.unittest
........
  r79266 | michael.foord | 2010-03-21 20:02:23 -0500 (Sun, 21 Mar 2010) | 1 line

  Fix failing test committed by accident.
........
üst dccc1fcf
...@@ -2106,6 +2106,38 @@ class Test_TestResult(TestCase): ...@@ -2106,6 +2106,38 @@ class Test_TestResult(TestCase):
Frame.tb_frame.f_globals['__unittest'] = True Frame.tb_frame.f_globals['__unittest'] = True
self.assertTrue(result._is_relevant_tb_level(Frame)) self.assertTrue(result._is_relevant_tb_level(Frame))
def testFailFast(self):
result = unittest.TestResult()
result._exc_info_to_string = lambda *_: ''
result.failfast = True
result.addError(None, None)
self.assertTrue(result.shouldStop)
result = unittest.TestResult()
result._exc_info_to_string = lambda *_: ''
result.failfast = True
result.addFailure(None, None)
self.assertTrue(result.shouldStop)
result = unittest.TestResult()
result._exc_info_to_string = lambda *_: ''
result.failfast = True
result.addUnexpectedSuccess(None)
self.assertTrue(result.shouldStop)
result = unittest.TestResult()
result._exc_info_to_string = lambda *_: ''
result.failfast = True
result.addExpectedFailure(None, None)
self.assertTrue(result.shouldStop)
def testFailFastSetByRunner(self):
runner = unittest.TextTestRunner(stream=io.StringIO(), failfast=True)
def test(result):
self.assertTrue(result.failfast)
result = runner.run(test)
classDict = dict(unittest.TestResult.__dict__) classDict = dict(unittest.TestResult.__dict__)
for m in ('addSkip', 'addExpectedFailure', 'addUnexpectedSuccess', for m in ('addSkip', 'addExpectedFailure', 'addUnexpectedSuccess',
'__init__'): '__init__'):
...@@ -2181,21 +2213,23 @@ class Foo(unittest.TestCase): ...@@ -2181,21 +2213,23 @@ class Foo(unittest.TestCase):
class Bar(Foo): class Bar(Foo):
def test2(self): pass def test2(self): pass
class LoggingTestCase(unittest.TestCase): def getLoggingTestCase():
"""A test case which logs its calls.""" class LoggingTestCase(unittest.TestCase):
"""A test case which logs its calls."""
def __init__(self, events): def __init__(self, events):
super(LoggingTestCase, self).__init__('test') super(LoggingTestCase, self).__init__('test')
self.events = events self.events = events
def setUp(self): def setUp(self):
self.events.append('setUp') self.events.append('setUp')
def test(self): def test(self):
self.events.append('test') self.events.append('test')
def tearDown(self): def tearDown(self):
self.events.append('tearDown') self.events.append('tearDown')
return LoggingTestCase
class ResultWithNoStartTestRunStopTestRun(object): class ResultWithNoStartTestRunStopTestRun(object):
"""An object honouring TestResult before startTestRun/stopTestRun.""" """An object honouring TestResult before startTestRun/stopTestRun."""
...@@ -2322,7 +2356,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing): ...@@ -2322,7 +2356,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
events = [] events = []
result = LoggingResult(events) result = LoggingResult(events)
class Foo(LoggingTestCase): class Foo(getLoggingTestCase()):
def setUp(self): def setUp(self):
super(Foo, self).setUp() super(Foo, self).setUp()
raise RuntimeError('raised by Foo.setUp') raise RuntimeError('raised by Foo.setUp')
...@@ -2335,7 +2369,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing): ...@@ -2335,7 +2369,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
def test_run_call_order__error_in_setUp_default_result(self): def test_run_call_order__error_in_setUp_default_result(self):
events = [] events = []
class Foo(LoggingTestCase): class Foo(getLoggingTestCase()):
def defaultTestResult(self): def defaultTestResult(self):
return LoggingResult(self.events) return LoggingResult(self.events)
...@@ -2359,7 +2393,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing): ...@@ -2359,7 +2393,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
events = [] events = []
result = LoggingResult(events) result = LoggingResult(events)
class Foo(LoggingTestCase): class Foo(getLoggingTestCase()):
def test(self): def test(self):
super(Foo, self).test() super(Foo, self).test()
raise RuntimeError('raised by Foo.test') raise RuntimeError('raised by Foo.test')
...@@ -2374,7 +2408,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing): ...@@ -2374,7 +2408,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
def test_run_call_order__error_in_test_default_result(self): def test_run_call_order__error_in_test_default_result(self):
events = [] events = []
class Foo(LoggingTestCase): class Foo(getLoggingTestCase()):
def defaultTestResult(self): def defaultTestResult(self):
return LoggingResult(self.events) return LoggingResult(self.events)
...@@ -2398,7 +2432,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing): ...@@ -2398,7 +2432,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
events = [] events = []
result = LoggingResult(events) result = LoggingResult(events)
class Foo(LoggingTestCase): class Foo(getLoggingTestCase()):
def test(self): def test(self):
super(Foo, self).test() super(Foo, self).test()
self.fail('raised by Foo.test') self.fail('raised by Foo.test')
...@@ -2411,7 +2445,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing): ...@@ -2411,7 +2445,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
# "When a test fails with a default result stopTestRun is still called." # "When a test fails with a default result stopTestRun is still called."
def test_run_call_order__failure_in_test_default_result(self): def test_run_call_order__failure_in_test_default_result(self):
class Foo(LoggingTestCase): class Foo(getLoggingTestCase()):
def defaultTestResult(self): def defaultTestResult(self):
return LoggingResult(self.events) return LoggingResult(self.events)
def test(self): def test(self):
...@@ -2435,7 +2469,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing): ...@@ -2435,7 +2469,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
events = [] events = []
result = LoggingResult(events) result = LoggingResult(events)
class Foo(LoggingTestCase): class Foo(getLoggingTestCase()):
def tearDown(self): def tearDown(self):
super(Foo, self).tearDown() super(Foo, self).tearDown()
raise RuntimeError('raised by Foo.tearDown') raise RuntimeError('raised by Foo.tearDown')
...@@ -2448,7 +2482,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing): ...@@ -2448,7 +2482,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
# "When tearDown errors with a default result stopTestRun is still called." # "When tearDown errors with a default result stopTestRun is still called."
def test_run_call_order__error_in_tearDown_default_result(self): def test_run_call_order__error_in_tearDown_default_result(self):
class Foo(LoggingTestCase): class Foo(getLoggingTestCase()):
def defaultTestResult(self): def defaultTestResult(self):
return LoggingResult(self.events) return LoggingResult(self.events)
def tearDown(self): def tearDown(self):
......
...@@ -16,6 +16,7 @@ Options: ...@@ -16,6 +16,7 @@ Options:
-h, --help Show this message -h, --help Show this message
-v, --verbose Verbose output -v, --verbose Verbose output
-q, --quiet Minimal output -q, --quiet Minimal output
-f, --failfast Stop on first failure
Examples: Examples:
%(progName)s test_module - run tests from test_module %(progName)s test_module - run tests from test_module
...@@ -30,6 +31,7 @@ Alternative Usage: %(progName)s discover [options] ...@@ -30,6 +31,7 @@ Alternative Usage: %(progName)s discover [options]
Options: Options:
-v, --verbose Verbose output -v, --verbose Verbose output
-f, --failfast Stop on first failure
-s directory Directory to start discovery ('.' default) -s directory Directory to start discovery ('.' default)
-p pattern Pattern to match test files ('test*.py' default) -p pattern Pattern to match test files ('test*.py' default)
-t directory Top level directory of project (default to -t directory Top level directory of project (default to
...@@ -46,6 +48,7 @@ Options: ...@@ -46,6 +48,7 @@ Options:
-h, --help Show this message -h, --help Show this message
-v, --verbose Verbose output -v, --verbose Verbose output
-q, --quiet Minimal output -q, --quiet Minimal output
-f, --failfast Stop on first failure
Examples: Examples:
%(progName)s - run default set of tests %(progName)s - run default set of tests
...@@ -69,7 +72,7 @@ class TestProgram(object): ...@@ -69,7 +72,7 @@ class TestProgram(object):
def __init__(self, module='__main__', defaultTest=None, def __init__(self, module='__main__', defaultTest=None,
argv=None, testRunner=None, argv=None, testRunner=None,
testLoader=loader.defaultTestLoader, exit=True, testLoader=loader.defaultTestLoader, exit=True,
verbosity=1): verbosity=1, failfast=False):
if isinstance(module, str): if isinstance(module, str):
self.module = __import__(module) self.module = __import__(module)
for part in module.split('.')[1:]: for part in module.split('.')[1:]:
...@@ -80,6 +83,7 @@ class TestProgram(object): ...@@ -80,6 +83,7 @@ class TestProgram(object):
argv = sys.argv argv = sys.argv
self.exit = exit self.exit = exit
self.failfast = failfast
self.verbosity = verbosity self.verbosity = verbosity
self.defaultTest = defaultTest self.defaultTest = defaultTest
self.testRunner = testRunner self.testRunner = testRunner
...@@ -100,9 +104,9 @@ class TestProgram(object): ...@@ -100,9 +104,9 @@ class TestProgram(object):
return return
import getopt import getopt
long_opts = ['help','verbose','quiet'] long_opts = ['help', 'verbose', 'quiet', 'failfast']
try: try:
options, args = getopt.getopt(argv[1:], 'hHvq', long_opts) options, args = getopt.getopt(argv[1:], 'hHvqf', long_opts)
for opt, value in options: for opt, value in options:
if opt in ('-h','-H','--help'): if opt in ('-h','-H','--help'):
self.usageExit() self.usageExit()
...@@ -110,6 +114,8 @@ class TestProgram(object): ...@@ -110,6 +114,8 @@ class TestProgram(object):
self.verbosity = 0 self.verbosity = 0
if opt in ('-v','--verbose'): if opt in ('-v','--verbose'):
self.verbosity = 2 self.verbosity = 2
if opt in ('-f','--failfast'):
self.failfast = True
if len(args) == 0 and self.defaultTest is None: if len(args) == 0 and self.defaultTest is None:
# createTests will load tests from self.module # createTests will load tests from self.module
self.testNames = None self.testNames = None
...@@ -137,6 +143,8 @@ class TestProgram(object): ...@@ -137,6 +143,8 @@ class TestProgram(object):
parser = optparse.OptionParser() parser = optparse.OptionParser()
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('-f', '--failfast', dest='failfast', default=False,
help='Stop on first fail or error', action='store_true')
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',
...@@ -153,6 +161,8 @@ class TestProgram(object): ...@@ -153,6 +161,8 @@ class TestProgram(object):
if options.verbose: if options.verbose:
self.verbosity = 2 self.verbosity = 2
if options.failfast:
self.failfast = True
start_dir = options.start start_dir = options.start
pattern = options.pattern pattern = options.pattern
...@@ -166,9 +176,10 @@ class TestProgram(object): ...@@ -166,9 +176,10 @@ class TestProgram(object):
self.testRunner = runner.TextTestRunner self.testRunner = runner.TextTestRunner
if isinstance(self.testRunner, type): if isinstance(self.testRunner, type):
try: try:
testRunner = self.testRunner(verbosity=self.verbosity) testRunner = self.testRunner(verbosity=self.verbosity,
failfast=self.failfast)
except TypeError: except TypeError:
# didn't accept the verbosity argument # didn't accept the verbosity or failfast argument
testRunner = self.testRunner() testRunner = self.testRunner()
else: else:
# it is assumed to be a TestRunner instance # it is assumed to be a TestRunner instance
......
...@@ -3,9 +3,17 @@ ...@@ -3,9 +3,17 @@
import traceback import traceback
from . import util from . import util
from functools import wraps
__unittest = True __unittest = True
def failfast(method):
@wraps(method)
def inner(self, *args, **kw):
if getattr(self, 'failfast', False):
self.stop()
return method(self, *args, **kw)
return inner
class TestResult(object): class TestResult(object):
"""Holder for test result information. """Holder for test result information.
...@@ -21,6 +29,7 @@ class TestResult(object): ...@@ -21,6 +29,7 @@ class TestResult(object):
_previousTestClass = None _previousTestClass = None
_moduleSetUpFailed = False _moduleSetUpFailed = False
def __init__(self, stream=None, descriptions=None, verbosity=None): def __init__(self, stream=None, descriptions=None, verbosity=None):
self.failfast = False
self.failures = [] self.failures = []
self.errors = [] self.errors = []
self.testsRun = 0 self.testsRun = 0
...@@ -51,12 +60,14 @@ class TestResult(object): ...@@ -51,12 +60,14 @@ class TestResult(object):
See stopTest for a method called after each test. See stopTest for a method called after each test.
""" """
@failfast
def addError(self, test, err): def addError(self, test, err):
"""Called when an error has occurred. 'err' is a tuple of values as """Called when an error has occurred. 'err' is a tuple of values as
returned by sys.exc_info(). returned by sys.exc_info().
""" """
self.errors.append((test, self._exc_info_to_string(err, test))) self.errors.append((test, self._exc_info_to_string(err, test)))
@failfast
def addFailure(self, test, err): def addFailure(self, test, err):
"""Called when an error has occurred. 'err' is a tuple of values as """Called when an error has occurred. 'err' is a tuple of values as
returned by sys.exc_info().""" returned by sys.exc_info()."""
...@@ -70,11 +81,13 @@ class TestResult(object): ...@@ -70,11 +81,13 @@ class TestResult(object):
"""Called when a test is skipped.""" """Called when a test is skipped."""
self.skipped.append((test, reason)) self.skipped.append((test, reason))
@failfast
def addExpectedFailure(self, test, err): def addExpectedFailure(self, test, err):
"""Called when an expected failure/error occured.""" """Called when an expected failure/error occured."""
self.expectedFailures.append( self.expectedFailures.append(
(test, self._exc_info_to_string(err, test))) (test, self._exc_info_to_string(err, test)))
@failfast
def addUnexpectedSuccess(self, test): def addUnexpectedSuccess(self, test):
"""Called when a test was expected to fail, but succeed.""" """Called when a test was expected to fail, but succeed."""
self.unexpectedSuccesses.append(test) self.unexpectedSuccesses.append(test)
......
...@@ -124,10 +124,11 @@ class TextTestRunner(object): ...@@ -124,10 +124,11 @@ class TextTestRunner(object):
resultclass = TextTestResult resultclass = TextTestResult
def __init__(self, stream=sys.stderr, descriptions=True, verbosity=1, def __init__(self, stream=sys.stderr, descriptions=True, verbosity=1,
resultclass=None): failfast=False, resultclass=None):
self.stream = _WritelnDecorator(stream) self.stream = _WritelnDecorator(stream)
self.descriptions = descriptions self.descriptions = descriptions
self.verbosity = verbosity self.verbosity = verbosity
self.failfast = failfast
if resultclass is not None: if resultclass is not None:
self.resultclass = resultclass self.resultclass = resultclass
...@@ -137,6 +138,7 @@ class TextTestRunner(object): ...@@ -137,6 +138,7 @@ class TextTestRunner(object):
def run(self, test): def run(self, test):
"Run the given test case or test suite." "Run the given test case or test suite."
result = self._makeResult() result = self._makeResult()
result.failfast = self.failfast
startTime = time.time() startTime = time.time()
startTestRun = getattr(result, 'startTestRun', None) startTestRun = getattr(result, 'startTestRun', None)
if startTestRun is not None: if startTestRun is not None:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment