You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 19:20:12