Just wrote:
> In article <[EMAIL PROTECTED]>,
>  [EMAIL PROTECTED] wrote:
>
> > Can doctests be added to nested functions too? (This can be useful to
> > me, I use nested function when I don't have attributes that I have to
> > remember, but I want to split the logic in some subparts anyway).
>
> I think we had that discussion before, but that's not what nested
> functions are for (in Python). Use modules for that. Also solves your
> doctest problem nicely.
>
> Just

Just is right.
But...
I struggled with coming up with something. The problem is that bar does
not exist in the right form unlee you are executing the function so:

def foo(_test = False):
  r"""
  >>> foo()
  21
  >>> foo(_test = True)
  20
  """
  def bar():
    """
    >>> bar()
    11
    """
    print 10
  print 20

  if _test:
    _locals = locals()

    import doctest

    g = globals().copy()
    g.update(_locals)
    g['__test__'] = {}

    _totest = {}
    for loc in _locals.values():
      try:
        if loc.__doc__ and loc.__name__ and (loc.__name__
            not in g['__test__']):
          _totest[loc.__name__] = loc
      except:
        pass
    for _name, _testing in sorted(_totest.items()):
      doctest.run_docstring_examples(_testing, g,
          name = "foo:" + _name)

import doctest
doctest.testmod()


The output is:

Trying:
    foo()
Expecting:
    21
**********************************************************************
File "__main__", line 3, in __main__.foo
Failed example:
    foo()
Expected:
    21
Got:
    20
Trying:
    foo(_test = True)
Expecting:
    20
**********************************************************************
File "__main__", line 5, in __main__.foo
Failed example:
    foo(_test = True)
Expected:
    20
Got:
    20

**********************************************************************
    File "__main__", line 10, in foo:bar
    Failed example:
        bar()
    Expected:
        11
    Got:
        10
1 items had no tests:
    __main__
**********************************************************************
1 items had failures:
   2 of   2 in __main__.foo
2 tests in 2 items.
0 passed and 2 failed.
***Test Failed*** 2 failures.
*** DocTestRunner.merge: '__main__.foo' in both testers; summing
outcomes.
*** DocTestRunner.merge: '__main__' in both testers; summing outcomes.


- Paddy.
(but use a module instead)!

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to