from unittest.mock import MagicMock
class Person:
def __init__(self, name):
self.name = name
def myfunc(self):
return self.name
p1 = Person("John")
print(p1.myfunc())
orig_func = p1.myfunc
p1.myfunc = MagicMock(return_value="Mock Return")
print(p1.myfunc())
p1.myfunc = orig_func
print(p1.myfunc())
This should work. Just store it in a variable before you mock it and restore it when you’re done with the mock.