From 0698aee02c2207a8cef6fea9ebf1361d4bf201a3 Mon Sep 17 00:00:00 2001 From: Michael Seifert Date: Sun, 26 Jun 2022 14:06:21 +0200 Subject: [PATCH 1/3] docs: Removed contradicting information about the event_loop fixture not setting the global event loop. Signed-off-by: Michael Seifert --- README.rst | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index 17a10f67..5efc8d6c 100644 --- a/README.rst +++ b/README.rst @@ -123,19 +123,16 @@ Fixtures ``event_loop`` ~~~~~~~~~~~~~~ -Creates and injects a new instance of the default asyncio event loop. By -default, the loop will be closed at the end of the test (i.e. the default -fixture scope is ``function``). +Creates a new asyncio event loop based on the current event loop policy. The new loop +is available as the return value of this fixture or via `asyncio.get_running_loop `__. +The event loop is closed when the fixture scope ends. The fixture scope defaults +to ``function`` scope. Note that just using the ``event_loop`` fixture won't make your test function a coroutine. You'll need to interact with the event loop directly, using methods like ``event_loop.run_until_complete``. See the ``pytest.mark.asyncio`` marker for treating test functions like coroutines. -Simply using this fixture will not set the generated event loop as the -default asyncio event loop, or change the asyncio event loop policy in any way. -Use ``pytest.mark.asyncio`` for this purpose. - .. code-block:: python def test_http_client(event_loop): From 8eaaebe2f3b54f98f26754763e4e486045750225 Mon Sep 17 00:00:00 2001 From: Michael Seifert Date: Sun, 26 Jun 2022 14:10:39 +0200 Subject: [PATCH 2/3] docs: Clarified the effect of pytest.mark.asyncio with regards to the event_loop fixture. Signed-off-by: Michael Seifert --- README.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 5efc8d6c..b7a89377 100644 --- a/README.rst +++ b/README.rst @@ -153,9 +153,8 @@ event loop. This will take effect even if you're using the yield loop loop.close() -If the ``pytest.mark.asyncio`` marker is applied, a pytest hook will -ensure the produced loop is set as the default global loop. -Fixtures depending on the ``event_loop`` fixture can expect the policy to be properly modified when they run. +If the ``pytest.mark.asyncio`` marker is applied to a test function, the ``event_loop`` +fixture will be requested automatically by the test function. ``unused_tcp_port`` ~~~~~~~~~~~~~~~~~~~ From 08952eed061218dc7f93132f993441123bcf0d27 Mon Sep 17 00:00:00 2001 From: Michael Seifert Date: Sun, 26 Jun 2022 14:21:48 +0200 Subject: [PATCH 3/3] docs: Provide an example for redefining the event_loop fixture rather than for setting a custom event loop. Signed-off-by: Michael Seifert --- README.rst | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index b7a89377..12c76eb0 100644 --- a/README.rst +++ b/README.rst @@ -140,19 +140,21 @@ for treating test functions like coroutines. resp = event_loop.run_until_complete(http_client(url)) assert b"HTTP/1.1 200 OK" in resp -This fixture can be easily overridden in any of the standard pytest locations -(e.g. directly in the test file, or in ``conftest.py``) to use a non-default -event loop. This will take effect even if you're using the -``pytest.mark.asyncio`` marker and not the ``event_loop`` fixture directly. +The ``event_loop`` fixture can be overridden in any of the standard pytest locations, +e.g. directly in the test file, or in ``conftest.py``. This allows redefining the +fixture scope, for example: .. code-block:: python - @pytest.fixture + @pytest.fixture(scope="session") def event_loop(): - loop = MyCustomLoop() + policy = asyncio.get_event_loop_policy() + loop = policy.new_event_loop() yield loop loop.close() +If you need to change the type of the event loop, prefer setting a custom event loop policy over redefining the ``event_loop`` fixture. + If the ``pytest.mark.asyncio`` marker is applied to a test function, the ``event_loop`` fixture will be requested automatically by the test function.