You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pytest-asyncio can run asynchronous tests with custom event loop factories by defining a ``pytest_asyncio_loop_factories`` hook in ``conftest.py``. The hook returns the factories to use for the current test item:
6
+
7
+
.. code-block:: python
8
+
9
+
import asyncio
10
+
11
+
import pytest
12
+
13
+
14
+
classCustomEventLoop(asyncio.SelectorEventLoop):
15
+
pass
16
+
17
+
18
+
defpytest_asyncio_loop_factories(config, item):
19
+
return [CustomEventLoop]
20
+
21
+
When multiple factories are returned, each asynchronous test is run once per factory. Synchronous tests are not parametrized. The configured loop scope still determines how long each event loop instance is kept alive.
22
+
23
+
Factories should be callables without required parameters and should return an ``asyncio.AbstractEventLoop`` instance. The hook must return a non-empty sequence for every asyncio test.
24
+
25
+
To select different factories for specific tests, you can inspect ``item``:
26
+
27
+
.. code-block:: python
28
+
29
+
import asyncio
30
+
31
+
import uvloop
32
+
33
+
34
+
defpytest_asyncio_loop_factories(config, item):
35
+
if item.get_closest_marker("uvloop"):
36
+
return [uvloop.new_event_loop]
37
+
else:
38
+
return [asyncio.new_event_loop]
39
+
40
+
Factory selection can vary per test item, regardless of loop scope. In other words, with ``module``/``package``/``session`` loop scopes you can still choose different factories for different tests by inspecting ``item``.
41
+
42
+
.. note::
43
+
44
+
When the hook is defined, async tests are parametrized, so factory names are appended to test IDs. For example, a test ``test_example`` with factory ``CustomEventLoop`` will appear as ``test_example[CustomEventLoop]`` in the test output.
Copy file name to clipboardExpand all lines: docs/how-to-guides/uvloop.rst
+23-2Lines changed: 23 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,29 @@
2
2
How to test with uvloop
3
3
=======================
4
4
5
-
Redefining the *event_loop_policy* fixture will parametrize all async tests. The following example causes all async tests to run multiple times, once for each event loop in the fixture parameters:
6
-
Replace the default event loop policy in your *conftest.py:*
5
+
Define a ``pytest_asyncio_loop_factories`` hook in your *conftest.py* that returns ``uvloop.new_event_loop`` as a loop factory:
6
+
7
+
.. code-block:: python
8
+
9
+
import uvloop
10
+
11
+
12
+
defpytest_asyncio_loop_factories(config, item):
13
+
return [uvloop.new_event_loop]
14
+
15
+
.. seealso::
16
+
17
+
:doc:`custom_loop_factory`
18
+
More details on the ``pytest_asyncio_loop_factories`` hook, including per-test factory selection and multiple factory parametrization.
19
+
20
+
Using the event_loop_policy fixture
21
+
------------------------------------
22
+
23
+
.. note::
24
+
25
+
``asyncio.AbstractEventLoopPolicy`` is deprecated as of Python 3.14 (removal planned for 3.16), and ``uvloop.EventLoopPolicy`` will be removed alongside it. Prefer the hook approach above.
26
+
27
+
For older versions of Python and uvloop, you can override the *event_loop_policy* fixture in your *conftest.py:*
0 commit comments