Python automatic testing: mocking an imported module?

Chris Lasher chris.lasher at
Wed Mar 28 03:13:22 CEST 2007

On Mar 27, 6:18 pm, "Silfheed" <silfh... at> wrote:
> Heyas
> So we have the following situation: we have a that we want
> to automatically test out and verifiy that it is worthy of being
> deployed.  We want our to test the code for
> without changing the code for has a module in it
> that we want to mock in some tests and in others use the real module.

I think you mean " *imports* a module", rather than "has a
module in it". Semantics but I was thrown off by this at first.

Why not write tests for the module that imports (here called
"")? If you write out the tests and passes to spec, then
you should feel confident it will work correctly with Never
trust it; always play paranoid coder, but feel comfortable enough
using it.

After all, why should you feel comfortable using modules in the
standard library? Nobody guarantees bugs don't still lurk in there
(heck, c.l.p just found one in pdb today), however, they have unit
tests out the wazzoo that demonstrate, to the best of our knowledge,
the module works to spec. If that approach is good enough for the
standard library, wouldn't it be good enough for you?


More information about the Python-list mailing list