测试asgi web应用程序的异步客户端
async-asgi-testclient的Python项目详细描述
异步asgi测试客户端
async asgi testclient是一个用于测试实现asgi规范(版本2和3)的web应用程序的库。
这个项目背后的动机是构建一个不依赖于web框架(Quart,Startlette,…)的通用测试库,就像人们使用不依赖于框架的uvicorn或hypercorn等asgi服务器一样。
这个库基于Quart中提供的测试模块。
快速启动
要求:python 3.6+
安装:
pip install async-asgi-testclient
用法
my_api.py
:
fromquartimportQuart,jsonifyapp=Quart(__name__)@app.route("/")asyncdefroot():return"plain response"@app.route("/json")asyncdefjson():returnjsonify({"hello":"world"})if__name__=='__main__':app.run()
test_app.py
:
fromasync_asgi_testclientimportTestClientimportpytest@pytest.mark.asyncioasyncdeftest_quart_app():from.my_apiimportappasyncwithTestClient(app)asclient:resp=awaitclient.get("/")assertresp.status_code==200assertresp.text=="plain response"resp=awaitclient.get("/json")assertresp.status_code==200assertresp.json()=={"hello":"world"}
支持
- [X]饼干
- [X]遵循重定向
- [X]响应流
- [X]请求流
- [X]WebSocket支持