Async fixtures with pytest

前端 未结 2 572
陌清茗
陌清茗 2021-01-17 09:39

How do I define async fixtures and use them in async tests?

The following code, all in the same file, fails miserably. Is the fixture called plainly by the test runn

2条回答
  •  情话喂你
    2021-01-17 10:00

    You only need to mark your tests as async

    @pytest.mark.asyncio
    async def test_app(create_x, auth):
        api_client, x_id = create_x
        resp = await api_client.get(f'my_res/{x_id}', headers=auth)
        assert resp.status == web.HTTPOk.status_code
    

    This tells pytest to run the test inside an event loop rather than calling it directly.

    The fixtures can be marked as normal

    @pytest.fixture
    async def create_x(api_client):
        x_id = await add_x(api_client)
        return api_client, x_id
    

提交回复
热议问题