[TIP] Mock objects in testing object composition?

Kumar McMillan kumar.mcmillan at gmail.com
Thu May 17 07:55:45 PDT 2007


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.  That is, something outside of your
control.  I'm leaving this intentionally vague, so for clarity here
are some example of cases when I've used mock objects recently:

- made a mock ftp object that does not connect to a server but instead
makes a tmp directory locally and reads/writes to that dir as if it
were a server
- used wsgi_intercept [1] to queue up static responses for what the
calling code thinks is a web service.  The calling code even thinks it
is connecting to a URL and port (fooled!).

[1] http://darcs.idyll.org/~t/projects/wsgi_intercept/README.html



More information about the testing-in-python mailing list