如何让 httpretty 在测试期间停止打印异常?
How can I get httpretty to stop printing exceptions during testing?
我有一个模拟来自不存在的远程服务器的请求的测试:
@httpretty.activate
def test_no_such_host(self):
def no_such_host(request, uri, headers):
raise requests.ConnectionError()
httpretty.register_uri(
'GET',
EXAMPLE_WEBFINGER_URL,
status=200,
headers = {
'Content-Type': 'application/jrd+json',
},
body = no_such_host,
)
webfinger = get_webfinger(
EXAMPLE_USERNAME,
EXAMPLE_HOSTNAME,
)
self.assertEqual(
webfinger.url,
None,
)
get_webfinger
实际上赶上了 ConnectionError
。当它 运行s 时,测试通过——但它也报告来自 httpretty 线程的 ConnectionError
异常:
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/home/marnanel/alpha/lib/python3.6/site-packages/httpretty/core.py", line 781, in fill_filekind
status, headers, self.body = self.callable_body(self.request, self.info.full_url(), headers)
File "/home/marnanel/proj/kepi/kepi/sombrero_sendpub/tests/test_webfinger.py", line 183, in no_such_host
raise requests.ConnectionError()
requests.exceptions.ConnectionError
.
----------------------------------------------------------------------
Ran 1 test in 0.117s
OK
Destroying test database for alias 'default'...
单元测试或正在测试的代码中没有线程。这是许多类似的测试之一。那些不涉及异常的都是 运行 完美。
如何让它停止打印异常?
HTTPPretty 使用线程 simulate socket timeouts but are not handling exceptions correctly, see issue #334。后者提出了一种处理异常的方法,但尚未被维护者采纳(尚未)。
但是,您看到的消息是默认打印的 threading.excepthook()
implementation。你可以设置自己的钩子;空操作 lambda 会使错误消失:
threading.excepthook = lambda a: None
您可以使用上下文管理器暂时禁用挂钩:
import threading
from contextlib import contextmanager
@contextmanager
def suppress_thread_exceptions():
orig = threading.excepthook
threading.excepthook = lambda a: None
try:
yield
finally:
threading.excepthook = orig
然后在你的测试中使用上面的:
with suppress_thread_exceptions():
# use HTTPretty
我有一个模拟来自不存在的远程服务器的请求的测试:
@httpretty.activate
def test_no_such_host(self):
def no_such_host(request, uri, headers):
raise requests.ConnectionError()
httpretty.register_uri(
'GET',
EXAMPLE_WEBFINGER_URL,
status=200,
headers = {
'Content-Type': 'application/jrd+json',
},
body = no_such_host,
)
webfinger = get_webfinger(
EXAMPLE_USERNAME,
EXAMPLE_HOSTNAME,
)
self.assertEqual(
webfinger.url,
None,
)
get_webfinger
实际上赶上了 ConnectionError
。当它 运行s 时,测试通过——但它也报告来自 httpretty 线程的 ConnectionError
异常:
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/home/marnanel/alpha/lib/python3.6/site-packages/httpretty/core.py", line 781, in fill_filekind
status, headers, self.body = self.callable_body(self.request, self.info.full_url(), headers)
File "/home/marnanel/proj/kepi/kepi/sombrero_sendpub/tests/test_webfinger.py", line 183, in no_such_host
raise requests.ConnectionError()
requests.exceptions.ConnectionError
.
----------------------------------------------------------------------
Ran 1 test in 0.117s
OK
Destroying test database for alias 'default'...
单元测试或正在测试的代码中没有线程。这是许多类似的测试之一。那些不涉及异常的都是 运行 完美。
如何让它停止打印异常?
HTTPPretty 使用线程 simulate socket timeouts but are not handling exceptions correctly, see issue #334。后者提出了一种处理异常的方法,但尚未被维护者采纳(尚未)。
但是,您看到的消息是默认打印的 threading.excepthook()
implementation。你可以设置自己的钩子;空操作 lambda 会使错误消失:
threading.excepthook = lambda a: None
您可以使用上下文管理器暂时禁用挂钩:
import threading
from contextlib import contextmanager
@contextmanager
def suppress_thread_exceptions():
orig = threading.excepthook
threading.excepthook = lambda a: None
try:
yield
finally:
threading.excepthook = orig
然后在你的测试中使用上面的:
with suppress_thread_exceptions():
# use HTTPretty