模拟不覆盖 Python 中函数的 return
Mock not overriding the return of a function in Python
我正在对我的项目 类 之一实施单元测试。我要测试的方法是 queryCfsNoteVariations:
class PdfRaportDaoImpl:
def queryCfsNoteVariations(self, reportId):
sql = """
select v.* from item_value_table v
where v.table_id in
(select table_id from table_table t
where t.report_id=%s and table_name='CFS')
"""
cfsItemList = dbFind(sql, (reportId))
sql = "select * from variations_cfs_note"
cfsNoteVariations = dbFind(sql)
if cfsNoteVariations == None or len(cfsNoteVariations) == 0:
raise Exception("cfs note variations is null!")
cfsNoteVariationList = []
for itemInfo in cfsItemList:
for cfsNoteVariation in cfsNoteVariations:
if (
cfsNoteVariation["item_name_cfs"].lower()
== itemInfo["item_name"].lower()
):
cfsNoteVariationList.append(cfsNoteVariation["item_name_cfs_note"])
if len(cfsNoteVariationList) > 0:
return cfsNoteVariationList, itemInfo["item_note"]
return None, None
其中有一个路径:/com/pdfgather/PDFReportDao.py
在我的测试中,我正在对位于 /com/pdfgather/GlobalHelper.py 中的 dbFind() 方法进行修补。我当前的测试如下所示:
from com.pdfgather.PDFReportDao import PdfReportDaoImpl
@patch("com.pdfgather.GlobalHelper.dbFind")
def test_query_cfs_note_variations(self, mock_find):
mock_find.side_effect = iter([
[{"item_name" : "foo"}, {"item_name" : "hey"}],
[{"item_name_cfs": "foo"},
{"item_name_cfs": "foo"},
{"item_name_cfs": "hey"}]]
])
report_id = 3578
result = TestingDao.dao.queryCfsNoteVariations(report_id)
# Printing result
print(result)
但是我没有得到我想要的结果,即进入循环并从循环内返回。相反,dbFind 没有返回任何内容(但它不应该返回,因为我已经为 dbFind 预先分配了返回值)。
提前致谢!
Python 指的是 com.pdfgather.PDFReportDao.dbFind
和 com.pdfgather.GlobalHelper.dbFind
是两个不同的 类。第二个是您要修补的导入。尝试将补丁更改为:
@patch("com.pdfgather.PDFReportDao.dbFind")
我正在对我的项目 类 之一实施单元测试。我要测试的方法是 queryCfsNoteVariations:
class PdfRaportDaoImpl:
def queryCfsNoteVariations(self, reportId):
sql = """
select v.* from item_value_table v
where v.table_id in
(select table_id from table_table t
where t.report_id=%s and table_name='CFS')
"""
cfsItemList = dbFind(sql, (reportId))
sql = "select * from variations_cfs_note"
cfsNoteVariations = dbFind(sql)
if cfsNoteVariations == None or len(cfsNoteVariations) == 0:
raise Exception("cfs note variations is null!")
cfsNoteVariationList = []
for itemInfo in cfsItemList:
for cfsNoteVariation in cfsNoteVariations:
if (
cfsNoteVariation["item_name_cfs"].lower()
== itemInfo["item_name"].lower()
):
cfsNoteVariationList.append(cfsNoteVariation["item_name_cfs_note"])
if len(cfsNoteVariationList) > 0:
return cfsNoteVariationList, itemInfo["item_note"]
return None, None
其中有一个路径:/com/pdfgather/PDFReportDao.py
在我的测试中,我正在对位于 /com/pdfgather/GlobalHelper.py 中的 dbFind() 方法进行修补。我当前的测试如下所示:
from com.pdfgather.PDFReportDao import PdfReportDaoImpl
@patch("com.pdfgather.GlobalHelper.dbFind")
def test_query_cfs_note_variations(self, mock_find):
mock_find.side_effect = iter([
[{"item_name" : "foo"}, {"item_name" : "hey"}],
[{"item_name_cfs": "foo"},
{"item_name_cfs": "foo"},
{"item_name_cfs": "hey"}]]
])
report_id = 3578
result = TestingDao.dao.queryCfsNoteVariations(report_id)
# Printing result
print(result)
但是我没有得到我想要的结果,即进入循环并从循环内返回。相反,dbFind 没有返回任何内容(但它不应该返回,因为我已经为 dbFind 预先分配了返回值)。
提前致谢!
Python 指的是 com.pdfgather.PDFReportDao.dbFind
和 com.pdfgather.GlobalHelper.dbFind
是两个不同的 类。第二个是您要修补的导入。尝试将补丁更改为:
@patch("com.pdfgather.PDFReportDao.dbFind")