| 1 |
======================= |
| 2 |
How to test with uvloop |
| 3 |
======================= |
| 4 |
|
| 5 |
Define a ``pytest_asyncio_loop_factories`` hook in your *conftest.py* that maps factory names to loop factories: |
| 6 |
|
| 7 |
.. code-block:: python |
| 8 |
|
| 9 |
import uvloop |
| 10 |
|
| 11 |
|
| 12 |
def pytest_asyncio_loop_factories(config, item): |
| 13 |
return { |
| 14 |
"uvloop": uvloop.new_event_loop, |
| 15 |
} |
| 16 |
|
| 17 |
.. seealso:: |
| 18 |
|
| 19 |
:doc:`custom_loop_factory` |
| 20 |
More details on the ``pytest_asyncio_loop_factories`` hook, including per-test factory selection and multiple factory parametrization. |
| 21 |
|
| 22 |
Using the event_loop_policy fixture |
| 23 |
----------------------------------- |
| 24 |
|
| 25 |
.. note:: |
| 26 |
|
| 27 |
``asyncio.AbstractEventLoopPolicy`` is deprecated as of Python 3.14 (removal planned for 3.16), and ``uvloop.EventLoopPolicy`` will be removed alongside it. Overriding the *event_loop_policy* fixture is also deprecated in pytest-asyncio. Prefer the hook approach above. |
| 28 |
|
| 29 |
For older versions of Python and uvloop, you can override the *event_loop_policy* fixture in your *conftest.py:* |
| 30 |
|
| 31 |
.. code-block:: python |
| 32 |
|
| 33 |
import pytest |
| 34 |
import uvloop |
| 35 |
|
| 36 |
|
| 37 |
@pytest.fixture(scope="session") |
| 38 |
def event_loop_policy(): |
| 39 |
return uvloop.EventLoopPolicy() |
| 40 |
|
| 41 |
You may choose to limit the scope of the fixture to *package,* *module,* or *class,* if you only want a subset of your tests to run with uvloop. |