469,352 Members | 2,154 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Dynamic doctests?

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

"""
Docstring:
n

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.

Jul 19 '05 #1
1 1264

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

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Materialised | last post: by
3 posts views Thread by Stephen Gennard | last post: by
reply views Thread by Pascal Costanza | last post: by
2 posts views Thread by bearophileHUGS | last post: by
1 post views Thread by nriley | last post: by
2 posts views Thread by Brian van den Broek | last post: by
5 posts views Thread by bearophileHUGS | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.