| 1 |
================================================ |
| 2 |
How to use custom event loop factories for tests |
| 3 |
================================================ |
| 4 |
|
| 5 |
pytest-asyncio can run asynchronous tests with custom event loop factories by implementing ``pytest_asyncio_loop_factories`` in ``conftest.py``. The hook provides the named event loop factories that are available for a test item by returning a mapping from factory names to loop factory callables: |
| 6 |
|
| 7 |
.. code-block:: python |
| 8 |
|
| 9 |
import asyncio |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
|
| 14 |
class CustomEventLoop(asyncio.SelectorEventLoop): |
| 15 |
pass |
| 16 |
|
| 17 |
|
| 18 |
def pytest_asyncio_loop_factories(config, item): |
| 19 |
return { |
| 20 |
"stdlib": asyncio.new_event_loop, |
| 21 |
"custom": CustomEventLoop, |
| 22 |
} |
| 23 |
|
| 24 |
The hook receives the current pytest ``item``, so it can return different factory mappings for different tests. See :doc:`configure_loop_factories_per_test` for item-based factory configuration. |
| 25 |
|
| 26 |
To run a test with only some configured factories, see :doc:`run_test_with_specific_loop_factories`. |