-
-
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
Rely on pytest-asyncio capabilities #2496
Comments
What is MR? |
Pull Request/Merge Request. Github/Gitlab terminology :) |
You could try but I expect pitfalls on this way. |
I'll have a look at it and see how it goes |
As far as I can tell, currently the testing facilities of aiohttp (like aiohttp_client) are incompatible with pytest-asyncio. I get errors about missing event_loops which suggests that they screw up each others setups:
As you can see this happens even though I’m not using the fixtures at once. This is a super bummer because one of the reasons I love to using async frameworks is the option of easily mixing a web framework with general purpose network code. While I’m sure interop can be created in other ways, it would be really nice if we had one really good goto solution that everyone relied on. :) |
According to #1097, well written app allows to passing loop explicitly on init. I've managed to get rid of import asyncio
from aiohttp.web import Application, run_app
class Foo:
def __init__(self, app):
self.queue = asyncio.Queue(loop=app.loop) # this will crush with pytest without current loop
def make_app(loop=None):
app = Application()
if loop is not None:
app._set_loop(loop)
app.foo = Foo(app) # this one will get loop from app
if __name__ == '__main__':
run_app(make_app()) and in import pytest
from myapp import make_app
@pytest.fixture
def cli(loop, aiohttp_client):
return loop.run_until_complete(aiohttp_client(make_app(loop))) |
What pytest plugin do you use for running async tests? |
I have both installed. Not all tests are related to |
Yeah I think this is a key point: if I write a “normal” web app, I just go for Falcon or Pyramid. I specifically use aiohttp only if the application has a good reason to be async. That usually means that it communicates with the world in other ways than just HTTP. That’s why aiohttp's testing facilities remain useless to me as long as they stop me from testing the usually more important parts of my applications. |
Well, simple question: What pytest-asyncio feature is missing in pytest-aiohttp? |
Excellent question! I kind of assumed it’s lacking something but it doesn’t seem to. It even saves me the |
Nice to her it. I'll manage a smooth transition someday but keeping backward compatibility is a pain as usual. |
On https://us-pycon-2019-tutorial.readthedocs.io/aiohttp_tests.html is written: |
IMHO it is, still. |
First time looking at this thread, but reading through I notice there is also just some bad asyncio code being attributed to a problem with the pytest plugins... For example:
|
Not related to this report, so please open a new discussion next time to avoid spamming others. asvetlov usually seems to recommend creating a test server, which avoids mocking entirely, and allows to fully test the connection. I often prefer mocking though, in which case you can take a look at the aioresponses library. |
You have a usage question, so you can ask on StackOverflow or Discourse. |
I tried SO but with no success. |
Sorry to hear that! Note that Dreamsorcerer gave you an answer: mocking is one possible technique (see |
FWIW we should probably replace the Discourse link with https://github.com/aio-libs/aiohttp/discussions (the org-global link is already pointing at https://github.com/aio-libs/.github/discussions). @ghuname sounds like you may want to start a conversation at https://github.com/aio-libs/aiohttp/discussions/categories/q-a. |
Thanks, I have started discussion there on #6497 |
Fixed by pytest-asyncio 0.17 and pytest-aiohttp 1.0 |
I think it would be healthy to rely some of the asynchronous testing capabilities to https://github.com/pytest-dev/pytest-asyncio/.
For example it provides support for asynchronous fixtures, provides an
event_loop
fixture, etc.IMO
pytest-aiohttp
should dedicate to improve/provide fixtures related to aiohttp itself liketest_client
and the likes and let the other things more asyncio related topytest-asyncio
. This way we would also avoid problems (pytest-dev/pytest-asyncio#52) where users usepytest-asyncio
andpytest-aiohttp
for testing (myself included)If you agree I volunteer to start a MR to see how it would look like.
The text was updated successfully, but these errors were encountered: