[TIP] Modifying context for doctests under nose

Alan McIntyre alan.mcintyre at gmail.com
Wed Jun 25 10:08:34 PDT 2008


Hi all,

I'd like to be able to modify the context for doctests to effectively
add implicit imports for all doctests (import numpy as np, in this
case), so that every bit of sample code in the docstrings doesn't need
to include bits of import boilerplate.  At the moment I can see two
ways of doing this:

1. Monkeypatching the Doctest plugin's loadTestsFromModule method, and
adding (for example) a "test.globs['np'] = numpy" before the test is
wrapped up in a DocTestCase.
2. Subclass Doctest and modify the test in startTest
(test.test._dt_test.globs['np'] = numpy), and then pass in a list of
plugins (it seems like I need to pass in every plugin that might be
possibly used, though, including the builtins).

Is there a more appropriate way of doing this?  I don't like
monkeypatching or touching underscored attributes, but I don't think
either one will be a big deal if these are the only way to do it
currently.

Thanks,
Alan



More information about the testing-in-python mailing list