Skip to main content

Pytest plugin for testing async python code

Project description

pytest-aio

pytest-aio – Is a simple pytest plugin for testing any async python code

Tests Status PYPI Version Python Versions

Features

  • Supports all most popular async python libraries: Asyncio, Trio and Curio

  • Automatically run your async tests

  • Works with contextvars correctly (supports it for async/sync fixtures)

  • Supports trio-asyncio

Requirements

  • python >= 3.10

Installation

pytest-aio should be installed using pip:

pip install pytest-aio

optionally extras are available:

pip install pytest-aio[curio,trio]

Usage

When installed the plugin runs all your async test functions/fixtures.

async def test_async():
    assert True

No need to mark your async tests. Just run pytest as is.

Async fixtures for sync tests

If you plan use async fixtures for sync tests, please ensure you have to include aiolib fixture:

# It's important to add aiolib fixture here
def test_with_async_fixtures(async_fixture, aiolib):
    assert async_fixture == 'value from async fixture'

As an alternative, If you are doing the async fixtures yourself, you can add aiolib inside them:

@pytest.fixture
async def async_fixture(aiolib):
    return 'value from async fixture'

# So for the test we don't need to implicity use `aiolib` anymore
def test_with_async_fixtures(async_fixture):
    assert async_fixture == 'value from async fixture'

Customize async libraries

By default each test function will be run with asyncio, trio, curio backends consistently (only if trio/curio are installed). But you can customise the libraries for all your tests creating the global fixture:

# Run all tests with Asyncio/Trio only
@pytest.fixture(params=['asyncio', 'trio'])
def aiolib(request):
    return request.param

If you want to specify different options for the selected backend, you can do so by passing a tuple of (backend name, options dict):

@pytest.fixture(params=[
    pytest.param(('asyncio'), id='asyncio'),
    pytest.param(('asyncio', {'loop_factory': uvloop.new_event_loop}), id='asyncio+uvloop'),
    pytest.param(('trio', {'trio_asyncio': True}), id='trio+asyncio'),
    pytest.param(('curio', {'debug': True}), id='curio'),
])
def aiolib(request):
    assert request.param

To set a specific backends for a single test only:

@pytest.mark.parametrize('aiolib', ['asyncio'])
async def only_with_asyncio():
    await asyncio.sleep(1)
    assert True

Helpers

The plugin contains aiosleep fixture. It’s an equivalent to asyncio.sleep, trio.sleep, curio.sleep for current running library.

Bug tracker

If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/asgi-tools/issues

Contributing

Development of the project happens at: https://github.com/klen/pytest-aio

License

Licensed under a MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_aio-2.1.7.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_aio-2.1.7-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest_aio-2.1.7.tar.gz.

File metadata

  • Download URL: pytest_aio-2.1.7.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pytest_aio-2.1.7.tar.gz
Algorithm Hash digest
SHA256 c9c2fa05787a48cc6abf90f57d2333dc4fe356a1b49db7bcb52ec32ada8bb684
MD5 a2de7d03531f34ebb699b264f6f15102
BLAKE2b-256 ba389aab892a064592dde69b68554550a7f773d9120d5cbd77c9c033a91bba71

See more details on using hashes here.

File details

Details for the file pytest_aio-2.1.7-py3-none-any.whl.

File metadata

  • Download URL: pytest_aio-2.1.7-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pytest_aio-2.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 72eca30834d254d3f36336295a19119b42900c84bdd8155a0c242467987d0e73
MD5 07b8d69c9de7b9cb767cd0b7c0266695
BLAKE2b-256 8a39a9a065eaf0336041225443e79ecbaa27146c241514782990480a70df0ac0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page