Skip to content

Commit

Permalink
Provide stack trace information when assertFailure fails.
Browse files Browse the repository at this point in the history
 * Author: jml
 * Reviewer: exarkun
 * Fixes #1869

assertFailure (aka failUnlessFailure) used to only report the class
of the unexpected exception. Now it also reports the brief stacktrace
and message from that exception.



git-svn-id: svn://svn.twistedmatrix.com/svn/Twisted/trunk@17509 bbbe8e31-12d6-0310-92fd-ac37d47ddeeb
  • Loading branch information
jml committed Jul 9, 2006
1 parent b7e78eb commit ce0b00d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
21 changes: 20 additions & 1 deletion twisted/trial/test/test_assertions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import StringIO

from twisted.python import reflect
from twisted.python import reflect, failure
from twisted.python.util import dsu
from twisted.internet import defer
from twisted.trial import unittest, runner, reporter
Expand Down Expand Up @@ -273,6 +273,25 @@ def test_assertFailure_noException(self):
lambda x: x.trap(self.failureException))
return d

def test_assertFailure_moreInfo(self):
"""In the case of assertFailure failing, check that we get lots of
information about the exception that was raised.
"""
try:
1/0
except ZeroDivisionError:
f = failure.Failure()
d = defer.fail(f)
d = self.assertFailure(d, RuntimeError)
d.addErrback(self._checkInfo, f)
return d

def _checkInfo(self, assertionFailure, f):
assert assertionFailure.check(self.failureException)
output = assertionFailure.getErrorMessage()
self.assertIn(f.getErrorMessage(), output)
self.assertIn(f.getBriefTraceback(), output)

def test_assertFailure_masked(self):
"""A single wrong assertFailure should fail the whole test.
"""
Expand Down
23 changes: 13 additions & 10 deletions twisted/trial/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,9 @@ def failUnlessApproximates(self, first, second, tolerance, msg=None):
assertApproximates = failUnlessApproximates

def failUnlessFailure(self, deferred, *expectedFailures):
"""assert that deferred will errback a failure of type in expectedFailures
this is analagous to an async assertRaises
"""Assert that C{deferred} will errback with one of
C{expectedFailures}. Returns the original Deferred with callbacks
added. You will need to return this Deferred from your test case.
"""
def _cb(ignore):
raise self.failureException(
Expand All @@ -247,8 +248,9 @@ def _eb(failure):
if failure.check(*expectedFailures):
return failure.value
else:
raise self.failureException("%r not expected (%r)"
% (failure, expectedFailures))
output = ('\nExpected: %r\nGot:\n%s'
% (expectedFailures, str(failure)))
raise self.failureException(output)
return deferred.addCallbacks(_cb, _eb)
assertFailure = failUnlessFailure

Expand Down Expand Up @@ -313,13 +315,14 @@ def onTimeout(d):
% (self, methodName, timeout))
f = failure.Failure(e)
# try to errback the deferred that the test returns (for no gorram
# reason) (see issue1005 and test_errorPropagation in test_deferred)
# reason) (see issue1005 and test_errorPropagation in
# test_deferred)
try:
d.errback(f)
except defer.AlreadyCalledError:
# if the deferred has been called already but the *back chain is
# still unfinished, crash the reactor and report timeout error
# ourself.
# if the deferred has been called already but the *back chain
# is still unfinished, crash the reactor and report timeout
# error ourself.
reactor.crash()
self._timedOut = True # see self._wait
todo = self.getTodo()
Expand Down Expand Up @@ -568,8 +571,8 @@ def stop():
try:
d.addBoth(append)
if results:
# d might have already been fired, in which case append is called
# synchronously. Avoid any reactor stuff.
# d might have already been fired, in which case append is
# called synchronously. Avoid any reactor stuff.
return
d.addBoth(crash)
reactor.stop = stop
Expand Down

0 comments on commit ce0b00d

Please sign in to comment.