Skip to content

Commit

Permalink
[docs] Explain that scopes of event_loop fixtures should not overlap.
Browse files Browse the repository at this point in the history
The example for overriding event loop scopes now uses a module scope rather than a session scope. Due to the constraint that event_loop fixture scopes should not overlap, a session-scoped fixture is usually not that helpful.

Addresses #531

Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de>
  • Loading branch information
seifertm committed Jul 12, 2023
1 parent 5890189 commit 1821542
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion docs/source/reference/fixtures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ fixture scope, for example:

.. code-block:: python
@pytest.fixture(scope="session")
@pytest.fixture(scope="module")
def event_loop():
policy = asyncio.get_event_loop_policy()
loop = policy.new_event_loop()
yield loop
loop.close()
When defining multiple ``event_loop`` fixtures, you should ensure that their scopes don't overlap.
Each of the fixtures replace the running event loop, potentially without proper clean up.
This will emit a warning and likely lead to errors in your tests suite.
You can manually check for overlapping ``event_loop`` fixtures by running pytest with the ``--setup-show`` option.

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`` decorator is applied to a test function, the ``event_loop``
Expand Down

0 comments on commit 1821542

Please sign in to comment.