简体   繁体   English

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

[英]How to test that a model was used in a FastAPI route?

I'm trying to check if a specific model was used as an input parser for a FastAPI route.我正在尝试检查特定的 model 是否用作 FastAPI 路由的输入解析器。 However, I'm not sure how to patch (or spy on) it.但是,我不确定如何修补(或监视)它。

I have the following file structure:我有以下文件结构:

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

main.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: 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

However, patched_model is never called (other asserts pass).但是,从不调用patched_model (其他断言通过)。 I don't want to change the functionality or replace ItemModel in main.py, I just want to check if it was used.我不想更改功能或替换ItemModel中的 ItemModel,我只想检查它是否被使用。

My first approach to this was to wrap the read_main method and check that the item passed into the function is indeed an instance of ItemModel .我的第一个方法是包装read_main方法并检查传递给 function 的item确实ItemModel的一个实例。 But that was a dead-end approach because of the way FastAPI endpoints are prepared and stored: FastAPI stores a copy of the endpoint function objects in a list: (see fastapi/routing.py ), then evaluates at request-time which endpoint to call.但这是一个死胡同,因为 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?

My second approach involves spying or "breaking" the initialization of ItemModel , such that if the endpoint does indeed use that model, then a "broken" ItemModel would cause a request that hits that endpoint to fail.我的第二种方法涉及监视或“破坏” ItemModel的初始化,这样如果端点确实使用了该 model,那么“损坏的” ItemModel将导致到达该端点的请求失败。 We "break" ItemModel by making use of the fact that (1) FastAPI calls the __init__ of your model during the request-response cycle, and (2) a 422 error response is propagated by default when the endpoint is unable to serialize a model properly:我们利用以下事实“破坏” 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)

So in tests, just mock the __init__ method:所以在测试中,只需模拟__init__方法:

  • Example for pytest 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)
  • Example for pytest + pytest-mock 's mocker.spy 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'})
  • Example for unittest单元测试示例
    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'})

Again, the tests check that the endpoint fails in either serializing into an ItemModel or in accessing item.name , which will only happen if the endpoint is indeed using ItemModel .同样,测试检查端点是否在序列化为ItemModel或访问item.name时失败,这只会在端点确实使用ItemModel

If you modify the endpoint from item: ItemModel into item: OtherModel :如果您将端点从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}"}

then running the tests should now fail because the endpoint is now creating the wrong object:那么现在运行测试应该会失败,因为端点现在正在创建错误的 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

The assertion errors for 422 == 200 is a bit confusing, but it basically means that even though we "broke" ItemModel , we still got a 200/OK response.. which means ItemModel is not being used. 422 == 200 的断言错误有点令人困惑,但这基本上意味着即使我们“破坏”了ItemModel ,我们仍然得到 200/OK 响应......这意味着ItemModel没有被使用。

Likewise, if you modified the tests first and mocked-out the __init__ of OtherModel instead of ItemModel , then running the tests without modifying the endpoint will result in similar failing tests:同样,如果您先修改测试并模拟出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.

The assertion here is less confusing because it says we expected that the endpoint will call OtherModel 's __init__ , but it wasn't called.这里的断言不那么令人困惑,因为它说我们期望端点会调用OtherModel__init__ ,但它没有被调用。 It should pass after modifying the endpoint to use item: OtherModel .它应该在修改端点以使用item: OtherModel后通过。

One last thing to note is that since we are manipulating the __init__ , then it can cause the "happy path" to fail, so it should now be tested separately.最后要注意的一件事是,由于我们正在操纵__init__ ,因此它可能导致“快乐路径”失败,因此现在应该单独对其进行测试。 Make sure to undo/revert the mocks and patches:确保撤消/还原模拟和补丁:

  • Example for pytest 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"}
  • Example for pytest + pytest-mock 's mocker.spy 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"}
  • Example for unittest单元测试示例
    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"}

All in all, you might want to consider if/why it's useful to check for which model is exactly used.总而言之,您可能需要考虑是否/为什么检查确切使用的 model 是否有用。 Normally, I just check that passing-in valid request params returns the expected valid response, and likewise, that invalid requests returns an error response.通常,我只检查传入的有效请求参数是否返回预期的有效响应,同样,无效请求是否返回错误响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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