如何测试模型是否在 FastAPI 路由中使用?

时间:2021-01-24 22:08:19

标签: python unit-testing pytest python-unittest fastapi

我正在尝试检查是否将特定模型用作 FastAPI 路由的输入解析器。但是,我不确定如何修补(或监视)它。

我有以下文件结构:

.
└── roo
    ├── __init__.py
    ├── main.py
    └── test_demo.py

main.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 永远不会被调用(其他断言通过)。我不想更改功能或替换 main.py 中的 ItemModel,我只想检查它是否被使用过。

1 个答案:

答案 0 :(得分:1)

我对此的第一个方法是包装 col like 'ab####' 方法并检查传递给函数的 read_main 是否确实item 的实例。但这是一种死胡同的方法,因为 FastAPI 端点的准备和存储方式:FastAPI 将端点函数对象的副本存储在列表中:(参见 fastapi/routing.py),然后在请求时评估哪个端点要打电话。

ItemModel

我的第二种方法涉及监视或“破坏”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 将导致命中该端点的请求失败。我们通过利用以下事实来“破坏”ItemModel:(1) FastAPI 在请求-响应周期期间调用您模型的 ItemModel,以及 (2) 默认情况下传播 422 错误响应端点无法正确序列化模型:

__init__

因此在测试中,只需模拟 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) 方法:

  • pytest 示例
    __init__
  • pytest + pytest-mockmocker.spy 示例
    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)
    
  • 单元测试示例
    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 时失败,这只会在端点确实使用 {{ 1}}。

如果您将端点从 item.name 修改为 ItemModel

item: ItemModel

然后运行测试现在应该失败,因为端点现在正在创建错误的对象:

item: OtherModel

422 == 200 的断言错误有点令人困惑,但这基本上意味着即使我们“破坏”了 class OtherModel(BaseModel): name: str class ItemModel(BaseModel): name: str @app.post("/") async def read_main(item: OtherModel): # <---- return {"msg": f"Item: {item.name}"} ,我们仍然得到 200/OK 响应..这意味着 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 没有被使用。

同样,如果您先修改测试并模拟出 ItemModelItemModel 而不是 __init__,那么在不修改端点的情况下运行测试将导致类似的失败测试:

OtherModel

这里的断言不那么令人困惑,因为它说我们预计端点将调用 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__,因此它可能会导致“快乐路径”失败,因此现在应该对其进行单独测试。确保撤消/恢复模拟和补丁:

  • pytest 示例
    item: OtherModel
  • pytest + pytest-mockmocker.spy 示例
    __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"}
    

总而言之,您可能需要考虑检查确切使用的模型是否/为什么有用。通常,我只是检查传入的有效请求参数是否返回预期的有效响应,同样,无效请求返回错误响应。

相关问题