Bug 941158 - Honour expected failures in setUp or tearDown. r=zac

This commit is contained in:
Andreas Tolfsen 2014-02-05 17:04:42 -05:00
Родитель ed1449751c
Коммит 09762be6af
1 изменённых файлов: 33 добавлений и 11 удалений

Просмотреть файл

@ -99,6 +99,18 @@ class CommonTestCase(unittest.TestCase):
result.addSuccess(self)
def run(self, result=None):
# Bug 967566 suggests refactoring run, which would hopefully
# mean getting rid of this inner function, which only sits
# here to reduce code duplication:
def expected_failure(result):
addExpectedFailure = getattr(result, "addExpectedFailure", None)
if addExpectedFailure is not None:
addExpectedFailure(self, e.exc_info)
else:
warnings.warn("TestResult has no addExpectedFailure method, "
"reporting as passes", RuntimeWarning)
result.addSuccess(self)
self.start_time = time.time()
orig_result = result
if result is None:
@ -124,11 +136,19 @@ class CommonTestCase(unittest.TestCase):
try:
success = False
try:
self.setUp()
if self.expected == "fail":
try:
self.setUp()
except Exception:
raise _ExpectedFailure(sys.exc_info())
else:
self.setUp()
except SkipTest as e:
self._addSkip(result, str(e))
except KeyboardInterrupt:
raise
except _ExpectedFailure:
expected_failure(result)
except:
result.addError(self, sys.exc_info())
else:
@ -136,7 +156,7 @@ class CommonTestCase(unittest.TestCase):
if self.expected == 'fail':
try:
testMethod()
except Exception:
except:
raise _ExpectedFailure(sys.exc_info())
raise _UnexpectedSuccess
else:
@ -147,14 +167,8 @@ class CommonTestCase(unittest.TestCase):
raise
except self.failureException:
result.addFailure(self, sys.exc_info())
except _ExpectedFailure as e:
addExpectedFailure = getattr(result, 'addExpectedFailure', None)
if addExpectedFailure is not None:
addExpectedFailure(self, e.exc_info)
else:
warnings.warn("TestResult has no addExpectedFailure method, reporting as passes",
RuntimeWarning)
result.addSuccess(self)
except _ExpectedFailure:
expected_failure(result)
except _UnexpectedSuccess:
addUnexpectedSuccess = getattr(result, 'addUnexpectedSuccess', None)
if addUnexpectedSuccess is not None:
@ -170,9 +184,17 @@ class CommonTestCase(unittest.TestCase):
else:
success = True
try:
self.tearDown()
if self.expected == "fail":
try:
self.tearDown()
except:
raise _ExpectedFailure(sys.exc_info())
else:
self.tearDown()
except KeyboardInterrupt:
raise
except _ExpectedFailure:
expected_failure(result)
except:
result.addError(self, sys.exc_info())
success = False