Python unit testing Class properties -
i trying figure out if there's way (unit test) verify property , setter called set name attribute.
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 string.') self.name = name
trying this...
@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 called set name # assert name called name # assert name 'test_name'
seems name() , setter never accessed. has better idea? thanks!
by using mocks you've overwritten code you're trying test. mocks calls external code under test.
an appropriate test code assert exception raised if pass isn't string.
def testnameproperty(self): dummyname = dummyname() self.assertraises(exception): dummyname.name = 12345
Comments
Post a Comment