python - How to mock a base class's method when it was overridden? -
i have code similar this:
from mock import magicmock class parent(object): def test_method(self, param): # param pass class child(parent): def test_method(self, param): # child-specific param super(child, self).test_method(param) now want make sure child.test_method calls parent.test_method. this, i'd use assert_called_once_with mock module/library. however, cannot figure out way this. if method not overridden subclass easy pointed out need mock out base class behavior in python test case. however, in case same method, do?
you can use patch.object:
with mock.patch.object(parent, 'test_method') mock_method: child = child() mock_param = mock.mock() child.test_method(mock_param) mock_method.assert_called_with(mock_param)
Comments
Post a Comment