]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Configure the async loop at test session start
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 13 Jan 2022 16:17:37 +0000 (17:17 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 13 Jan 2022 17:05:17 +0000 (18:05 +0100)
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>.

tests/conftest.py

index d22c86f860c556dc688ce44ad98c640340eb6d2d..77c08ce1b2cc003d85d3da2131213605b229044c 100644 (file)
@@ -62,13 +62,9 @@ def retries(request):
         )
 
 
-@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
 
@@ -76,10 +72,5 @@ def event_loop(request):
     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()