我正在尝试使用pytest-mock进行模拟。这个库本质上是mock和patch的插件/包装器。
我的问题被定义为:
我有一个使用SQL Alchemy的应用程序(mymodule.py)。基本上,有一个函数定义了SQL Alchemy中的一些对象,并返回包含这些对象的字典。
def some_function1():
# some code
from sqlalchemy import create_engine, MetaData, Table
engine = create_engine(f"mysql+pymysql://{username}:{password}@{host}:{port})
meta = MetaData(engine)
my_table = Table(
'my_table',
meta,
autoload=True,
schema="some_schema"
)
db_tools = {"engine": engine, "table": my_table}
return db_tools然后,第二个函数将该输出字典作为输入并使用它们:
def some_function2(db_tools, data):
sql_query = db_tools["table"].insert().values(data)
db_tools["engine"].execute(sql_query)
# some more code所以现在我在写单元测试,我不想和真正的数据库通信。所以我只需要模拟所有与sqlalchemy相关的东西。到目前为止,我已经通过执行以下操作成功模拟了create_engine、MetaData和Table:
mocker.patch(
'my_module.create_engine',
return_value=True
)
mocker.patch(
'my_module.MetaData',
return_value=True
)
mocker.patch(
'my_module.Table',
return_value=True
)这让我可以测试some_function1。但现在我需要测试some_function2,它使用.insert()、.values和.execute()方法或属性。我怎么才能修补它?
发布于 2021-05-22 02:19:03
模拟some_function1没有太多好处,因为它只会建立到数据库的连接。它不需要任何输入,它返回的只是一个指向表和连接的字典。关于some_function2,我们可以在db_tools参数中传入多个MagicMock并使用configure_mock。
def test_some_function2(mocker):
mock_table = mocker.MagicMock()
mock_engine = mocker.MagicMock()
fake_query = "INSERT blah INTO foo;"
fake_data = [2, 3]
mock_table.configure_mock(
**{
"insert.return_value": mock_table,
"values.return_value": fake_query
}
)
db_tools = {
"table": mock_table,
"engine": mock_engine
}
some_function2(db_tools, fake_data)
mock_table.insert.assert_called_once()
mock_table.values.assert_called_once_with(fake_data)
mock_engine.execute.assert_called_once_with(fake_query)当测试运行时,它返回以下内容。
========================================================== test session starts ==========================================================
platform darwin -- Python 3.7.4, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: ***
plugins: mock-3.2.0
collected 1 item
test_foo.py . [100%]
=========================================================== 1 passed in 0.01s ===========================================================https://stackoverflow.com/questions/67627176
复制相似问题