I would like to patch a method to able to test whether it was called or not, but in the same time I dont want to loose the functionality so the idea was to patch the method with itself:
import unittest
from unittest.mock import Mock, patch
class MyClass():
def foo(self, num):
return num + 2
class myTestClass(unittest.TestCase):
@patch.object(MyClass,'foo', Mock(wraps=MyClass.foo))
def test_foo(self):
my_class = MyClass()
result = my_class.foo(2)
my_class.foo.assert_called_once_with(2)
self.assertEqual(result, 4)
During execution I am getting the following error:
File "/usr/lib64/python3.6/unittest/mock.py", line 1014, in _mock_call
return self._mock_wraps(*args, **kwargs)
TypeError: foo() missing 1 required positional argument: 'num'
Is this kind of patching possible in this way? There is probably a workaround described here