Dynamic doctests?

M

mitchell

I'm trying to execute doc tests without writing to the filesystem (i.e.
in the Python interpreter). I have something like:

"""
Docstring: 6
"""

# Code:
n=6

import doctest
doctest.testmod()

The tests all pass when saving this text to a python script (as it
should), but when I load this text into a string and run:

code='"""\n>>> n\n6\n"""\nn=6\nimport doctest\ndoctest.testmod()'
exec(code)

I get:

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 7, in ?
File "/usr/lib/python2.4/doctest.py", line 1841, in testmod
for test in finder.find(m, name, globs=globs,
extraglobs=extraglobs):
File "/usr/lib/python2.4/doctest.py", line 851, in find
self._find(tests, obj, name, module, source_lines, globs, {})
File "/usr/lib/python2.4/doctest.py", line 914, in _find
for valname, val in getattr(obj, '__test__', {}).items():
AttributeError: 'function' object has no attribute 'items'

Can what I'm trying to do be done?

Any help is greatly appreciated.
 
?

=?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=

code='"""\n>>> n\n6\n"""\nn=6\nimport doctest\ndoctest.testmod()'
exec(code)

Remove 'doctest.tesmod()' and the import from your 'code' string.

]]] exec(code)
]]] import doctest
]]] doctest.testmod()

should do the trick.

Cheers,

SB
 

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,780
Messages
2,569,611
Members
45,272
Latest member
MaricruzDu

Latest Threads

Top