Reputation: 397
is there any way to doctest locally defined functions? As an example I would want
def foo():
""" >>> foo()
testfoo"""
def foo2():
""" >>> 1/0 """
print 'testfoo'
foo2()
to NOT pass the test. But still I would not want to make foo2 global for the entire module...
Upvotes: 10
Views: 2045
Reputation: 397
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.
Upvotes: 4
Reputation: 881635
You just have a whitespace problem -- if you fix it, for example as follows:
def foo():
"""
>>> foo()
testfoo"""
def foo2():
""" >>> 1/0 """
print 'testfoo'
foo2()
if __name__ == '__main__':
import doctest
doctest.testmod()
the test passes just fine.
Upvotes: 1