Mocking only a single method on an object Mocking only a single method on an object python python

Mocking only a single method on an object


I think what you are looking for is mock.patch.object

with mock.patch.object(MyClassUnderTest, "submethod") as submethod_mocked:    submethod_mocked.return_value = 13    MyClassUnderTest().main_method()    submethod_mocked.assert_called_once_with(user_id, 100, self.context,                                             self.account_type)

Here is small description

 patch.object(target, attribute, new=DEFAULT,               spec=None, create=False, spec_set=None,               autospec=None, new_callable=None, **kwargs)

patch the named member (attribute) on an object (target) with a mock object.