Dynamic doctests?

mitchell mforal at gmail.com
Fri May 13 12:32:30 EDT 2005


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.




More information about the Python-list mailing list