我有类似的代码:
from mock import MagicMock
class Parent(object):
def test_method(self, param):
# do something with param
pass
class Child(Parent):
def test_method(self, param):
# do something Child-specific with param
super(Child, self).test_method(param)
现在我想确保Child.test_method
来电Parent.test_method
。为此,我想使用mock模块/库中的assert_called_once_with
。但是,我无法想办法做到这一点。如果子类不会覆盖该方法,那么Need to mock out some base class behavior in a python test case指出这很容易。但是,在我的情况下,这是相同的方法,所以我该怎么办?
答案 0 :(得分:2)
您可以使用patch.object:
with mock.patch.object(Parent, 'test_method') as mock_method:
child = Child()
mock_param = mock.Mock()
child.test_method(mock_param)
mock_method.assert_called_with(mock_param)