Fixtures

event_loop_policy

Returns the event loop policy used to create asyncio event loops. The default return value is asyncio.get_event_loop_policy().

This fixture can be overridden when a different event loop policy should be used.

import asyncio
import warnings

with warnings.catch_warnings():
    warnings.simplefilter("ignore", DeprecationWarning)
    from asyncio import DefaultEventLoopPolicy

import pytest


class CustomEventLoopPolicy(DefaultEventLoopPolicy):
    pass


@pytest.fixture(scope="module")
def event_loop_policy(request):
    return CustomEventLoopPolicy()


@pytest.mark.asyncio(loop_scope="module")
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
async def test_uses_custom_event_loop_policy():
    assert isinstance(asyncio.get_event_loop_policy(), CustomEventLoopPolicy)

Multiple policies can be provided via fixture parameters. The fixture is automatically applied to all pytest-asyncio tests. Therefore, all tests managed by pytest-asyncio are run once for each fixture parameter. The following example runs the test with different event loop policies.

import asyncio
import warnings

with warnings.catch_warnings():
    warnings.simplefilter("ignore", DeprecationWarning)
    from asyncio import DefaultEventLoopPolicy

import pytest


class CustomEventLoopPolicy(DefaultEventLoopPolicy):
    pass


@pytest.fixture(
    params=(
        DefaultEventLoopPolicy(),
        CustomEventLoopPolicy(),
    ),
)
def event_loop_policy(request):
    return request.param


@pytest.mark.asyncio
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
async def test_uses_custom_event_loop_policy():
    assert isinstance(asyncio.get_event_loop_policy(), DefaultEventLoopPolicy)

unused_tcp_port

Finds and yields a single unused TCP port on the localhost interface. Useful for binding temporary test servers.

unused_tcp_port_factory

A callable which returns a different unused TCP port each invocation. Useful when several unused TCP ports are required in a test.

def a_test(unused_tcp_port_factory):
    _port1, _port2 = unused_tcp_port_factory(), unused_tcp_port_factory()

unused_udp_port and unused_udp_port_factory

Works just like their TCP counterparts but returns unused UDP ports.