summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Lange <jml@mumak.net>2016-01-30 09:27:30 +0000
committerJonathan Lange <jml@mumak.net>2016-01-30 09:27:30 +0000
commit91c195c514b5fe01533bb68f993d243a61be1555 (patch)
tree2169afdf8a8dcd8717902f8041a3bae387e6b697
parentaf8e294ed1f670c27683d24362d099f514ad9d87 (diff)
downloadtesttools-91c195c514b5fe01533bb68f993d243a61be1555.tar.gz
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.
-rw-r--r--NEWS3
-rw-r--r--requirements.txt2
-rw-r--r--testtools/_deferreddebug.py21
-rw-r--r--testtools/_spinner.py16
-rw-r--r--testtools/tests/test_deferredruntest.py13
5 files changed, 45 insertions, 10 deletions
diff --git a/NEWS b/NEWS
index 514e9ed..5deb6d1 100644
--- a/NEWS
+++ b/NEWS
@@ -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
~~~~~
diff --git a/requirements.txt b/requirements.txt
index 978514f..100d8e4 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -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
diff --git a/testtools/_deferreddebug.py b/testtools/_deferreddebug.py
new file mode 100644
index 0000000..75da93d
--- /dev/null
+++ b/testtools/_deferreddebug.py
@@ -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))
diff --git a/testtools/_spinner.py b/testtools/_spinner.py
index fa1b18b..9205a03 100644
--- a/testtools/_spinner.py
+++ b/testtools/_spinner.py
@@ -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()
diff --git a/testtools/tests/test_deferredruntest.py b/testtools/tests/test_deferredruntest.py
index b48b3ba..92cd624 100644
--- a/testtools/tests/test_deferredruntest.py
+++ b/testtools/tests/test_deferredruntest.py
@@ -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):