-
Notifications
You must be signed in to change notification settings - Fork 159
Sync wrapper for hypothesis tests uses the event loop provided by pytest-asyncio #122
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
Sync wrapper for hypothesis tests uses the event loop provided by pytest-asyncio #122
Conversation
pytest_asyncio/plugin.py
Outdated
) | ||
# It is important to yield, not return. Otherwise the event_loop fixture will get cleaned up too early | ||
# when using a combination of hypothesis and pytest.mark.parametrize | ||
yield None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the comment says, return None
will cause problems with parametrized hypothesis tests. However, changing return None
to yield None
breaks test_simple::test_asyncio_marker_fail
for no apparent reason. Do you have any idea why this might cause xfail
to break?
I can't put my finger on the exact problem, but I have a bad feeling about isolation and the timing of cleanup. That is, that the event_loop might live too long and there might be cases where it gets closed between runs of the test case. The yield-not-return thing makes me super suspicious for this reason. |
f35c2c3
to
dee505e
Compare
You are right: The "yield-not-return" thing is smelly and affects other pytest markers, so I got rid of it. At this point, the parametrization is broken, because the event loop is already closed after the first parameter. This is weird, because Thanks to @TimothyFitz who added a test case that reproduces the issue! |
…est-asyncio instead of creating a new loop.
2f02fbd
to
ee348b3
Compare
All tests run successfully now and the issue with the prematurely closed event loop seems to be fixed. I did this by simplifying the wrapper for async functions: |
There is only one marker and one fixture provided by pytest-asyncio. Retrieving these from a dictionary seems unnecessary complicated.
…eturns the loop provided by the event_loop fixture of pytest-asyncio.
I also added two refactoring commits that hopefully make the code easier to understand. |
Congratulations! This looks great to me, and the refactoring makes it much easier to follow. |
1 similar comment
Congratulations! This looks great to me, and the refactoring makes it much easier to follow. |
LGTM! |
Async test functions marked for execution by Hypothesis are currently wrapped in a synchronous function during the test discovery phase of pytest. At this point, the
event_loop
fixture provided by pytest-asyncio has not been evaluated and the wrapper is forced to create a new loop for each hypothesis test. This may result is problems when using async-related code in fixtures as in #117.This PR moves the wrapping code to the actual test execution. At this point the
event_loop
fixture is evaluated and there is no need to create a new loop for the test.What's missing from this PR is a proper automated test case. Since I have run into this issue using aiohttp, I was using the following code to test it:
The test succeeds with the patch and fails otherwise.
Currently, one of the test cases still fails, so this cannot be merged. I would like to hear your comments on this, though! Especially from @Zac-HD as the original author of the hypothesis integration and @VladimirWork to see if this patch also solves his problem described in #117.