简体   繁体   English

如何在同一测试用例中使用假设和基于pytest-tornado yield的测试?

[英]How can I use hypothesis, and pytest-tornado yield-based testing, in the same test case?

I'm writing py.test tests of my code that uses the Tornado library. 我正在编写使用Tornado库的代码的py.test测试。 How can I use Hypothesis in my tests that involve coroutines and the IOLoop? 如何在涉及协同程序和IOLoop的测试中使用假设 I've been able to write yield-based tests without Hypothesis by using pytest-tornado 's @pytest.mark.gen_test , but when I try to combine it with @given , I receive the following error: 通过使用pytest-tornado@pytest.mark.gen_test ,我已经能够在没有假设的情况下编写基于yield的测试,但是当我尝试将它与@given结合使用@given ,我收到以下错误:

FailedHealthCheck: Tests run under @given should return None , but test_both returned <generator object test_both at 0x7fc4464525f0> instead. FailedHealthCheck:在@given下运行的测试应返回None ,但test_both返回<generator object test_both at 0x7fc4464525f0>

See http://hypothesis.readthedocs.org/en/latest/healthchecks.html for more information about this. 有关此内容的更多信息,请参见http://hypothesis.readthedocs.org/en/latest/healthchecks.html If you want to disable just this health check, add HealthCheck.return_value to the suppress_health_check settings for this test. 如果要仅禁用此运行状况检查,请将HealthCheck.return_value添加到此测试的suppress_health_check设置中。

I'm pretty confident that this is a real problem and not just a question of disabling the health check, considering that the Hypothesis docs say 我非常有信心这是一个真正的问题而不只是一个禁用健康检查的问题,考虑到假设文档

yield based tests simply won't work. 基于产量的测试根本不起作用。

Here's code that demonstrates my situation: 这是代表我的情况的代码:

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 and test_tornado work fine, but I get the error with test_both because I'm using yield and Hypothesis together. test_hypothesistest_tornado工作正常,但我得到了test_both的错误,因为我一起使用yield和Hypothesis。

Changing the order of the decorators didn't change anything, probably because the gen_test decorator is simply an attribute mark. 改变装饰器的顺序并没有改变任何东西,可能是因为gen_test装饰器只是一个属性标记。

Can I write tests of my Tornado-based code that use Hypothesis? 我可以编写使用假设的基于Tornado的代码的测试吗? How? 怎么样?

You can accomplish this by calling run_sync() on the io_loop py.test fixture of pytest-tornado. 您可以通过在pytest-tornado的io_loop py.test fixture上调用run_sync()来完成此操作。 This can be used in place of yield : 这可以用来代替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

Or you can place the body of your test in a coroutine, so that it can continue to use yield , and call this coroutine with run_sync() : 或者,您可以将测试的正文放在协程中,以便它可以继续使用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.

相关问题 我可以对训练和测试数据使用假设检验吗? - Can I use hypothesis Testing on Train and Test data? 在带有假设的测试中使用 pytest 固定装置 - Use pytest fixtures in test with hypothesis 基于产量的协程是真正的协同程序吗? - Is yield-based coroutine is REAL coroutine? 如何使用将在 pytest 测试用例中用作参数化的夹具函数和数据的返回 - How can I use return of fixture function and data which will be used as parameterized in pytest test case Pytest CLI:如何按名称忽略测试用例? - Pytest CLI: how can i ignore test case by name? 在某些流程之间,如何在龙卷风中使用像`yield queue.get()`这样的队列? - How can I use queue like `yield queue.get()` in tornado ,between some Process? 使用pytest和假设进行异常处理和测试 - Exception handling and testing with pytest and hypothesis Python中基于收益的协程与@ asyncio.coroutine和@ types.coroutine装饰器的协程有何不同? - How do yield-based coroutines in Python differ from coroutines with @asyncio.coroutine and @types.coroutine decorators? 如何使用 pytest 测试使用 Flask 创建的路由? - How can I use pytest to test routes created using flask? 如何使用假设检验来比较组 - How to use hypothesis testing to compare groups
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM