FastAPI升级后pytest测试出现无法打印的ValidationError
FastAPI升级后pytest测试抛出不可打印的ValidationError
问题情况
将FastAPI从0.79.0升级到0.92.0后,执行测试命令python -m pytest app时,出现错误:pydantic.error_wrappers.ValidationError: <unprintable ValidationError object>
测试代码
from fastapi.testclient import TestClient from main import app def test_health_check(): """ Test the GET health check """ client = TestClient(app) response = client.get("/") assert response.status_code == 200
使用的依赖版本
- fastapi==0.92.0
- pydantic==1.10.11
关键补充
当控制器仅返回{"message": "Welcome"}时测试能正常通过,说明错误源于当前的返回结构。控制器代码如下:
@app.get("/") async def index() -> dict[str, str]: return { "message": "Welcome", "API": {"version": __version__, "updated": updated}, "Common package": {"version": apicommon.__version__, "updated": apicommon.updated}, }
内容的提问来源于stack exchange,提问作者Henrique Andrade
相关产品推荐
相关产品推荐

