pytest-anyio (DEPRECATED: Plugin built into anyio)
This package is obsolete. The pytest plugin for AnyIO is built directly into the `anyio` library itself, meaning `pytest-anyio` does not need to be installed. The PyPI entry for `pytest-anyio` serves primarily as a redirect to this information. AnyIO provides a robust asynchronous networking and concurrency library compatible with asyncio and Trio, with its pytest plugin facilitating asynchronous test writing and fixture management.
Warnings
- breaking The `pytest-anyio` package is deprecated and should NOT be installed. Its functionality was absorbed into the `anyio` library itself.
- gotcha Asynchronous test functions and fixtures are not automatically detected by pytest without proper marking or configuration. They require explicit indications for the AnyIO plugin to run them correctly.
- gotcha Conflicts can arise if `pytest-asyncio` is installed and configured to use `asyncio_mode = 'auto'` simultaneously with `anyio`'s plugin, as both attempt to auto-detect and run async tests.
Install
-
pip install anyio pytest
Imports
- pytest.mark.anyio
from pytest_anyio import anyio_backend
import pytest # ... then use @pytest.mark.anyio on async test functions or pytestmark = pytest.mark.anyio
- anyio_backend (fixture)
def test_my_async_function(anyio_backend): ...
Quickstart
import pytest
import anyio
# Option 1: Mark individual async test functions
@pytest.mark.anyio
async def test_async_function():
await anyio.sleep(0.001)
assert True
# Option 2: Mark all async test functions in a module
pytestmark = pytest.mark.anyio
async def test_another_async_function_module_wide():
await anyio.sleep(0.001)
assert True
# Option 3: Use the anyio_backend fixture (often implicitly, or for custom backend control)
async def test_with_anyio_backend_fixture(anyio_backend):
# 'anyio_backend' can be used to explicitly specify a backend or is implicitly available
# For example, to run only on 'asyncio':
# @pytest.mark.parametrize('anyio_backend', ['asyncio'])
# async def test_on_asyncio_only(anyio_backend):
await anyio.sleep(0.001)
assert True
# Example of an asynchronous fixture
@pytest.fixture
@pytest.mark.anyio
async def async_data():
# Simulate async setup
await anyio.sleep(0.001)
yield {'key': 'value'}
# Simulate async teardown
await anyio.sleep(0.001)
async def test_using_async_fixture(async_data):
assert async_data['key'] == 'value'