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 example in testing.rst #939

Closed
faassen opened this issue Jun 21, 2016 · 6 comments
Closed

pytest example in testing.rst #939

faassen opened this issue Jun 21, 2016 · 6 comments
Labels

Comments

@faassen
Copy link

faassen commented Jun 21, 2016

There are a few confusing things concerning pytest in testing.rst:

  • it doesn't uses async/await unlike the rest of the docs but @asyncio.coroutine and yield from.
  • it says in the comment "# assuming you are using pytest-asyncio" but then it sets up its entirely independent loop fixture and uses loop.run_until_complete explicitly, instead of using pytest.mark.asyncio from pytest-asyncio

If people agree that these should be changed to async/await and pytest.mark.asyncio then I can create a pull request to update the example.

@faassen
Copy link
Author

faassen commented Jun 21, 2016

In addition the examples refer to a _create_example_app and in the pytest case, create_app, but they're not spelled out. But this would be useful for me to see for newbies like me.

@faassen
Copy link
Author

faassen commented Jun 21, 2016

When I try to naively update the example to use pytest-asyncio and change the fixtures to use event_loop from pytest-asyncio and also use @pytest.mark.asyncio, things fail for me with errors. So I think the docs should either explain you should not use pytest-asyncio or also include a pytest-asyncio example that works.

@pfreixes
Copy link
Contributor

pfreixes commented Jul 3, 2016

Agree that there is a confusion with the testing.rst file, however the test case coded is not oriented to the pytest-asyncio plugin.

Talking about the issue that you mentioned it is a well known issue that is like a pain in the ass, and already reported and in the roadmap for future versions of pytest-asyncio [1].

There is a workaround that can be modified and use it to avoid the issue of close the loop just after the execution of the tests. The following fixture will replace the event_loop used by py.test and will avoid the side effects of the premature close of the loop faking it as a lambda parameter yielding the responsibility to close the loop to the loop_context delivered by aiohttp.

@pytest.yield_fixture
def loop():
    with loop_context() as loop:
        yield loop

@pytest.yield_fixture
def event_loop(loop):
    loop._close = loop.close
    loop.close = lambda: None
    yield loop
    loop.close = loop._close

However will be nice if the guys from asyncio keep on eye on the pytest-asyncio project and once this issue will be fixed code another test example using the pattern that uses pytest.mark.asyncio decorator having a less verbose code /cc @asvetlov

[1] pytest-dev/pytest-asyncio#29

@faassen
Copy link
Author

faassen commented Jul 4, 2016

Yes, the test code isn't oriented to pytest-asyncio but it mentions you assume you use it in a comment, which is very misleading, as it's the opposite of what it actually does... I went with ignoring pytest-asyncio altogether myself. The documentation should be modified so it says you shouldn't be using pytest-asyncio if you use this pattern.

@asvetlov
Copy link
Member

I think the issue can be closed.

Feel free to open another one if you discovered a new issue with testing doc.

@lock
Copy link

lock bot commented Oct 29, 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.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 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