[TIP] Some proposed additions to unittest.mock

Chris Conway clconway at google.com
Mon Apr 15 10:09:02 PDT 2013


I've recently started using the mock library in my code (transitioning from
Mox) and I really like it a lot. There's a few little utilities I've
written for myself and I wonder if you'd accept them as patches.

First, I find that I strongly prefer to use autospecced instances for most
of my mocks. To make that simpler, I wrote a create_strictspec() function
as a wrapper around create_autospec() with instance=True.

I also find that I frequently want a mock function that should never be
called (e.g., in a test where I'm patching open() and testing a code path
where no file is opened). This can be asserted ex post by checking
call_count, but then you don't get any information about where the call
occurred. Alternatively, you can use a NoncallableMock, but then you get a
fairly opaque error: "TypeError: 'NonCallableMock' object is not callable".
I find it preferable to use a utility method expect_never_called() to set
an AssertionError side effect on the mock.

Do you think these would be helpful additions to the library?

Best regards,
Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20130415/6e06f506/attachment.htm>


More information about the testing-in-python mailing list