Which mock library do you prefer?

Phlip phlip2005 at gmail.com
Mon Feb 15 17:57:37 CET 2010


Lacrima wrote:

> I am newbie mastering test driven development. I can't clarify myself
> which mock library to use.
> There are number of them and which one do you prefer?
>
> Two libraries that attracted my attention are:
> * minimock
> * dingus
> As for me the latest one, dingus, is the easiest (see this screencast:http://vimeo.com/3949077  ), but it has very few downloads from pypi,
> so it scares me a little.
> Minimock has wider usage and community, but I have some troubles using
> it. Maybe I am wrong, but with minimock you always have to keep track
> the order of imports in your test modules. Well, may be I just don't
> understand fully how minimock works.
>
> What are your suggestions?

I have used http://pypi.python.org/pypi/mock/0.6.0 . It mocks, and it
has a mode that works one method at a time, and another mode that
mocks a method before its owning object gets constructed.

However, TDD is not about mocking, and on greenfield code you should
only mock to recover from some external problem, such as:

     - a random number generator
     - the system clock
     - anything over "the wire" - over a TCP/IP socket
     - hardware, such as your graphics or sound

Never mock to avoid hitting the database. Some TDD verbiage advises
"never hit the database". That is a mind-game to force you to decouple
your code. Your objects should always have the option (via
"construction encapsulation") to run as stubs, with some of their
behaviors turned off. And if you TDD low-level code that hits a
database, a mock would only tell the test what it wants to hear. And
if you TDD high-level code that manages business rules, database
records make perfectly good behavioral "fixtures" to support those
rules.

--
  Phlip
  http://c2.com/cgi/wiki?ZeekLand



More information about the Python-list mailing list