Wednesday, October 10, 2012

doctesting just one function

There's no such function as doctest.testfunc. Or doctest.testclass, or, for that matter, doctest.testobj. However, you can run the doctests on a single function or class with doctest.run_docstring_examples, like so:

doctest.run_docstring_examples(myfunction, globals=globals())
"That's OK", I thought, "My function doesn't call any globals, so I'll just pass in an empty dictionary." Wrong. run_doctstring_examples actually needs your globals to find the object you're testing; you'll get

    NameError: name 'myfunction' is not defined
The docs say the function is "rarely useful", but I'm finding it very useful for an IPython Notebook-based interactive tutorial I'm hoping to work up shortly!

No comments: