我要测试的派生代码中有此代码。
tigerContracts = ['Element1','Element2']
def getReportingID(tigerKey):
dig = '2' if tigerKey in tigerContracts else '0'
return dig
我的单元测试是这样的:
class GeneratorDerived(unittest.TestCase):
def tigerContracts(self):
return ['Element3']
@patch('derived.tigerContracts')
def test_getReportingID(self, mockFunc):
mockFunc.return_value = self.tigerContracts()
reportingID = derived.getReportingID('XX-XX')
self.assertEqual(reportingID,'0')
reportingID = derived.getReportingID('Element3')
self.assertEqual(reportingID,'2')
我的问题是,我正在将该方法作为返回值。
MagicMock name ='tigerContracts'id ='138171616'
相反,我希望返回值为
['Element3']
答案 0 :(得分:0)
也许这是您正在寻找的更多信息,以得到解释: 当我们调用self.tigerContracts()时,将创建并返回一个新的模拟。根据我的评论:Full explanation
import unittest
import derived
from mock import patch
class GeneratorDerived(unittest.TestCase):
def test_getReportingID(self):
with patch('derived.tigerContracts', 'Element3'):
reportingID = derived.getReportingID('XX-XX')
self.assertEqual(reportingID,'0')
reportingID = derived.getReportingID('Element3')
self.assertEqual(reportingID,'2')