-
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
Release 0.14 yields ScopeMismatch for event_loop fixture #171
Comments
Thanks for your feedback. I have forked https://github.com/FAForever/server to reproduce it. And I get same result. The problem is that you have async fixtures that are session scope. E.g: test_data As copied from pytest-asyncio documentation:
So you just have to include next copy in your conftest.py:
Note: About why pytest doesn´t raise this error with pytest-asyncio V0.12 is something that I have no answer, and It's beyond pytest-asyncio. It's something that depends on pytest, but IMHO it should raise this error too with pytest-asyncio V0.12. See also #169, that also needs to increase event_loop scope, but it was not clear initially from pytest error messages. |
@Askaholic I agree with @alblasco , the plugin is stricter for certain classes of issues now. If you have a session-scoped async fixture, you should redefine the event loop to be session-scoped too. Is this doable? The problem with larger-scoped |
Thanks for the suggestion! Since it works on v0.12 I assumed it was some problem with Will redefining the |
|
Another option is to rework whatever fixture was using the event loop to be function scoped. It's not particularly dangerous to reuse the event loop. It's a tradeoff between efficiency (reusing the same loop) and having a cleaner test environment (recreating the loop, this making sure all event loop state is reset). |
I tried to redefine the event loop as you suggested, but now I'm getting a
Even though I pass the |
@Askaholic Interesting, could you prepare a minimal repro example somewhere so we can debug this? |
This does it for me: import asyncio
import pytest
@pytest.fixture(scope="session")
def event_loop():
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
@pytest.fixture(scope="session", autouse=True)
async def test_data(event_loop):
await asyncio.open_connection(
host="127.0.0.1",
port=3306,
loop=event_loop
)
@pytest.mark.asyncio
async def test_something():
assert True Yields a test error:
It seems that the problem comes from passing the event loop via the |
My 2c: since py3.8 (?) event loop should not be passed as an argument. Instead, the scaffolding should ensure that the current event loop is set correctly. And it will be used by |
New developments on this. I have managed to reproduce the ScopeMismatch error. It seems to be caused by defining an async fixture with scope other than function. Minimal example: import pytest
@pytest.fixture(scope="module")
async def async_fixture():
pass
def test_foo(async_fixture):
pass
# To demonstrate that it happens for both types of test functions
@pytest.mark.asyncio
async def test_bar(async_fixture):
pass Test output:
EDIT: Ha, I just read over the earlier comments which state exactly this :P |
Revisiting this again. So the reason why redefining the However... It made my tests take about twice as long to run (10 minutes rather than 5) which is kindof a big deal for me. I think this is mostly because I have a lot of background tasks that are created in function scoped fixtures, so the event loop just gets bogged down over the course of the test run. Since I just need my @pytest.fixture(scope="session", autouse=True)
def test_data():
async def _test_data():
# Contents of my old async fixture here
asyncio.run(_test_data()) This way I don't need to change the scope of the event loop for my tests. |
Thanks for keeping this thread updated! It sounds like the issue has been solved (apart from the performance problem). If anything else comes up, feel free to open another issue. |
When upgrading to pytest-asyncio 0.14 all of my tests error out with the following error:
This does not occur on pytest-asyncio 0.12.
System information:
Additionally here is a link to the travis build which also has this issue.
https://travis-ci.org/github/FAForever/server/builds/702782646
The text was updated successfully, but these errors were encountered: