I'm working with python for some years, and when it comes to unit testing and mocking I used to mock the collaborators of the unit under test where they are declared, for example if I want to mock a function called my_function declared in my_module.py module and used in my unit under test, I would write
@mock.patch('my_module.my_function')def test_toast(mock_my_function): ...and it works roughly all the times, but sometimesit does not work until mocking it where it is used, for example supposing my unit under test is declared in foo_module.py, and it imports my_function from my_module.pyin order to mock my_function I would write
@mock.patch('foo_module.my_function')def test_toast(mock_my_function): ...My first question is what the differences between the two methods of mocking in python ?, and when we should use each one ?
As far as my observations, they both work, but sometimes mocking where it's declared does not work.
Can you please help me to understand this behavior in details