diff options
author | Clay Gerrard <clay.gerrard@gmail.com> | 2017-06-13 10:46:10 -0700 |
---|---|---|
committer | Clay Gerrard <clay.gerrard@gmail.com> | 2017-06-13 10:51:09 -0700 |
commit | 01f5a9f3af3a1630297a92dff0f998b4f0e97a49 (patch) | |
tree | e7d26c1641617ed8761cc5ac3d5d3bb59e7e941e | |
parent | f18d070b0be3511ecb86c3299469024132561bc8 (diff) | |
download | python-swiftclient-01f5a9f3af3a1630297a92dff0f998b4f0e97a49.tar.gz |
Support pdb in tests better
Not really "better" so much as "at all" - the thing we do with the
capture stderr *everywhere* is probably brilliant - but absolutely not
strictly necessary for every MockHttpTest TestCase and comes with the
annoying overhead of trying to get into a debugger causes tests to hang
inexplicably and you can't even do debug prints in tests!?
Now if you add SWIFTCLIENT_DEBUG=1 to your nose -vsx command you can not
only jump into debugger, but if you're "in the know" you could even get
some stderr print debugging going on!
If you're not "in the know" when you try to pdb.set_trace() the tests
will blow-up for you because we monkeypatch pdb when not in
SWIFTCLIENT_DEBUG mode, you're welcome.
Change-Id: I21298bfd39fe386b5ea19e3a6f4408d8a0459c92
-rw-r--r-- | tests/unit/utils.py | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/tests/unit/utils.py b/tests/unit/utils.py index c05146e..f7e48d3 100644 --- a/tests/unit/utils.py +++ b/tests/unit/utils.py @@ -20,6 +20,7 @@ from time import sleep import unittest import mock import six +import os from six.moves import reload_module from six.moves.urllib.parse import urlparse, ParseResult from swiftclient import client as c @@ -206,7 +207,19 @@ class MockHttpTest(unittest.TestCase): # won't cover the references to sys.stdout/sys.stderr in # swiftclient.multithreading self.capture_output = CaptureOutput() - self.capture_output.__enter__() + if 'SWIFTCLIENT_DEBUG' not in os.environ: + self.capture_output.__enter__() + self.addCleanup(self.capture_output.__exit__) + + # since we're going to steal all stderr output globally; we should + # give the developer an escape hatch or risk scorn + def blowup_but_with_the_helpful(*args, **kwargs): + raise Exception( + "You tried to enter a debugger while stderr is " + "patched, you need to set SWIFTCLIENT_DEBUG=1 " + "and try again") + import pdb + pdb.set_trace = blowup_but_with_the_helpful def fake_http_connection(*args, **kwargs): self.validateMockedRequestsConsumed() @@ -384,7 +397,6 @@ class MockHttpTest(unittest.TestCase): # un-hygienic mocking on the swiftclient.client module; which may lead # to some unfortunate test order dependency bugs by way of the broken # window theory if any other modules are similarly patched - self.capture_output.__exit__() reload_module(c) |