[TIP] Mock objects in testing object composition?

Benji York benji at benjiyork.com
Thu May 17 08:11:47 PDT 2007

Kumar McMillan wrote:
> On 4/24/07, Julius Lucks <lucks at fas.harvard.edu> wrote:
>> Many thanks Benji and Paul.
>> Looks like simpler is better.  So when are mock objects useful?
> in my experience, mock objects are useful mostly for depending on some
> kind of "external" resource.

Purists would probably call those "stubs" instead of "mocks", but I see 
what you're saying.

A technique I like to use in those situations is to write a 
parameterized test and apply the same test twice.  Once in an 
environment where the "real" thing is used and once where the "stub" is 
used (with the "real" test running only in a non-developer-blocking way, 
like buildbot (except when explicitly requested)).

That gives you the best of both words: you can test against the real 
thing when you've made the effort of setting up the environment, and you 
can be reasonably sure that your stubbed out environment is realistic 
enough when not.
Benji York

More information about the testing-in-python mailing list