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

标签 python unit-testing pytest python-unittest fastapi

我正在尝试检查特定模型是否用作 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 从未被调用(其他断言通过)。我不想更改功能或替换 main.py 中的 ItemModel,我只是想检查它是否被使用。

最佳答案

我的第一个方法是包装 read_main 方法并检查传递给函数的 item 确实项目模型。但由于 FastAPI 端点的准备和存储方式,这是一种死胡同的方法:FastAPI 将端点函数对象的副本存储在列表中:(请参阅 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 的初始化,这样如果端点确实确实使用该模型,那么“破坏” ItemModel 会导致命中该端点的请求失败。我们通过利用以下事实来“破坏”ItemModel:(1) FastAPI 在请求-响应周期中调用模型的 __init__,以及 (2) 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-mock 示例的 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}"}

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

    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-mock 示例的 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"}
    

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

关于python - 如何测试模型是否在 FastAPI 路由中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65876503/

相关文章:

python - 错误 : expected declaration specifiers or '...' before string constant

python - 如何展平嵌套模型? (keras 函数式 API)

python - 使 ModelForm 与 Django 中的中间模型的多对多关系工作的步骤是什么?

angularjs - 在指令测试中模拟所需的 Controller

python-3.x - 当我将测试放在单独的目录中时,pytest 找不到包

python - 无法在我扭曲的网络资源中测试 reactor.callInThread

python - CqlEngine - sync_table() KeyError : 'cqlengine'

c# - 使用 FxCop 获得百分比代码覆盖率

java - 出于单元测试的目的,哪个内存中 Java 数据库最接近 MySQL 和 SqlServer?

python - Pytest:将 xfail 与 raise 一起使用,测试尽管意外异常仍保持 xfailiing