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_user_view()

其实很简单,fastapi里面有个模块,我们直接导入进来,编写用例即可。

运行看下:

可是我们简单的写的,运行后只是没有报错而已。证明执行成功,但是在实际中,我们做还是不够的,我们想要看着是否执行通过。如何实现呢。

我们可以使用python自带的unittest来组织测试用例。

unittest

我们看下改造后的。

复制代码
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"])

# ------------------------改造------------------------------
import unittest
from fastapi.testclient import TestClient

client = TestClient(app)


class FastApiTest(unittest.TestCase):

    def setUp(self) -> None:
        self.client = TestClient(app)

    def tearDown(self) -> None:
        self.client = None

    def test_get_user_view(self):
        response = self.client.get("/u/user?uid=3")
        self.assertEqual(response.status_code, 200)
        self.assertIn("email", response.json())

    def test_get_item_by_uid_view(self):
        response = self.client.get("/i/items/1")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json()[0]["id"], 3)


if __name__ == '__main__':
    unittest.main()

运行看下:

当然,你也可以单独创建一个test文件,将测试流程单独放置于test文件中。

这样我们就实现了对fastapi在开发过程中的接口测试,很简单,我们也不用启动服务端。

相关推荐
Muyuan199817 小时前
28.Paper RAG Agent 开发记录:修复 LLM Rerank 的解析、Fallback 与可验证性
linux·人工智能·windows·python·django·fastapi
观无1 天前
Python读取excel并形成api接口案例
python·pandas·fastapi
绘梨衣5471 天前
Docker+FastAPI+MySQL 项目部署报错汇总
mysql·docker·fastapi
Muyuan19982 天前
27.RAG 系统中的上下文充分性判断:从 Chunk 数量、FAISS 距离到 LLM Relevance Gate
python·django·pdf·fastapi·faiss
曲幽2 天前
FastAPI 少有人提的实用技巧:把 Depends 依赖提到路由层,代码少写60%
python·fastapi·web·routes·depends·prefix·apiroute
.柒宇.2 天前
AI掘金头条项目部署实践指南
linux·运维·python·fastapi
风流 少年2 天前
Python Web框架:FastAPI
前端·python·fastapi
iuu_star2 天前
Vue+FastAPI 项目宝塔Linux部署指南
linux·运维·fastapi
.柒宇.2 天前
AI掘金头条项目 Docker Compose 部署完整教程(附踩坑记录)
运维·后端·python·docker·容器·fastapi
曲幽3 天前
FastAPI 生产环境静态文件完全指南:从 /favicon.ico 404 到 HSTS 混合内容,一次全根治
python·fastapi·web·static·media·404·hsts·favicon·url_for