I have a class like this,
from external_package.module.sub_module import fun
class MyClass:
def __init__(self, x, y):
self.x = x
self.y = y
self.z = fun()
def printer(self):
print(f"X : {self.x}, y : {self.y}, z: {self.z}")
I want to write a unit test for this class and mock the function call that is happening in the init method.
I have tried patching like this,
from external_package.module import sub_module
def test_my_class(mocker):
mocker.patch.object(sub_module, 'fun', return_value="this is mocked")
obj = MyClass(10, 20)
assert obj.z == "this is mocked"
But I can see that the call is not patched and a function call happens in the init method.
How can I patch the call with pytest-mock or any other mocking packages?