Various behaviors of doctest
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sun Feb 27 18:55:47 EST 2011
On Sun, 27 Feb 2011 08:55:10 -0800, Gnarlodious wrote:
> Using the doctest module, I get three different outputs:
>
> 1) From the Terminal shell, I see a full report:
> python ~/Sites/Sectrum/Filter.py -v
Can we assume that Filter.py, whatever that is, runs doctest.testmod()?
> 2) From the Terminal interactive session, I see an abbreviated report of
> only the failures:
> from doctest import testmod; testmod(Filter)
That's because you haven't specified the verbose option, either by
injecting -v into sys.argv, or by verbose=True as an argument to testmod.
> 3) From a browser CGI, I see a named tuple: from doctest import testmod;
> doctest.testmod()
>
> This returns output like:
>
> TestResults(failed=1, attempted=5)
>
> The browser is invoking the exact same code as the shell doctest, so why
> should it return a named tuple?
Because testmod *always* returns a named tuple. Read the Fine Manual at
the interactive interpreter with:
help(testmod)
> And how do I get it to return the full
> text so I can read it in the browser?
You don't. Consider a simplified version:
def testmod():
print "lots of stuff"
return (failures, total)
If you want to capture "lots of stuff", you have to either play nasty
tricks with replacing standard out with a StringIO instance, or similar,
or you have to look at the implementation of testmod() and re-implement
it yourself.
Oh, here's a thought, if you're running Python 3, you could monkey-patch
doctest:
# COMPLETELY UNTESTED!!!
from stringio import StringIO
from functools import partial
buffer = StringIO()
myprint = partial(print, file=buffer)
doctest.print = myprint
doctest.testmod()
Now somehow you have to pass buffer.as_string() to your CGI program, in
whatever way you would normally do so.
--
Steven
More information about the Python-list
mailing list