如何解决如何断言模拟连接的execute方法?
在单元测试中,我只想模拟数据库连接并断言我正在使用某个查询字符串执行db api。在我的单元测试中,它根本没有显示我称为执行。
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()进行模拟和断言?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。