How can I test that a python function was called, without changing its behavior at all ?
There are many related posts but I could not find one of them that covers it all:
- How to test that a function is called within a function with nosetests (changes behavior)
- Patching a method without changing how the method works? (doesnt work with arguments and return values)
Basically, I just need to spy the function to know whether or not it was called.
For instance, the following snippet has extremely surprising behavior
from unittest.mock import patch
def func(a,b):
print('was called')
return a * b
with patch('test.func') as patched_function:
print(func(4,5))
print(patched_function.called)
Output:
<MagicMock name='func()' id='1721737249456'>
True
was called
20
False
While I was only expecting
was called
20
True