我想弄清楚是否有辦法(單元測試)驗證該屬性和setter實際上被調用來設置name屬性。Python單元測試類屬性
class DummyName:
def __init__(self):
self.name = ''
@property
def name(self):
return self.name
@name.setter
def name(self, name):
if not isinstance(name, basestring):
raise Exception('Name must be a string.')
self.name = name
試圖做這樣的事情......
@mock.patch.object(DummyName, 'name', new_callable=PropertyMock)
def testNameProperty(self, mock_name):
MockName = Mock()
mock_name.return_value = MockName
dummyName = DummyName()
dummyName.name = 'test_name'
# assert setter is called to set the name
# assert name is called to get the name
# assert name is 'test_name'
好像名稱()和setter從不訪問。任何人有更好的主意?謝謝!
'dummyName.name ='test_name''覆蓋了setter字段。使用不同的名稱爲不同的方法/變量/等 – EsotericVoid