Python automatic testing: mocking an imported module?

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


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

I think you mean "testee.py *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 testee.py imports (here called
"foo.py")? If you write out the tests and foo.py passes to spec, then
you should feel confident it will work correctly with testee.py. 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?

Chris




More information about the Python-list mailing list