How to avoid using files to store intermediate results
Peter Otten
__peter__ at web.de
Wed Apr 26 03:18:17 EDT 2006
André wrote:
> Now, I want to be able to test the code using the doctest module.
>
> I can't use exec as doctest.testmod() will be testing my entire
> application, not simply the code in the input window!
> While this works, I find it "messy", as it creates some intermediate
> files. I was wondering if there was a better way to do things all in
> memory, in an OS independent way.
Here's a manual setup you might be able to adapt:
import doctest
from cStringIO import StringIO
sample = """
>>> print "hello"
hello
>>> "hello"
'hello'
"""
globs = {}
out = StringIO()
parser = doctest.DocTestParser()
test = parser.get_doctest(sample, globs, "<noname>", "<nofile>", 0)
runner = doctest.DocTestRunner(verbose=True)
runner.run(test, out=out.write)
print out.getvalue()
Peter
More information about the Python-list
mailing list