Commits

Michael Foord committed cceb029

Further testing for expectedFailures and setUp / tearDown / cleanUps

Comments (0)

Files changed (3)

unittest2/case.py

                           DeprecationWarning, 2)
             result.addSuccess(self)
 
+    def _addExpectedFailure(self, result, exc_info):
+        addExpectedFailure = getattr(result, 'addExpectedFailure', None)
+        if addExpectedFailure is not None:
+            addExpectedFailure(self, exc_info)
+        else:
+            warnings.warn("Use of a TestResult without an addExpectedFailure method is deprecated",
+                          DeprecationWarning)
+            result.addSuccess(self)
+
     def run(self, result=None):
         orig_result = result
         if result is None:
                 self._addSkip(result, str(e))
             except Exception:
                 exc_info = sys.exc_info()
-                addExpectedFailure = getattr(result, 'addExpectedFailure', None)
-
                 if getattr(testMethod, '_expectedFailure', None) is not None:
-                    if addExpectedFailure is not None:
-                        addExpectedFailure(self, exc_info)
-                    else:
-                        warnings.warn("Use of a TestResult without an addExpectedFailure method is deprecated",
-                                      DeprecationWarning)
+                    self._addExpectedFailure(result, exc_info)
                 else:
                     result.addError(self, exc_info)
             else:
                     testMethod()
                 except self.failureException:
                     result.addFailure(self, sys.exc_info())
-                except _ExpectedFailure, e:
-                    addExpectedFailure = getattr(result, 'addExpectedFailure', None)
-                    if addExpectedFailure is not None:
-                        addExpectedFailure(self, e.exc_info)
-                    else:
-                        warnings.warn("Use of a TestResult without an addExpectedFailure method is deprecated",
-                                      DeprecationWarning)
-                        result.addSuccess(self)
+                except _ExpectedFailure:
+                    self._addExpectedFailure(result, sys.exc_info())
                 except _UnexpectedSuccess:
                     addUnexpectedSuccess = getattr(result, 'addUnexpectedSuccess', None)
                     if addUnexpectedSuccess is not None:
                 try:
                     self.tearDown()
                 except Exception:
-                    result.addError(self, sys.exc_info())
-                    success = False
+                    exc_info = sys.exc_info()
+                    if getattr(testMethod, '_expectedFailure', None) is not None:
+                        self._addExpectedFailure(result, exc_info)
+                    else:
+                        result.addError(self, exc_info)
+                        success = False
 
             cleanUpSuccess = self.doCleanups()
             success = success and cleanUpSuccess

unittest2/test/support.py

         self.shouldStop = False
 
     def startTest(self, test):
-        pass
+        # so this fake TestResult can still count tests
+        self.testsRun += 1
 
     def stopTest(self, test):
         pass

unittest2/test/test_case.py

         test(result)
 
         self.assertEqual(len(result.errors), 0)
+        self.assertEqual(len(result.failures), 0)
         self.assertEqual(len(result.expectedFailures), 1)
         self.assertEqual(result.testsRun, 1)
 
         test(result)
 
         self.assertEqual(len(result.errors), 0)
+        self.assertEqual(len(result.failures), 0)
         self.assertEqual(len(result.expectedFailures), 1)
+        self.assertEqual(len(result.unexpectedSuccesses), 0)
         self.assertEqual(result.testsRun, 1)
 
         result = OldTestResult()
         test(result)
 
         self.assertEqual(len(result.errors), 0)
+        self.assertEqual(len(result.failures), 0)
         self.assertEqual(len(result.expectedFailures), 1)
+        self.assertEqual(len(result.unexpectedSuccesses), 0)
         self.assertEqual(result.testsRun, 1)
 
         result = OldTestResult()