-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pytest: Allow coroutine fixtures #2223
Comments
It's very simple right now to create fixtures which execute asynchronous code, for example: @pytest.yield_fixture
def redis(loop):
async def _redis():
redis = await create_redis(('localhost', 6379))
await redis.flushdb()
return redis
redis = loop.run_until_complete(_redis())
yield redis
async def _close():
redis.close()
await redis.wait_closed()
loop.run_until_complete(_close()) |
Personally I think the way aiohttp executes async tests is good and I think @asvetlov built it this way specifically because he didn't agree with how pytest-asyncio was setup. |
Yes, I know that it's possible to execute asynchronous code in a fixture as in your example, however I find that it's a bit too much boilerplate. Actually I've managed to adapt pytest-asyncio's hack to pytest-aiohttp, I'll open a PR shortly. |
so what happens with fixtures which are session scoped? Is the same loop running thoughout all tests? From my understanding, accepting this kind of fixture in aiohttp without hacks would require a big change. |
It only works for function fixtures as it implies the usage of the loop fixture. So far what I have is not intrusive, it's just a pytest hook (mostly like the one in pytest-asyncio). I'm currently writing tests for it. |
I think the PR is ready (#2226). The tests pass and I'm using it with success in my project. |
Fixed by #2226 |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs. |
pytest-asyncio provides a very nice feature, which is to allow coroutine fixtures. For example it makes possible to create a fixture making asynchronous DB requests.
Unfortunately pytest-asyncio and pytest-aiohttp seems to no longer be compatible. So far this is the only feature that I'm missing, so it would be awesome if it were added to pytest-aiohttp.
Here is the implementation in pytest-asyncio:
https://github.com/pytest-dev/pytest-asyncio/blob/92b34e5e9a14c45062d6ce2e2686ed8b11c7c03e/pytest_asyncio/plugin.py#L52-L134
I've tried to adapt it to pytest-aiohttp but unsuccessfully so far.
The text was updated successfully, but these errors were encountered: