You can set the Mock.side_effect
to be the original method.
from unittest.mock import MagicMock
class A():
def tmp(self):
print("hi")
def b(a):
a.tmp()
a = A()
a.tmp = MagicMock(side_effect=a.tmp)
b(a)
a.tmp.assert_called()
When side_effect
is a function (or a bound method in this case, which is a kind of function), calling the Mock
will also call the side_effect
with the same arguments.
The Mock()
call will return whatever the side_effect
returns, unless it returns the unnittest.mock.DEFAULT
singleton. Then it will return Mock.return_value
instead.