Skip to content
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

Closed
k4nar opened this issue Aug 24, 2017 · 8 comments
Closed

Pytest: Allow coroutine fixtures #2223

k4nar opened this issue Aug 24, 2017 · 8 comments
Labels

Comments

@k4nar
Copy link
Contributor

k4nar commented Aug 24, 2017

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.

@samuelcolvin
Copy link
Member

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())

@samuelcolvin
Copy link
Member

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.

@k4nar
Copy link
Contributor Author

k4nar commented Aug 24, 2017

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.

@samuelcolvin
Copy link
Member

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.

@k4nar
Copy link
Contributor Author

k4nar commented Aug 24, 2017

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.

k4nar added a commit to k4nar/aiohttp that referenced this issue Aug 25, 2017
k4nar added a commit to k4nar/aiohttp that referenced this issue Aug 25, 2017
k4nar added a commit to k4nar/aiohttp that referenced this issue Aug 25, 2017
k4nar added a commit to k4nar/aiohttp that referenced this issue Aug 25, 2017
@k4nar k4nar mentioned this issue Aug 25, 2017
5 tasks
k4nar added a commit to k4nar/aiohttp that referenced this issue Aug 25, 2017
k4nar added a commit to k4nar/aiohttp that referenced this issue Aug 25, 2017
k4nar added a commit to k4nar/aiohttp that referenced this issue Aug 25, 2017
k4nar added a commit to k4nar/aiohttp that referenced this issue Aug 28, 2017
k4nar added a commit to k4nar/aiohttp that referenced this issue Aug 28, 2017
k4nar added a commit to k4nar/aiohttp that referenced this issue Aug 28, 2017
k4nar added a commit to k4nar/aiohttp that referenced this issue Aug 28, 2017
k4nar added a commit to k4nar/aiohttp that referenced this issue Aug 29, 2017
k4nar added a commit to k4nar/aiohttp that referenced this issue Aug 29, 2017
@k4nar
Copy link
Contributor Author

k4nar commented Aug 29, 2017

I think the PR is ready (#2226). The tests pass and I'm using it with success in my project.

asvetlov pushed a commit that referenced this issue Sep 17, 2017
* Accept async fixtures in pytest plugin (#2223)

* Add tests for async fixtures (#2223)

* Update changelog

* Update CONTRIBUTORS.txt

* Update helpers.py
@asvetlov
Copy link
Member

Fixed by #2226

@lock
Copy link

lock bot commented Oct 28, 2019

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.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants