我有一个这样的测试用例:
@mock.patch('xlwt.Workbook.Workbook.save')
def test_Generic_send_test_email_address(self, workspace_mock):
workspace_mock.return_value = None
oi = OptinInvitesGeneric()
oi.compute(...)
self.assert ...
实际方法会进行一些处理并将结果保存在Excel电子表格中。
class OptinInvitesGeneric(OptinBase):
def compute(...):
...
wb = excel_tool.write_xls(...)
wb.save('{0}.xls'.format(category))
似乎我的模拟补丁没有接管workbook.save()。我错过了什么?
答案 0 :(得分:1)
我不知道您为什么要尝试修补xlwt.Workbook.Workbook
,但这两个对我有用:
@patch.object(xlwt.Workbook, 'save', return_value=None)
def test_patch_object(mock):
wb = xlwt.Workbook()
assert wb.save() == None
@patch('xlwt.Workbook.save', return_value=None)
def test_patch(mock):
wb = xlwt.Workbook()
assert wb.save() == None