进行大型API响应测试的方式(Fastapi,Pydantic)
我在应用程序中使用了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 Schemas?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论