FastAPI 学习之路(三十九)对开发接口进行测试

概况

对于开发好的接口需要进行测试之后才能发布。当我们在开发的时候,没有提前测试,我们也要对我们自己的接口进行测试,那么FastApi自身也带有针对开发的接口进行测试的功能。我们看下FastApi官方给我们提供了什么样的支持。 

       接口还是基于FastAPI 学习之路(三十七)元数据和文档 URL实现。我们看下如何测试。

TestClient 

from routers.items import item_router
from routers.users import user_router
from fastapi import FastAPI


app = FastAPI(
    title="这是测试Title",
    description="这是测试description",
    version="这是测试version"
)

app.include_router(user_router, prefix="/u", tags=["users"])
app.include_router(item_router, prefix="/i", tags=["items"])

# ------------------------------以下是进行测试的方法---------------------------------
# 这是fastapi提供给我们来进行测试的类
from fastapi.testclient import TestClient

client = TestClient(app)


def test_get_user_view():
    response = client.get("/u/user?uid=1")
    assert response.status_code == 200
    assert isinstance(response.json(), dict) and "email" in response.json()


if __name__ == '__main__':
    test_get_use
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值