[TIP] Mocking properties on an instance

Julian Berman julian at grayvines.com
Sat Sep 3 18:20:56 PDT 2011


Hi!

So, I'm aware of mock.patch.object, but I have a use case at hand that seems to most easily be accomplished by patching properties (/ arbitrary descriptors) on an already existing instance.

Here's a test case showing my use case, along with a partial implementation that happens to work for me.

http://paste.pocoo.org/show/469788/

It happens to be that in my case, as of right now I have strictly non-data descriptors, so the quickly thrown together implementation simply uses the AttributeError (yeah sorry :P) to tell if we have a property or not, but obviously this could be done by checking to see if the class' attr has __get__ defined.

As far as some quick thought can tell, the only issue with this would be when klass.foo was a thing with __get__ while instance_of_klass.foo was an evil thing that replaced the descriptor on itself, but I highly doubt that's something at all to worry about?

Is there a particular aversion to including at least the possibility of trying to patch a property on an instance by doing something similar?

Thanks,

Julian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20110903/1a29d5f3/attachment.htm>


More information about the testing-in-python mailing list