用于测试异步 python 代码的 Pytest 插件
项目描述
pytest-aio
pytest-aio – 是一个简单的pytest插件,用于测试任何异步 python 代码
特征
<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.sleep、 trio.sleep、curio.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
查看哈希)
内置分布
pytest_aio-1.4.1-py3-none-any.whl
(6.8 kB
查看哈希)
关
pytest_aio -1.4.1-py3-none-any.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 6d1db049c987548547b7a0116559e6232bfffa06600fe4bc011ee8600013f553 |
|
| MD5 | 14a262d7af040241a3f4f2bea69b671f |
|
| 布莱克2-256 | 10baa98729478ae163b393862869bf9addb87be32b28a0ea7c7e5bb1df6bd19d |