aiohttp支持的pytest插件
项目描述
aiohttp支持的pytest插件
该库为创建测试aiohttp服务器和客户端提供了有用的配置项。
安装
$ pip install pytest-aiohttp
将 asyncio_mode = auto 行添加到 pytest配置(有关详细信息,请参阅pytest-asyncio模式)。该插件也支持严格模式。
用法
使用提供的配置项以pytest-asyncio风格编写测试,用于创建aiohttp测试服务器和客户端。该插件提供了一键资源清理。
简单用法示例
from aiohttp import web
async def hello(request):
return web.Response(body=b"Hello, world")
def create_app():
app = web.Application()
app.router.add_route("GET", "/", hello)
return app
async def test_hello(aiohttp_client):
client = await aiohttp_client(create_app())
resp = await client.get("/")
assert resp.status == 200
text = await resp.text()
assert "Hello, world" in text
有关 fixtures 使用更详细的信息,请参阅 aiohttp 文档 <https://docs.aiohttp.org/en/stable/testing.html#pytest>。