Mocking Python imports

July 3, 2012

Writing unit tests with Python is a joy, especially with the excellent mock library.

You can tweak the language and mock almost anything to your will, making testing even the smallest of units very easy.

HOWEVER , mocking imports, when a class / module depends on imports which you might not have on your machine, such as windows modules (oei vei) when you are (and you should be) on a nix machine.

Another typical case is when you integrate a module to a big system, which imports THE ENTIRE INTERNETZ in each file.

In those cases it’s critical to be able to isolate your class / module by totally disconnecting it from those modules.

Mock to the rescue:

TLDR: you can clone git://gist.github.com/3039780.git and play around with it…

Running nosetest now will yield the following :

nosetests
E.
======================================================================
ERROR: Failure: ImportError (No module named the.internetz)
----------------------------------------------------------------------

To fix this, we need to mock the “the internetz” module:

The reason to put all the patches, mocks and imports in the setUp function is that you’d probably reuse them in the same test class on other test methods.