]> granicus.if.org Git - python/commitdiff
Merged revisions 79265-79266 via svnmerge from
authorBenjamin Peterson <benjamin@python.org>
Mon, 22 Mar 2010 01:13:48 +0000 (01:13 +0000)
committerBenjamin Peterson <benjamin@python.org>
Mon, 22 Mar 2010 01:13:48 +0000 (01:13 +0000)
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.
........

Lib/test/test_unittest.py
Lib/unittest/main.py
Lib/unittest/result.py
Lib/unittest/runner.py

index 954b40a38baaaab5e394a05cdf8efef008e8eeb7..c8fc43965a390a4a25b36d63e83e36b834c6a1f2 100644 (file)
@@ -2106,6 +2106,38 @@ class Test_TestResult(TestCase):
         Frame.tb_frame.f_globals['__unittest'] = True
         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__)
 for m in ('addSkip', 'addExpectedFailure', 'addUnexpectedSuccess',
            '__init__'):
@@ -2181,21 +2213,23 @@ class Foo(unittest.TestCase):
 class Bar(Foo):
     def test2(self): pass
 
-class LoggingTestCase(unittest.TestCase):
-    """A test case which logs its calls."""
+def getLoggingTestCase():
+    class LoggingTestCase(unittest.TestCase):
+        """A test case which logs its calls."""
 
-    def __init__(self, events):
-        super(LoggingTestCase, self).__init__('test')
-        self.events = events
+        def __init__(self, events):
+            super(LoggingTestCase, self).__init__('test')
+            self.events = events
 
-    def setUp(self):
-        self.events.append('setUp')
+        def setUp(self):
+            self.events.append('setUp')
 
-    def test(self):
-        self.events.append('test')
+        def test(self):
+            self.events.append('test')
 
-    def tearDown(self):
-        self.events.append('tearDown')
+        def tearDown(self):
+            self.events.append('tearDown')
+    return LoggingTestCase
 
 class ResultWithNoStartTestRunStopTestRun(object):
     """An object honouring TestResult before startTestRun/stopTestRun."""
@@ -2322,7 +2356,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
         events = []
         result = LoggingResult(events)
 
-        class Foo(LoggingTestCase):
+        class Foo(getLoggingTestCase()):
             def setUp(self):
                 super(Foo, self).setUp()
                 raise RuntimeError('raised by Foo.setUp')
@@ -2335,7 +2369,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
     def test_run_call_order__error_in_setUp_default_result(self):
         events = []
 
-        class Foo(LoggingTestCase):
+        class Foo(getLoggingTestCase()):
             def defaultTestResult(self):
                 return LoggingResult(self.events)
 
@@ -2359,7 +2393,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
         events = []
         result = LoggingResult(events)
 
-        class Foo(LoggingTestCase):
+        class Foo(getLoggingTestCase()):
             def test(self):
                 super(Foo, self).test()
                 raise RuntimeError('raised by Foo.test')
@@ -2374,7 +2408,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
     def test_run_call_order__error_in_test_default_result(self):
         events = []
 
-        class Foo(LoggingTestCase):
+        class Foo(getLoggingTestCase()):
             def defaultTestResult(self):
                 return LoggingResult(self.events)
 
@@ -2398,7 +2432,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
         events = []
         result = LoggingResult(events)
 
-        class Foo(LoggingTestCase):
+        class Foo(getLoggingTestCase()):
             def test(self):
                 super(Foo, self).test()
                 self.fail('raised by Foo.test')
@@ -2411,7 +2445,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
     # "When a test fails with a default result stopTestRun is still called."
     def test_run_call_order__failure_in_test_default_result(self):
 
-        class Foo(LoggingTestCase):
+        class Foo(getLoggingTestCase()):
             def defaultTestResult(self):
                 return LoggingResult(self.events)
             def test(self):
@@ -2435,7 +2469,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
         events = []
         result = LoggingResult(events)
 
-        class Foo(LoggingTestCase):
+        class Foo(getLoggingTestCase()):
             def tearDown(self):
                 super(Foo, self).tearDown()
                 raise RuntimeError('raised by Foo.tearDown')
@@ -2448,7 +2482,7 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
     # "When tearDown errors with a default result stopTestRun is still called."
     def test_run_call_order__error_in_tearDown_default_result(self):
 
-        class Foo(LoggingTestCase):
+        class Foo(getLoggingTestCase()):
             def defaultTestResult(self):
                 return LoggingResult(self.events)
             def tearDown(self):
index e04ec16ffbec1df9acabc10ad49db72fd8edf950..63fbd160812696359e54502ff5a14fc59d203c0b 100644 (file)
@@ -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
index 91cf2183436936bb9153efa5efa0f6d0b5d665a1..96d56a114e966c60890525f5921afefa4cbad5ae 100644 (file)
@@ -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)
index 1729f1d098b1c38557858e820c6299805bc6b00c..5fe7d677da4f9048f0a4b66a169b9ccfef577e59 100644 (file)
@@ -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: