【发布时间】:2020-10-21 08:03:43
【问题描述】:
我有一个用 Uvicorn+FastAPI 编写的 REST-API 应用程序
我想使用 PyTest 进行测试。
我想在开始测试时在夹具中启动服务器,所以当测试完成时,夹具将终止应用程序。
FastAPI Testing 展示了如何测试 API 应用程序,
from fastapi import FastAPI
from starlette.testclient import TestClient
app = FastAPI()
@app.get("/")
async def read_main():
return {"msg": "Hello World"}
client = TestClient(app)
def test_read_main():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"msg": "Hello World"}
这不会以通常的方式使服务器联机。似乎由 client.get 命令触发的特定功能是唯一运行的东西。
我找到了这些额外的资源,但我无法让它们为我工作:
https://medium.com/@hmajid2301/pytest-with-background-thread-fixtures-f0dc34ee3c46
How to run server as fixture for py.test
您将如何从 PyTest 运行 Uvicorn+FastAPI 应用程序,以便它随着测试上升和下降?
【问题讨论】:
-
这是一个与这个问题stackoverflow.com/q/61577643/4165272和这个问题stackoverflow.com/q/68603658/4165272相关的问题
标签: python testing pytest fastapi uvicorn