Run all my doctests for all python modules in a folder without seeing failures because of bad imports
You can also create unittests that wrap desired doctests modules, it is a native feature of doctests: http://docs.python.org/2/library/doctest.html#unittest-api.
import unittest
import doctest
import my_module_with_doctests
def load_tests(loader, tests, ignore):
tests.addTests(doctest.DocTestSuite(my_module_with_doctests))
return tests
I think nose is the way. You should either exclude the problematic modules explicitly with -e
or catch the missing imports in your code with constructs like this:
try:
import simplejson as json
except ImportError:
import json
Update:
Another option is to provide mock replacements for the missing modules. Let's say your code has something like this:
import myfunkymodule
and you're trying run your tests in a system where myfunkymodule
is missing. You could create a mock_modules/myfunkymodule.py
file with mock implementations of the stuff you need from it (perhaps using MiniMock, which I highly recommend if you are using doctest). You could then run nose
like this:
$ PYTHONPATH=path_to/mock_modules nosetests --with-doctest