测试ASGI网络应用的异步客户端
项目描述
async-asgi-testclient
async-asgi-testclient是一个库,用于测试实现ASGI规范(版本2和3)的网络应用。
这个项目的动机是构建一个不依赖于网络框架(Quart,Startlette等)的通用测试库。
它通过直接调用ASGI应用来工作。这避免了需要在不同的进程/线程/asyncio循环中使用http服务器运行应用的需求。由于应用和测试运行在同一个asyncio循环中,因此编写测试和调试代码更容易。
这个库基于Quart提供的测试模块。
快速入门
要求:Python 3.6+
安装
pip install async-asgi-testclient
使用
my_api.py
:
from quart import Quart, jsonify
app = Quart(__name__)
@app.route("/")
async def root():
return "plain response"
@app.route("/json")
async def json():
return jsonify({"hello": "world"})
if __name__ == '__main__':
app.run()
test_app.py
:
from async_asgi_testclient import TestClient
import pytest
@pytest.mark.asyncio
async def test_quart_app():
from .my_api import app
async with TestClient(app) as client:
resp = await client.get("/")
assert resp.status_code == 200
assert resp.text == "plain response"
resp = await client.get("/json")
assert resp.status_code == 200
assert resp.json() == {"hello": "world"}
支持
- cookies
- multipart/form-data
- 跟随重定向
- 响应流
- 请求流
- WebSocket支持
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源代码分发
async-asgi-testclient-1.4.11.tar.gz (11.7 kB 查看哈希值)
关闭
async-asgi-testclient-1.4.11.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 4449ac85d512d661998ec61f91c9ae01851639611d748d81ae7f816736551792 |
|
MD5 | 4896444d7d6aeb7164c4befbbebd8d45 |
|
BLAKE2b-256 | 529a0eb3fd37d4f9ad1e9b2b6d6b91357d3ebf7534271c32e343185a5d204903 |