Pytest plugin for testing async python code
Project description
pytest-aio
pytest-aio – Is a simple pytest plugin for testing any async python code
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.7
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):
assert 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', {'use_uvloop': False}), id='asyncio'),
pytest.param(('asyncio', {'use_uvloop': True}), 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
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest-aio-1.2.2.tar.gz
.
File metadata
- Download URL: pytest-aio-1.2.2.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85e1ba547d7d2475e0b7f18f5e53afdbddfcf8072baca7d30dad91549e12f458 |
|
MD5 | 1450313d0b8117622344323a16213cee |
|
BLAKE2b-256 | ad3ddef752e02a27c9e6dbe0714b07ef0108b3ab3230e5fd4948e5523d5b4704 |
File details
Details for the file pytest_aio-1.2.2-py3-none-any.whl
.
File metadata
- Download URL: pytest_aio-1.2.2-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e41261662217bfea1e0d8cd898d3c18daae5c5827d2eda1c83781e3de46051bc |
|
MD5 | ee2a05cceacc9ecf0694c33f8db757f0 |
|
BLAKE2b-256 | f38aafc2fc5edc6e1c4744fed061347f5f052fec25417294cd4c6c8d35fee473 |