小编Joh*_*ohn的帖子

如何使用 MongoDB(Motor) 实现 FastAPI 的 pytest

我想为我的 FastAPI 端点编写测试

我的代码示例:

from fastapi import FastAPI
from fastapi.testclient import TestClient

app = FastAPI()

@app.get("/todos")
async def get_todo_by_title(title: str,current_user: User = Depends(get_current_user))
    document = await collection.find_one({"title": title})
    return document

client = TestClient(app)

def test_get_todo_by_title():
    response = client.get("/todos")
    assert response.status_code == 200
Run Code Online (Sandbox Code Playgroud)

测试我的端点的最佳方法是什么?

我想使用假数据库进行测试,例如 json 文件

db = {
todos: [...]
}
Run Code Online (Sandbox Code Playgroud)

unit-testing mongodb pytest mongomock fastapi

5
推荐指数
1
解决办法
6313
查看次数

标签 统计

fastapi ×1

mongodb ×1

mongomock ×1

pytest ×1

unit-testing ×1