Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

doctest locally defined functions

Tags:

python

doctest

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...

like image 608
Ivan Lappo-Danilevski Avatar asked Mar 08 '10 17:03

Ivan Lappo-Danilevski


1 Answers

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.

like image 133
Ivan Lappo-Danilevski Avatar answered Nov 13 '22 17:11

Ivan Lappo-Danilevski