Doing it in a fixture is too late: event_loop might have already been
called if the first test running is async, and it would fail on Windows.
This started to be needed after pytest-asyncio 0.17.0 was released.
See <https://github.com/pytest-dev/pytest-asyncio/issues/256>.
)
-@pytest.fixture
-def event_loop(request):
- """Return the event loop to test asyncio-marked tests."""
- # pytest-asyncio reset the the loop config after each test, so set
- # set them each time
-
- loop = request.config.getoption("--loop")
+def pytest_sessionstart(session):
+ # Configure the async loop.
+ loop = session.config.getoption("--loop")
if loop == "uvloop":
import uvloop
else:
assert loop == "default"
- loop = None
if sys.platform == "win32":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
- if not loop:
- loop = asyncio.get_event_loop_policy().new_event_loop()
- yield loop
- loop.close()