繁体   English   中英

如何测试在 FastAPI 路由中使用了 model?

[英]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__方法:

  • pytest 示例
    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)
  • pytest + pytest-mockmocker.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__ ,因此它可能导致“快乐路径”失败,因此现在应该单独对其进行测试。 确保撤消/还原模拟和补丁:

  • pytest 示例
    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"}
  • pytest + pytest-mockmocker.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM