Skip to content

Commit 25c54a5

Browse files
authored
Clarify documentation of event_loop fixture (#375)
* docs: Removed contradicting information about the event_loop fixture not setting the global event loop. Signed-off-by: Michael Seifert <[email protected]> * docs: Clarified the effect of pytest.mark.asyncio with regards to the event_loop fixture. Signed-off-by: Michael Seifert <[email protected]> * docs: Provide an example for redefining the event_loop fixture rather than for setting a custom event loop. Signed-off-by: Michael Seifert <[email protected]>
1 parent 49f07a4 commit 25c54a5

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

README.rst

+14-16
Original file line numberDiff line numberDiff line change
@@ -123,42 +123,40 @@ Fixtures
123123

124124
``event_loop``
125125
~~~~~~~~~~~~~~
126-
Creates and injects a new instance of the default asyncio event loop. By
127-
default, the loop will be closed at the end of the test (i.e. the default
128-
fixture scope is ``function``).
126+
Creates a new asyncio event loop based on the current event loop policy. The new loop
127+
is available as the return value of this fixture or via `asyncio.get_running_loop <https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_running_loop>`__.
128+
The event loop is closed when the fixture scope ends. The fixture scope defaults
129+
to ``function`` scope.
129130

130131
Note that just using the ``event_loop`` fixture won't make your test function
131132
a coroutine. You'll need to interact with the event loop directly, using methods
132133
like ``event_loop.run_until_complete``. See the ``pytest.mark.asyncio`` marker
133134
for treating test functions like coroutines.
134135

135-
Simply using this fixture will not set the generated event loop as the
136-
default asyncio event loop, or change the asyncio event loop policy in any way.
137-
Use ``pytest.mark.asyncio`` for this purpose.
138-
139136
.. code-block:: python
140137
141138
def test_http_client(event_loop):
142139
url = "http://httpbin.org/get"
143140
resp = event_loop.run_until_complete(http_client(url))
144141
assert b"HTTP/1.1 200 OK" in resp
145142
146-
This fixture can be easily overridden in any of the standard pytest locations
147-
(e.g. directly in the test file, or in ``conftest.py``) to use a non-default
148-
event loop. This will take effect even if you're using the
149-
``pytest.mark.asyncio`` marker and not the ``event_loop`` fixture directly.
143+
The ``event_loop`` fixture can be overridden in any of the standard pytest locations,
144+
e.g. directly in the test file, or in ``conftest.py``. This allows redefining the
145+
fixture scope, for example:
150146

151147
.. code-block:: python
152148
153-
@pytest.fixture
149+
@pytest.fixture(scope="session")
154150
def event_loop():
155-
loop = MyCustomLoop()
151+
policy = asyncio.get_event_loop_policy()
152+
loop = policy.new_event_loop()
156153
yield loop
157154
loop.close()
158155
159-
If the ``pytest.mark.asyncio`` marker is applied, a pytest hook will
160-
ensure the produced loop is set as the default global loop.
161-
Fixtures depending on the ``event_loop`` fixture can expect the policy to be properly modified when they run.
156+
If you need to change the type of the event loop, prefer setting a custom event loop policy over redefining the ``event_loop`` fixture.
157+
158+
If the ``pytest.mark.asyncio`` marker is applied to a test function, the ``event_loop``
159+
fixture will be requested automatically by the test function.
162160

163161
``unused_tcp_port``
164162
~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)