pytest-asyncio

Async fixtures with pytest

泄露秘密 提交于 2019-12-10 12:58:30
问题 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 runner and not awaited? @pytest.fixture async def create_x(api_client): x_id = await add_x(api_client) return api_client, x_id 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 producing ==============================

How to timeout an async test in pytest with fixture?

喜你入骨 提交于 2019-12-05 19:14:02
问题 I am testing an async function that might get deadlocked. I tried to add a fixture to limit the function to only run for 5 seconds before raising a failure, but it hasn't worked so far. Setup: pipenv --python==3.6 pipenv install pytest==4.4.1 pipenv install pytest-asyncio==0.10.0 Code: import asyncio import pytest @pytest.fixture def my_fixture(): # attempt to start a timer that will stop the test somehow asyncio.ensure_future(time_limit()) yield 'eggs' async def time_limit(): await asyncio