How to use custom event loop factories for tests
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:
import asyncio
import pytest
class CustomEventLoop(asyncio.SelectorEventLoop):
pass
def pytest_asyncio_loop_factories(config, item):
return {
"stdlib": asyncio.new_event_loop,
"custom": CustomEventLoop,
}
The hook receives the current pytest item, so it can return different factory mappings for different tests. See How to configure event loop factories from the test item for item-based factory configuration.
To run a test with only some configured factories, see How to run a test with specific event loop factories only.