[英]How can I use hypothesis, and pytest-tornado yield-based testing, in the same test case?
我正在编写使用Tornado库的代码的py.test测试。 如何在涉及协同程序和IOLoop的测试中使用假设 ? 通过使用pytest-tornado的@pytest.mark.gen_test
,我已经能够在没有假设的情况下编写基于yield的测试,但是当我尝试将它与@given
结合使用@given
,我收到以下错误:
FailedHealthCheck:在
@given
下运行的测试应返回None
,但test_both
返回<generator object test_both at 0x7fc4464525f0>
。有关此内容的更多信息,请参见http://hypothesis.readthedocs.org/en/latest/healthchecks.html 。 如果要仅禁用此运行状况检查,请将
HealthCheck.return_value
添加到此测试的suppress_health_check
设置中。
我非常有信心这是一个真正的问题而不只是一个禁用健康检查的问题,考虑到假设文档说
基于产量的测试根本不起作用。
这是代表我的情况的代码:
class MyHandler(RequestHandler):
@gen.coroutine
def get(self, x):
yield gen.moment
self.write(str(int(x) + 1))
self.finish()
@pytest.fixture
def app():
return Application([(r'/([0-9]+)', MyHandler)])
@given(x=strategies.integers(min_value=0))
def test_hypothesis(x):
assert int(str(x)) == x
@pytest.mark.gen_test
def test_tornado(app, http_client, base_url):
x = 123
response = yield http_client.fetch('%s/%i' % (base_url, x))
assert int(response.body) == x + 1
@pytest.mark.gen_test
@given(x=strategies.integers(min_value=0))
def test_both(x, app, http_client, base_url):
response = yield http_client.fetch('%s/%i' % (base_url, x))
assert int(response.body) == x + 1
test_hypothesis
和test_tornado
工作正常,但我得到了test_both
的错误,因为我一起使用yield
和Hypothesis。
改变装饰器的顺序并没有改变任何东西,可能是因为gen_test
装饰器只是一个属性标记。
我可以编写使用假设的基于Tornado的代码的测试吗? 怎么样?
您可以通过在pytest-tornado的io_loop
py.test fixture上调用run_sync()
来完成此操作。 这可以用来代替yield
:
@given(x=strategies.integers(min_value=0))
def test_solution(x, app, http_client, base_url, io_loop):
response = io_loop.run_sync(
lambda: http_client.fetch('%s/%i' % (base_url, x)))
assert int(response.body) == x + 1
或者,您可以将测试的正文放在协程中,以便它可以继续使用yield
,并使用run_sync()
调用此协程:
@given(x=strategies.integers(min_value=0))
def test_solution_general(x, app, http_client, base_url, io_loop):
@gen.coroutine
def test_gen():
response = yield http_client.fetch('%s/%i' % (base_url, x))
assert int(response.body) == x + 1
io_loop.run_sync(test_gen)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.