Doctests for nested functions

B

bearophileHUGS

Recently I have posted this same question on it.comp.lang.python, maybe
there aren't solutions, but I'd like to know what you think.

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).
Example:

def foo():
""" 21
"""
def bar():
""" 11
"""
print 10
print 20
import doctest
doctest.testmod()

Bye,
bearophile
 
J

Just

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
 
P

Paddy

Just said:
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""" 20
"""
def 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)!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top