6👍
✅
You need to mock function by its path to which it’s imported.
For example, you use your function inside myapp’s views. So you need to do it in following way:
with mock.patch('myapp.views.which_user', return_value=self.user):
user = which_user(self.user2)
This is because it patches only variables defined in testable module, not patching original libraries and packages in the system or venv.
Source:stackexchange.com