When models
is imported, it runs from my_module.my_file import my_function
, which is not mocked yet. When you run your test case, the my_function
name in the models
module is already bound to the real function: patching my_files
has no effect.
What you need is to patch models.my_function
:
with patch('models.my_function') as add_method:
m = MyModel(name=name)
m.save()
An alternative would be to patch my_file.my_function
at models
import time.
See where to patch documentation.