diff options
author | Benjamin Peterson <benjamin@python.org> | 2010-03-22 01:13:48 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2010-03-22 01:13:48 (GMT) |
commit | 8769fd8a17dad1b1861c04ea75ce46478d21a34d (patch) | |
tree | d560c681b9e306646f9f54eb3e7c4bba29b0b7ef /Lib/unittest | |
parent | dccc1fcfafd37c6fd94ca766516cb4903b29668e (diff) | |
download | cpython-8769fd8a17dad1b1861c04ea75ce46478d21a34d.zip cpython-8769fd8a17dad1b1861c04ea75ce46478d21a34d.tar.gz cpython-8769fd8a17dad1b1861c04ea75ce46478d21a34d.tar.bz2 |
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.
........
Diffstat (limited to 'Lib/unittest')
-rw-r--r-- | Lib/unittest/main.py | 21 | ||||
-rw-r--r-- | Lib/unittest/result.py | 13 | ||||
-rw-r--r-- | Lib/unittest/runner.py | 4 |
3 files changed, 32 insertions, 6 deletions
diff --git a/Lib/unittest/main.py b/Lib/unittest/main.py index e04ec16..63fbd16 100644 --- a/Lib/unittest/main.py +++ b/Lib/unittest/main.py @@ -16,6 +16,7 @@ Options: -h, --help Show this message -v, --verbose Verbose output -q, --quiet Minimal output + -f, --failfast Stop on first failure Examples: %(progName)s test_module - run tests from test_module @@ -30,6 +31,7 @@ Alternative Usage: %(progName)s discover [options] Options: -v, --verbose Verbose output + -f, --failfast Stop on first failure -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 @@ -46,6 +48,7 @@ Options: -h, --help Show this message -v, --verbose Verbose output -q, --quiet Minimal output + -f, --failfast Stop on first failure Examples: %(progName)s - run default set of tests @@ -69,7 +72,7 @@ class TestProgram(object): def __init__(self, module='__main__', defaultTest=None, argv=None, testRunner=None, testLoader=loader.defaultTestLoader, exit=True, - verbosity=1): + verbosity=1, failfast=False): if isinstance(module, str): self.module = __import__(module) for part in module.split('.')[1:]: @@ -80,6 +83,7 @@ class TestProgram(object): argv = sys.argv self.exit = exit + self.failfast = failfast self.verbosity = verbosity self.defaultTest = defaultTest self.testRunner = testRunner @@ -100,9 +104,9 @@ class TestProgram(object): return import getopt - long_opts = ['help','verbose','quiet'] + long_opts = ['help', 'verbose', 'quiet', 'failfast'] try: - options, args = getopt.getopt(argv[1:], 'hHvq', long_opts) + options, args = getopt.getopt(argv[1:], 'hHvqf', long_opts) for opt, value in options: if opt in ('-h','-H','--help'): self.usageExit() @@ -110,6 +114,8 @@ class TestProgram(object): self.verbosity = 0 if opt in ('-v','--verbose'): self.verbosity = 2 + if opt in ('-f','--failfast'): + self.failfast = True if len(args) == 0 and self.defaultTest is None: # createTests will load tests from self.module self.testNames = None @@ -137,6 +143,8 @@ class TestProgram(object): parser = optparse.OptionParser() parser.add_option('-v', '--verbose', dest='verbose', default=False, 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='.', help="Directory to start discovery ('.' default)") parser.add_option('-p', '--pattern', dest='pattern', default='test*.py', @@ -153,6 +161,8 @@ class TestProgram(object): if options.verbose: self.verbosity = 2 + if options.failfast: + self.failfast = True start_dir = options.start pattern = options.pattern @@ -166,9 +176,10 @@ class TestProgram(object): self.testRunner = runner.TextTestRunner if isinstance(self.testRunner, type): try: - testRunner = self.testRunner(verbosity=self.verbosity) + testRunner = self.testRunner(verbosity=self.verbosity, + failfast=self.failfast) except TypeError: - # didn't accept the verbosity argument + # didn't accept the verbosity or failfast argument testRunner = self.testRunner() else: # it is assumed to be a TestRunner instance diff --git a/Lib/unittest/result.py b/Lib/unittest/result.py index 91cf218..96d56a1 100644 --- a/Lib/unittest/result.py +++ b/Lib/unittest/result.py @@ -3,9 +3,17 @@ import traceback from . import util +from functools import wraps __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): """Holder for test result information. @@ -21,6 +29,7 @@ class TestResult(object): _previousTestClass = None _moduleSetUpFailed = False def __init__(self, stream=None, descriptions=None, verbosity=None): + self.failfast = False self.failures = [] self.errors = [] self.testsRun = 0 @@ -51,12 +60,14 @@ class TestResult(object): See stopTest for a method called after each test. """ + @failfast def addError(self, test, err): """Called when an error has occurred. 'err' is a tuple of values as returned by sys.exc_info(). """ self.errors.append((test, self._exc_info_to_string(err, test))) + @failfast def addFailure(self, test, err): """Called when an error has occurred. 'err' is a tuple of values as returned by sys.exc_info().""" @@ -70,11 +81,13 @@ class TestResult(object): """Called when a test is skipped.""" self.skipped.append((test, reason)) + @failfast def addExpectedFailure(self, test, err): """Called when an expected failure/error occured.""" self.expectedFailures.append( (test, self._exc_info_to_string(err, test))) + @failfast def addUnexpectedSuccess(self, test): """Called when a test was expected to fail, but succeed.""" self.unexpectedSuccesses.append(test) diff --git a/Lib/unittest/runner.py b/Lib/unittest/runner.py index 1729f1d..5fe7d67 100644 --- a/Lib/unittest/runner.py +++ b/Lib/unittest/runner.py @@ -124,10 +124,11 @@ class TextTestRunner(object): resultclass = TextTestResult def __init__(self, stream=sys.stderr, descriptions=True, verbosity=1, - resultclass=None): + failfast=False, resultclass=None): self.stream = _WritelnDecorator(stream) self.descriptions = descriptions self.verbosity = verbosity + self.failfast = failfast if resultclass is not None: self.resultclass = resultclass @@ -137,6 +138,7 @@ class TextTestRunner(object): def run(self, test): "Run the given test case or test suite." result = self._makeResult() + result.failfast = self.failfast startTime = time.time() startTestRun = getattr(result, 'startTestRun', None) if startTestRun is not None: |