[英]How to test that a model was used in a FastAPI route?
我正在嘗試檢查特定的 model 是否用作 FastAPI 路由的輸入解析器。 但是,我不確定如何修補(或監視)它。
我有以下文件結構:
.
└── roo
├── __init__.py
├── main.py
└── test_demo.py
主要.py:
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class ItemModel(BaseModel):
name: str
@app.post("/")
async def read_main(item: ItemModel):
return {"msg": f"Item: {item.name}"}
test_demo.py:
from fastapi.testclient import TestClient
from unittest.mock import patch
from roo.main import app, ItemModel
client = TestClient(app)
def test_can_creating_new_item_users_proper_validation_model():
with patch('roo.main.ItemModel', wraps=ItemModel) as patched_model:
response = client.post("/", json={'name': 'good'})
assert response.status_code == 200
assert response.json() == {"msg": "Item: good"}
assert patched_model.called
但是,從不調用patched_model
(其他斷言通過)。 我不想更改功能或替換ItemModel
中的 ItemModel,我只想檢查它是否被使用。
我的第一個方法是包裝read_main
方法並檢查傳遞給 function 的item
確實是ItemModel
的一個實例。 但這是一個死胡同,因為 FastAPI 端點的准備和存儲方式:FastAPI 將端點 function 對象的副本存儲在一個列表中:(參見fastapi/routing.py ),然后在請求時評估哪個端點稱呼。
from roo.main import app
def test_read_main():
assert 'read_main' in [r.endpoint.__name__ for r in app.routes]
# check that read_main was called *and* received an ItemModel instance?
我的第二種方法涉及監視或“破壞” ItemModel
的初始化,這樣如果端點確實使用了該 model,那么“損壞的” ItemModel
將導致到達該端點的請求失敗。 我們利用以下事實“破壞” ItemModel
:(1)FastAPI 在請求-響應周期中調用 model 的__init__
,以及(2)當端點無法序列化 model 時,默認傳播 422 錯誤響應適當地:
class ItemModel(BaseModel):
name: str
def __init__(__pydantic_self__, **data: Any) -> None:
print("Make a POST request and confirm that this is printed out")
super().__init__(**data)
所以在測試中,只需模擬__init__
方法:
import pytest from fastapi.testclient import TestClient from roo.main import app, ItemModel def test_read_main(monkeypatch: pytest.MonkeyPatch): client = TestClient(app) def broken_init(self, **data): pass # `name` and other fields won't be set monkeypatch.setattr(ItemModel, '__init__', broken_init) with pytest.raises(AttributeError) as exc: client.post("/", json={'name': 'good'}) assert 422 == response.status_code assert "'ItemModel' object has no attribute" in str(exc.value)
mocker.spy
from fastapi.testclient import TestClient from pytest_mock import MockerFixture from roo.main import app, ItemModel def test_read_main(mocker: MockerFixture): client = TestClient(app) spy = mocker.spy(ItemModel, '__init__') client.post("/", json={'name': 'good'}) spy.assert_called() spy.assert_called_with(**{'name': 'good'})
from fastapi.testclient import TestClient from roo.main import app, ItemModel from unittest.mock import patch def test_read_main(): client = TestClient(app) # Wrapping __init__ like this isn't really correct, but serves the purpose with patch.object(ItemModel, '__init__', wraps=ItemModel.__init__) as mocked_init: response = client.post("/", json={'name': 'good'}) assert 422 == response.status_code mocked_init.assert_called() mocked_init.assert_called_with(**{'name': 'good'})
同樣,測試檢查端點是否在序列化為ItemModel
或訪問item.name
時失敗,這只會在端點確實使用ItemModel
。
如果您將端點從item: ItemModel
修改為item: OtherModel
:
class OtherModel(BaseModel):
name: str
class ItemModel(BaseModel):
name: str
@app.post("/")
async def read_main(item: OtherModel): # <----
return {"msg": f"Item: {item.name}"}
那么現在運行測試應該會失敗,因為端點現在正在創建錯誤的 object:
def test_read_main(mocker: MockerFixture):
client = TestClient(app)
spy = mocker.spy(ItemModel, '__init__')
client.post("/", json={'name': 'good'})
> spy.assert_called()
E AssertionError: Expected '__init__' to have been called.
test_demo_spy.py:11: AssertionError
with pytest.raises(AttributeError) as exc:
response = client.post("/", json={'name': 'good'})
> assert 422 == response.status_code
E assert 422 == 200
E +422
E -200
test_demo_pytest.py:15: AssertionError
422 == 200 的斷言錯誤有點令人困惑,但這基本上意味着即使我們“破壞”了ItemModel
,我們仍然得到 200/OK 響應......這意味着ItemModel
沒有被使用。
同樣,如果您先修改測試並模擬出OtherModel
的__init__
而不是ItemModel
,那么在不修改端點的情況下運行測試將導致類似的失敗測試:
def test_read_main(mocker: MockerFixture):
client = TestClient(app)
spy = mocker.spy(OtherModel, '__init__')
client.post("/", json={'name': 'good'})
> spy.assert_called()
E AssertionError: Expected '__init__' to have been called.
def test_read_main():
client = TestClient(app)
with patch.object(OtherModel, '__init__', wraps=OtherModel.__init__) as mocked_init:
response = client.post("/", json={'name': 'good'})
# assert 422 == response.status_code
> mocked_init.assert_called()
E AssertionError: Expected '__init__' to have been called.
這里的斷言不那么令人困惑,因為它說我們期望端點會調用OtherModel
的__init__
,但它沒有被調用。 它應該在修改端點以使用item: OtherModel
后通過。
最后要注意的一件事是,由於我們正在操縱__init__
,因此它可能導致“快樂路徑”失敗,因此現在應該單獨對其進行測試。 確保撤消/還原模擬和補丁:
def test_read_main(monkeypatch: pytest.MonkeyPatch): client = TestClient(app) def broken_init(self, **data): pass # Are we really using ItemModel? monkeypatch.setattr(ItemModel, '__init__', broken_init) with pytest.raises(AttributeError) as exc: response = client.post("/", json={'name': 'good'}) assert 422 == response.status_code assert "'ItemModel' object has no attribute" in str(exc.value) # Okay, really using ItemModel. Does it work correctly? monkeypatch.undo() response = client.post("/", json={'name': 'good'}) assert response.status_code == 200 assert response.json() == {"msg": "Item: good"}
mocker.spy
from pytest_mock import MockerFixture from fastapi.testclient import TestClient from roo.main import app, ItemModel def test_read_main(mocker: MockerFixture): client = TestClient(app) # Are we really using ItemModel? spy = mocker.spy(ItemModel, '__init__') client.post("/", json={'name': 'good'}) spy.assert_called() spy.assert_called_with(**{'name': 'good'}) # Okay, really using ItemModel. Does it work correctly? mocker.stopall() response = client.post("/", json={'name': 'good'}) assert response.status_code == 200 assert response.json() == {"msg": "Item: good"}
def test_read_main(): client = TestClient(app) # Are we really using ItemModel? with patch.object(ItemModel, '__init__', wraps=ItemModel.__init__) as mocked_init: response = client.post("/", json={'name': 'good'}) assert 422 == response.status_code mocked_init.assert_called() mocked_init.assert_called_with(**{'name': 'good'}) # Okay, really using ItemModel. Does it work correctly? response = client.post("/", json={'name': 'good'}) assert response.status_code == 200 assert response.json() == {"msg": "Item: good"}
總而言之,您可能需要考慮是否/為什么檢查確切使用的 model 是否有用。 通常,我只檢查傳入的有效請求參數是否返回預期的有效響應,同樣,無效請求是否返回錯誤響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.