Twisted tests now pass under --debug-stacktraces

Our tests make assertions that only hold when debugging is disabled.
This patch updates the code to ensure that debugging is in the state
we want.
This commit is contained in:
Jonathan Lange
2016-01-30 09:27:30 +00:00
parent af8e294ed1
commit 91c195c514
5 changed files with 45 additions and 10 deletions

3
NEWS
View File

@@ -12,6 +12,9 @@ Changes
* Python 2.6 and 3.2 are no longer supported. If you want to use either of
these versions of Python, use testtools 1.9.0. (Jonathan Lange)
* Make ``fixtures`` a real dependency, not just a test dependency.
(Jonathan Lange)
1.9.0
~~~~~

View File

@@ -1,9 +1,9 @@
pbr>=0.11
extras
fixtures>=1.3.0
pyrsistent
# 'mimeparse' has not been uploaded by the maintainer with Python3 compat
# but someone kindly uploaded a fixed version as 'python-mimeparse'.
python-mimeparse
unittest2>=1.0.0
traceback2
fixtures>=1.3.0

View File

@@ -0,0 +1,21 @@
# Copyright (c) testtools developers. See LICENSE for details.
#
# TODO: Move this to testtools.twistedsupport. See testing-cabal/testtools#202.
from fixtures import Fixture, MonkeyPatch
class DebugTwisted(Fixture):
"""Set debug options for Twisted."""
def __init__(self, debug=True):
super(DebugTwisted, self).__init__()
self._debug_setting = debug
def _setUp(self):
self.useFixture(
MonkeyPatch('twisted.internet.defer.Deferred.debug',
self._debug_setting))
self.useFixture(
MonkeyPatch('twisted.internet.base.DelayedCall.debug',
self._debug_setting))

View File

@@ -16,12 +16,12 @@ __all__ = [
'trap_unhandled_errors',
]
from fixtures import Fixture
import signal
from testtools.monkey import MonkeyPatcher
from testtools._deferreddebug import DebugTwisted
from twisted.internet import defer
from twisted.internet.base import DelayedCall
from twisted.internet.interfaces import IReactorThreads
from twisted.python.failure import Failure
from twisted.python.util import mergeFunctionMetadata
@@ -265,12 +265,12 @@ class Spinner(object):
:return: Whatever is at the end of the function's callback chain. If
it's an error, then raise that.
"""
debug = MonkeyPatcher()
if self._debug:
debug.add_patch(defer.Deferred, 'debug', True)
debug.add_patch(DelayedCall, 'debug', True)
debug.patch()
try:
debug_settings = DebugTwisted(True)
else:
debug_settings = Fixture()
with debug_settings:
junk = self.get_junk()
if junk:
raise StaleJunkError(junk)
@@ -300,5 +300,3 @@ class Spinner(object):
return self._get_result()
finally:
self._clean()
finally:
debug.restore()

View File

@@ -12,6 +12,7 @@ from testtools import (
TestCase,
TestResult,
)
from testtools._deferreddebug import DebugTwisted
from testtools.matchers import (
AfterPreprocessing,
Contains,
@@ -388,6 +389,10 @@ class TestAsynchronousDeferredRunTest(NeedsTwistedTestCase):
def test_unhandled_error_from_deferred(self):
# If there's a Deferred with an unhandled error, the test fails. Each
# unhandled error is reported with a separate traceback.
# We're interested in the behavior when debugging is disabled. When
# debugging is enabled, we get more stack traces.
self.useFixture(DebugTwisted(False))
class SomeCase(TestCase):
def test_cruft(self):
# Note we aren't returning the Deferred so that the error will
@@ -415,6 +420,10 @@ class TestAsynchronousDeferredRunTest(NeedsTwistedTestCase):
# If there's a Deferred with an unhandled error, the test fails. Each
# unhandled error is reported with a separate traceback, and the error
# is still reported.
# We're interested in the behavior when debugging is disabled. When
# debugging is enabled, we get more stack traces.
self.useFixture(DebugTwisted(False))
class SomeCase(TestCase):
def test_cruft(self):
# Note we aren't returning the Deferred so that the error will
@@ -573,6 +582,10 @@ class TestAsynchronousDeferredRunTest(NeedsTwistedTestCase):
def test_only_addError_once(self):
# Even if the reactor is unclean and the test raises an error and the
# cleanups raise errors, we only called addError once per test.
# We're interested in the behavior when debugging is disabled. When
# debugging is enabled, we get more stack traces.
self.useFixture(DebugTwisted(False))
reactor = self.make_reactor()
class WhenItRains(TestCase):
def it_pours(self):