How to test with different event loops
Parametrizing the event_loop_policy fixture parametrizes all async tests. The following example causes all async tests to run multiple times, once for each event loop in the fixture parameters:
import asyncio
from asyncio import DefaultEventLoopPolicy
import pytest
class CustomEventLoopPolicy(DefaultEventLoopPolicy):
pass
@pytest.fixture(
scope="session",
params=(
CustomEventLoopPolicy(),
CustomEventLoopPolicy(),
),
)
def event_loop_policy(request):
return request.param
@pytest.mark.asyncio
async def test_uses_custom_event_loop_policy():
assert isinstance(asyncio.get_event_loop_policy(), CustomEventLoopPolicy)
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 different event loops.