Python 如何测试FastAPI路由中是否使用了模型?
我试图检查是否有一个特定的模型被用作FastAPI路由的输入解析器。然而,我不知道如何修补(或监视)它 我有以下文件结构:Python 如何测试FastAPI路由中是否使用了模型?,python,unit-testing,pytest,python-unittest,fastapi,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): na
.
└── 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
,我只想检查它是否被使用。我的第一个方法是包装read\u main
方法,并检查传递到函数中的项是否确实是ItemModel
的实例。但这是一个死胡同,因为FastAPI端点的准备和存储方式:FastAPI将端点函数对象的副本存储在列表中:(请参阅),然后在请求时计算要调用的端点
来自roo.main导入应用程序
def test_read_main():
在[r.endpoint.\u name\uuuuuuuu]中为r-in-app.routes断言'read\u main'
#检查read_main是否被调用*并*接收到ItemModel实例?
我的第二种方法涉及监视或“破坏”ItemModel
的初始化,这样,如果端点确实使用该模型,那么“破坏”的ItemModel
将导致命中该端点的请求失败。我们“中断”ItemModel
,方法是利用以下事实:(1)FastAPI在请求-响应周期内调用模型的\uuuu init\uuuu
,以及(2)当端点无法正确序列化模型时,默认情况下传播422错误响应:
class ItemModel(基本模型):
姓名:str
def uuu init uuu(uuu pydantic_self,**数据:任意)->无:
打印(“提出POST请求并确认已打印”)
超级()
因此,在测试中,只需模拟\uuuu init\uuu
方法:
- 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)
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+的示例
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)
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"}
- 单元测试示例
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'})
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"}
同样,测试检查端点在序列化为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}"}
422==200的断言错误有点令人困惑,但它基本上意味着,即使我们“破坏了”ItemModel
,我们仍然得到了200/OK响应。。这意味着未使用ItemModel
同样,如果您首先修改了测试,并模拟了OtherModel'的\uuu init\uuu
,而不是
ItemModel`,那么在不修改端点的情况下运行测试将导致类似的失败测试:
def test_read_main(mocker:mockerfix):
client=TestClient(应用程序)
spy=mocker.spy(其他模型,'.\u init')
client.post(“/”,json={'name':'good'})
>spy.assert_调用()
E AssertionError:预期已调用“\uuuu init\uuuuuuu”。
def test_read_main():
client=TestClient(应用程序)
将patch.object(OtherModel,'.'uuuu init'.'uuuuu',wrapps=OtherModel.'uuuuuu init.'uuuuuuuuu')作为mock_init:
response=client.post(“/”,json={'name':'good'})
#assert 422==response.status\u代码
>mocked_init.assert_called()
E AssertionError:预期已调用“\uuuu init\uuuuuuu”。
这里的断言不那么令人费解,因为它说我们期望端点调用OtherModel
的\uuuu init\uuuu
,但它没有被调用。它应该在修改端点以使用项:OtherModel
后通过
最后要注意的一点是,由于我们正在操作\uuuu init\uuuu
,因此它可能会导致“快乐路径”失败,因此现在应该单独测试它。确保撤消/恢复模拟和修补程序:
- 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)
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+的示例
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)
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"}
- 单元测试示例
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'})
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"}
总而言之,您可能想考虑是否/为什么检查正确使用哪个模型是有用的。通常,我只检查传入的有效请求参数是否返回预期的有效响应,同样,无效请求是否返回错误响应