I have a utility method that behaves like this
def my_patch_method(self):
pass
def patch_my_lib():
from mylib import MyClass
MyClass.target_method = my_patch_method
return MyClass()
This test fails:
self.assertEqual(my_patch_method, patch_my_lib().target_method)
Whereas this one works:
self.assertEqual(my_patch_method.__name__, patch_my_lib().target_method.__name__)
As the patch method does not have the same name, this is still acceptable proof that patch_my_lib() is doing what it’s payed for but why doesn’t the first work as I would expect ? And, is there a way to “fix” it ?
The reason your first test fails is that once you monkey-patch the function into your class, it isn’t really the same object any more.
In fact, the original function and the new method have different types. Instead, have your first test check this condition:
So maybe this:
self.assertIs(my_patch_method, patch_my_lib().target_method.im_func)