Programmatically replacing an API for another module

Larry Bates larry.bates at
Tue Nov 21 16:05:51 CET 2006

> Depends on what all I'm mocking.  If I'm mocking an expensive service
> across the internet that is time consuming to connect to, or even
> mocking a well-defined system API call that is similarly time
> consuming to call, then I can stub something in that doesn't do
> anything but validate the arguments.  Additionally, I can "document"
> the caveats and possibly-errant behavior with my mock, and
> subsequently get automatic testing that I'm dealing with API bugs
> properly.

When I find myself doing what you describe, I often introduce
a "testing" mode switch into the equation.  It is normally a
keyword argument to functions that I then interrogate in the
function.  If I'm running in testing mode, I execute the less
"expensive" testing code.  If I'm not in test mode, I don't.
There may be better ways, but this has worked well for me.


More information about the Python-list mailing list