Pytest plugin for testing async python code
Project description
pytest-aio
pytest-aio – Is a simple pytest plugin for testing async python code
The plugin supports all popular python async libraries: (Asyncio, Trio, Curio)
pytest-aio has been strongly influenced by AnyIO. Since 2021-02-09 AnyIO has dropped support for Curio, so the plugin has been created.
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 run all your async test functions/fixtures.
async def test_async():
assert True
By default each test function will be run with asyncio, trio, curio backends consistently (only if trio/curio are available). 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'),
'trio',
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
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'
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
Hashes for pytest_aio-1.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd73bc0f8e25c01fb04b688998cdd717cf2690319745ba270deee53b05bde683 |
|
MD5 | fbc305fecc19d832ef451ed3cfcb8551 |
|
BLAKE2b-256 | f726d520dceb4755e635fdf6f412b6255170964456e11b1598ccb7c628ab5dda |