我有一个 FastAPI 应用程序,其路由前缀为/api/v1.
当我运行测试时它会抛出404. 我发现这是因为TestClient无法找到 处的路线/ping,并且当测试用例中的路线更改为 时,它可以正常工作/api/v1/ping。
有没有一种方法可以避免根据前缀更改所有测试函数中的所有路由?这似乎很麻烦,因为有很多测试用例,而且我不想在测试用例中对路由前缀进行硬编码依赖。TestClient有没有一种方法可以让我像在 中那样配置前缀app,并像在 中提到的那样简单地提及路由routes.py?
路线.py
from fastapi import APIRouter
router = APIRouter()
@router.get("/ping")
async def ping_check():
return {"msg": "pong"}
Run Code Online (Sandbox Code Playgroud)
主要.py
from fastapi import FastAPI
from routes import router
app = FastAPI()
app.include_router(prefix="/api/v1")
Run Code Online (Sandbox Code Playgroud)
在测试文件中我有:
测试.py
from main import app
from fastapi.testclient import TestClient
client = TestClient(app)
def test_ping():
response = client.get("/ping")
assert response.status_code == 200
assert response.json() == {"msg": …Run Code Online (Sandbox Code Playgroud)