进行大型API响应测试的方式(Fastapi,Pydantic)

发布于 2025-01-31 23:21:16 字数 882 浏览 2 评论 0原文

我在应用程序中使用了Fastapi和Pydantic。

我正在寻找使用Pydantic Basemodel模式进行测试的方法。

如果我像这样编写pydantic basemodel,

class Response(BaseModel):
    str_data1 : str,
    str_date2: str,
    int_data1: int,
    int_data2: int

我想这样测试。

from fastapi import status
from fastapi.testclient import TestClient

from main import app
from schemas.schema import Response
client = TestClient(app)


def test_report():

   response = client.get("/")
   assert response.status_code == status.HTTP_200_OK
   assert response.json() == Response //obviously this line is not working

这样做的方法是硬编码,但是

def test_report():

   response = client.get("/")
   assert response.status_code == status.HTTP_200_OK
   assert type(response.json()["str_data1"]) == str
   ...

如果响应真的很大,我不想这样做,我该如何检查类型和键以响应使用Pydantic Sc​​hemas?

I'm using Fastapi and Pydantic in my app.

I'm looking for way to using Pydantic BaseModel Schemas for testing.

If I write pydantic Basemodel like this,

class Response(BaseModel):
    str_data1 : str,
    str_date2: str,
    int_data1: int,
    int_data2: int

and I want to test like this.

from fastapi import status
from fastapi.testclient import TestClient

from main import app
from schemas.schema import Response
client = TestClient(app)


def test_report():

   response = client.get("/")
   assert response.status_code == status.HTTP_200_OK
   assert response.json() == Response //obviously this line is not working

the way to do this as hard coding but I dont want to do like this

def test_report():

   response = client.get("/")
   assert response.status_code == status.HTTP_200_OK
   assert type(response.json()["str_data1"]) == str
   ...

If Response is really Big, How can I check type and key for response to using pydantic schemas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文