-
Notifications
You must be signed in to change notification settings - Fork 161
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
Make loop accessible outside of test function #29
Comments
So, if you're using the
If you have a more complex scenario, you don't actually need to use the marker at all. Just make an ordinary pytest test function that receives the event_loop fixture, do whatever you like, and then use the given event loop to run something ( Furthermore, pytest fixtures can depend on other fixtures. There's nothing stopping you from doing something like this:
I think this should work (can't test it out currently). It's more complex but if it fits your use case, a little cleaner. |
Ok, I had seen that option but thought that the fixture would receive a different instance of event_loop than the test function (I guess it figures out what order the fixtures need to be created in?). I just checked and it works for setting up, thanks. The only issue I'm having now is that the event_loop is closed before any teardown code is called. For instance:
When the teardown code runs the loop is closed, which throws |
Yeah, the problem is we're kinda bypassing the normal pytest fixture mechanism with the event loop. Fixtures are supposed to handle their own teardown, like in your snippet, but the event loop teardown is handled by the plugin (and before your fixture teardown). My idea was to do it this way to make writing your event_loop fixture trivial for beginners (just create an instance and return it) but now I'm not sure it was the best course of action. If you need a quick and dirty fix until this is sorted here, try something like this:
Yes, this is incredibly dirty, but should work for your case while I think of a good way of handling this. |
That makes a lot of sense. I agree that moving the close to the event_loop fixture would probably be the easiest to understand. Fortunately for my case it's not a show-stopper. Thanks for looking into it. |
As mentioned here I had to add @pytest.yield_fixture
def event_loop():
"""Create an instance of the default event loop for each test case."""
policy = asyncio.get_event_loop_policy()
res = policy.new_event_loop()
asyncio.set_event_loop(res)
res._close = res.close
res.close = lambda: None
yield res
res._close() |
Hi, this issue should be solved by the latest release (0.5.0). Please close if that's the case! |
This is still an issue with 0.5.0. The event loop is not properly restored, causing tests that expect an open event loop and that follow an asyncio-marked-test to fail.
|
@thomasst Should be fixed in master, could you give it a try from git and report back? |
Thanks, looks good! |
Removed unnecessary proxy check
pytest-asyncio 0.15.1 the problem is still here. I can not use I've tried @lars-tiede workaround, but I get "RuntimeError" "attached to a different loop" exception. And I tried @Tinche workaround, but I get |
The test should depend on |
I am using pytest_asyncio for my asyncio tests and am trying to get it to work with another fixture I wrote. They work together fine as long as I only access the loop inside the test function by calling functions on the passed in fixture. I was trying to figure out if it was possible to access the loop in the fixture function itself (or even pytest_runtest_setup).
I'm not that familiar with py.test internals, but as far as I can tell from reading examples and source code, it looks like pytest_asyncio creates the loop right before calling the test function (after *_setup and fixture functions are called) and then closes the loop right after execution. Is this right?
Specifically, what I'm trying to do is kick off an
asyncio.ensure_future()
right before the test runs using the loop that the test will be running on.The text was updated successfully, but these errors were encountered: