The reason your first test fails is because once you defuse a function in your class, it is not the same object.
>>> def foo(self): pass ... >>> class Foo: pass ... >>> Foo.bar = foo >>> type(Foo.bar) <type 'instancemethod'> >>> type(foo) <type 'function'> >>> >>> Foo.bar is foo False >>> Foo.bar == foo False
In fact, the original function and the new method are of different types. Instead, try checking this condition:
>>> Foo.bar.im_func is foo True
So maybe this is: self.assertIs(my_patch_method, patch_my_lib().target_method.im_func)
source share