Thanks. I already feared there would be no way around code outside the docstring. Still I thought there might be a trick to import the locals of a function and thus get access to nested functions. Anyhow, a solution using Alex' approach would read
def foo(debug=False):
"""
>>> foo()
testfoo
>>> foo(debug=True)
"""
def foo2():
"""
>>> 1/0"""
print 'testfoo'
if debug :
import doctest
for f in [foo2]: doctest.run_docstring_examples(f,locals())
foo2()
Now the only question is how to automate this approach, so one has something like
for f in locals().values(): doctest.run_docstring_examples(f,locals())
but without the imported and built in functions and variables.