Skip to main content

用于测试异步 python 代码的 Pytest 插件

项目描述

pytest-aio

pytest-aio – 是一个简单的pytest插件,用于测试任何异步 python 代码

测试状态 PYPI 版本 Python 版本

特征

  • 支持所有最流行的异步 Python 库:AsyncioTrioCurio

  • 自动运行你的异步测试

  • 正确使用contextvars(支持异步/同步设备)

  • 支持三异步

<nav class="contents" id="topic-1">

内容

</nav>

要求

  • 蟒蛇> = 3.7

安装

pytest-aio应该使用 pip 安装:

pip install pytest-aio

可选的附加功能:

pip install pytest-aio[curio,trio]

用法

安装后,插件会运行您所有的异步测试功能/装置。

async def test_async():
    assert True

无需标记您的异步测试。只需按原样运行 pytest 即可。

用于同步测试的异步夹具

如果您计划使用异步夹具进行同步测试,请确保您必须包含aiolib夹具:

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

作为替代方案,如果您自己做异步设备,您可以在其中添加 aiolib

@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'

自定义异步库

默认情况下,每个测试函数都将与 asyncio、trio、curio 后端一致地运行(仅当安装了 trio/curio 时)。但是您可以为创建全局夹具的所有测试自定义库:

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

如果你想为选定的后端指定不同的选项,你可以通过传递(后端名称,选项字典)的元组来做到这一点:

@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

仅为单个测试设置特定后端:

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

帮手

该插件包含aiosleep固定装置。对于当前正在运行的库,它等效于asyncio.sleeptrio.sleepcurio.sleep 。

错误跟踪器

如果您有任何建议、错误报告或烦恼,请将它们报告给https://github.com/klen/asgi-tools/issues的问题跟踪器

贡献

该项目的开发发生在:https ://github.com/klen/pytest-aio

执照

根据MIT 许可证获得许可

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

pytest-aio-1.4.1.tar.gz (6.8 kB 查看哈希)

已上传 source

内置分布

pytest_aio-1.4.1-py3-none-any.whl (6.8 kB 查看哈希

已上传 py3