Python 如何断言模拟连接的execute方法?
在单元测试中,我只想模拟db连接,并断言我正在使用某个查询字符串执行db api。在我的单元测试中,它并没有显示我调用了executePython 如何断言模拟连接的execute方法?,python,unit-testing,mocking,pytest,Python,Unit Testing,Mocking,Pytest,在单元测试中,我只想模拟db连接,并断言我正在使用某个查询字符串执行db api。在我的单元测试中,它并没有显示我调用了execute def foo(): # did not show code to setup connection and query connection = ... query = ... with connection cursor = connection.cursor() cursor.execu
def foo():
# did not show code to setup connection and query
connection = ...
query = ...
with connection
cursor = connection.cursor()
cursor.execute(query)
return cursor.fetchall()
单元测试
@patch("db_module.connect")
def test_query(connect):
cursor = connect.cursor.return_value
cursor.execute.return_value = ['foo']
cursor.execute.called_once_with("SELECT * FROM my_table WHERE ....")
Pytest告诉我execute被调用了0次
我该如何对cursor.execute()进行模拟和断言