Travis Parks
Travis Parks

Reputation: 8695

Python Mock type only within module

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?

Upvotes: 0

Views: 809

Answers (1)

Julian
Julian

Reputation: 3429

Yes. You patch as 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 patch type.

The documentation for patch has plenty of examples for doing this if you'd like to peruse them.

Cheers.

Upvotes: 1

Related Questions