Rev 4783: TestNotApplicable handling improved for compatibility with stdlib TestResult objects. in http://bazaar.launchpad.net/~lifeless/bzr/subunit

Robert Collins robertc at robertcollins.net
Sun Nov 1 05:01:51 GMT 2009


At http://bazaar.launchpad.net/~lifeless/bzr/subunit

------------------------------------------------------------
revno: 4783
revision-id: robertc at robertcollins.net-20091101050147-sj4ltnewftl0lw98
parent: robertc at robertcollins.net-20091101042753-q7vkznd2x9lbczhd
committer: Robert Collins <robertc at robertcollins.net>
branch nick: subunit
timestamp: Sun 2009-11-01 16:01:47 +1100
message:
  TestNotApplicable handling improved for compatibility with stdlib TestResult objects.
=== modified file 'NEWS'
--- a/NEWS	2009-11-01 04:27:53 +0000
+++ b/NEWS	2009-11-01 05:01:47 +0000
@@ -36,6 +36,11 @@
 Testing
 *******
 
+* TestNotApplicable is now handled within the TestCase.run method rather
+  than being looked for within ``ExtendedTestResult.addError``. This
+  provides better handling with other ``TestResult`` objects, degrading to
+  sucess rather than error. (Robert Collins)
+
 * The private method ``_testConcluded`` on ``ExtendedTestResult`` has been
   removed - it was empty and unused. (Robert Collins)
 

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2009-11-01 04:27:53 +0000
+++ b/bzrlib/tests/__init__.py	2009-11-01 05:01:47 +0000
@@ -298,16 +298,13 @@
         Called from the TestCase run() method when the test
         fails with an unexpected error.
         """
-        if isinstance(err[1], TestNotApplicable):
-            return self._addNotApplicable(test, err)
-        else:
-            self._post_mortem()
-            unittest.TestResult.addError(self, test, err)
-            self.error_count += 1
-            self.report_error(test, err)
-            if self.stop_early:
-                self.stop()
-            self._cleanupLogFile(test)
+        self._post_mortem()
+        unittest.TestResult.addError(self, test, err)
+        self.error_count += 1
+        self.report_error(test, err)
+        if self.stop_early:
+            self.stop()
+        self._cleanupLogFile(test)
 
     def addFailure(self, test, err):
         """Tell result that test failed.
@@ -364,21 +361,9 @@
         self.skip_count += 1
         self.report_skip(test, reason)
 
-    def _addNotApplicable(self, test, skip_excinfo):
-        if isinstance(skip_excinfo[1], TestNotApplicable):
-            self.not_applicable_count += 1
-            self.report_not_applicable(test, skip_excinfo)
-        try:
-            test.tearDown()
-        except KeyboardInterrupt:
-            raise
-        except:
-            self.addError(test, test.exc_info())
-        else:
-            # seems best to treat this as success from point-of-view of unittest
-            # -- it actually does nothing so it barely matters :)
-            unittest.TestResult.addSuccess(self, test)
-            test._log_contents = ''
+    def addNotApplicable(self, test, reason):
+        self.not_applicable_count += 1
+        self.report_not_applicable(test, reason)
 
     def printErrorList(self, flavour, errors):
         for test, err in errors:
@@ -523,7 +508,7 @@
     def report_skip(self, test, reason):
         pass
 
-    def report_not_applicable(self, test, skip_excinfo):
+    def report_not_applicable(self, test, reason):
         pass
 
     def report_unsupported(self, test, feature):
@@ -590,10 +575,9 @@
         self.stream.writeln(' SKIP %s\n%s'
                 % (self._testTimeString(test), reason))
 
-    def report_not_applicable(self, test, skip_excinfo):
-        self.stream.writeln('  N/A %s\n%s'
-                % (self._testTimeString(test),
-                   self._error_summary(skip_excinfo)))
+    def report_not_applicable(self, test, reason):
+        self.stream.writeln('  N/A %s\n    %s'
+                % (self._testTimeString(test), reason))
 
     def report_unsupported(self, test, feature):
         """test cannot be run because feature is missing."""
@@ -1589,6 +1573,17 @@
         else:
             addSkip(self, reason)
 
+    def _do_not_applicable(self, result, e):
+        addNotApplicable = getattr(result, 'addNotApplicable', None)
+        if addNotApplicable is not None:
+            if not e.args:
+                reason = 'No reason given'
+            else:
+                reason = e.args[0]
+            result.addNotApplicable(self, reason)
+        else:
+            self._do_skip(result, reason)
+
     def _do_unsupported_or_skip(self, result, reason):
         addNotSupported = getattr(result, 'addNotSupported', None)
         if addNotSupported is not None:
@@ -1627,6 +1622,10 @@
                 except KeyboardInterrupt:
                     self._runCleanups()
                     raise
+                except TestNotApplicable, e:
+                    self._do_not_applicable(result, e)
+                    self.tearDown()
+                    return
                 except TestSkipped, e:
                     self._do_skip(result, e.args[0])
                     self.tearDown()
@@ -1646,6 +1645,8 @@
                     ok = True
                 except self.failureException:
                     result.addFailure(self, sys.exc_info())
+                except TestNotApplicable, e:
+                    self._do_not_applicable(result, e)
                 except TestSkipped, e:
                     if not e.args:
                         reason = "No reason given."
@@ -1675,10 +1676,6 @@
                     ok = False
                 if ok: result.addSuccess(self)
                 return result
-            except TestNotApplicable:
-                # Not moved from the result [yet].
-                self._runCleanups()
-                raise
             except KeyboardInterrupt:
                 self._runCleanups()
                 raise

=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py	2009-11-01 04:01:50 +0000
+++ b/bzrlib/tests/test_selftest.py	2009-11-01 05:01:47 +0000
@@ -1128,11 +1128,12 @@
 
     def test_not_applicable(self):
         # run a test that is skipped because it's not applicable
-        def not_applicable_test():
-            raise tests.TestNotApplicable('this test never runs')
+        class Test(tests.TestCase):
+            def not_applicable_test(self):
+                raise tests.TestNotApplicable('this test never runs')
         out = StringIO()
         runner = tests.TextTestRunner(stream=out, verbosity=2)
-        test = unittest.FunctionTestCase(not_applicable_test)
+        test = Test("not_applicable_test")
         result = self.run_test_runner(runner, test)
         self._log_file.write(out.getvalue())
         self.assertTrue(result.wasSuccessful())




More information about the bazaar-commits mailing list