我正在尝试Mock返回列表的方法。在设置了return_value
之后,它仍然返回一个Mock对象,而不是列表,所以我不能迭代该对象。
我想测试的功能示例:
def func(db_engine):
query = f"SELECT * FROM table"
query_result = db_engine.execute(query).fetchall()
extracted_val = []
for res in query_result:
extracted_val.append(res[1])
return extracted_val
测试示例:
def test_fun():
query = MagicMock()
query_res = [("name_1", "value_1"), ("name_2", "value_2")]
expected_vals = ["value_1", "value_2"]
db_engine = MagicMock()
db_engine.execute(query).fetchall().return_value = query_res
vals = func(db_engine)
assert expected_vals == vals
总之,db_engine.execute(query).fetchall()
必须返回list,所以我可以对query_result
进行迭代,但这并没有发生。虽然query_result
在矩阵return_value
中有一个正确的值,但我不能迭代它
您可以在这里找到如何模拟链式调用文档。以下是单元测试解决方案:
func.py
:
def func(db_engine):
query = f"SELECT * FROM table"
query_result = db_engine.execute(query).fetchall()
extracted_val = []
for res in query_result:
extracted_val.append(res[1])
return extracted_val
test_func.py
:
import unittest
from func import func
from unittest.mock import MagicMock
class TestFunc(unittest.TestCase):
def test_func(self):
query_res = [("name_1", "value_1"), ("name_2", "value_2")]
expected_vals = ["value_1", "value_2"]
db_engine = MagicMock()
execute = db_engine.execute.return_value
execute.fetchall.return_value = query_res
vals = func(db_engine)
db_engine.execute.assert_called_with("SELECT * FROM table")
execute.fetchall.assert_called_once()
self.assertEqual(expected_vals, vals)
if __name__ == '__main__':
unittest.main()
100%覆盖率的单元测试结果:
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Name Stmts Miss Cover Missing
-----------------------------------------------------------------------
src/stackoverflow/61318070/func.py 7 0 100%
src/stackoverflow/61318070/test_func.py 16 0 100%
-----------------------------------------------------------------------
TOTAL 23 0 100%
Python版本:Python 3.7.5