I am using mock for testing in Python. I am trying to unit test a metaclass which overwrites the __new__ method and then calls type.__new__(cls) internally.
I don’t want to actually call type.__new__, so I want to mock out type. Of course, I can’t patch __builtin__.type because it breaks object construction within the test.
So, I really want to limit mocking type within the module under test. Is this possible?
Yes. You
patchas close to where you’re going to call your function as possible for just these sort of reasons. So, in your test case, only around the function (or whatever callable) you’ll call that’s under tests, you can patchtype.The documentation for
patchhas plenty of examples for doing this if you’d like to peruse them.Cheers.