[py-dev] running doctest-based tests

Max Ischenko ischenko at gmail.com
Fri Jun 10 12:07:06 CEST 2005


David,

> I made a couple of changes to your conftest.py. The original one did not
> run under my version of Python. The classes being used were added in 2.4.
> Please see if that works for you. Later today I'll investigate Holger's
> suggestion.

Besides DoctestFinder (that you have replaced with Tester) you need a way to
capture doctest output as well. DocTest in 2.4 provides nice report_*
methods plus an optional 'writer' argument to aid this task. Have no idea
how it'd translate for pre-2.4 version.

Python doctest module has been improved considerably for 2.4 release and I
think the simplest and reasonable way to make it work for version < 2.4 is
to use standalone doctest24 module (can be found on the web). 

If my suggestion (to use doctest24.py) is not viable for some reason in your
situation then you have to rework DoctestItem.run() as well.

Btw, the code you posted doesn't work with Python 2.4 because doctest has no
function _find_tests() anymore.

Here is my current code:

class DoctestItem(py.test.Item):
    def __init__(self, test, parent):
        py.test.Item.__init__(self, test.name, parent)
        self.test = test
    def run(self):
        runner = doctest.DocTestRunner()
        out = StringIO()
        writer = lambda msg: out.write(msg)
        failed, tried = runner.run(self.test, out=writer)
        if failed:
            py.test.skip(out.getvalue())

class DoctestModule(py.test.collect.Module): 
    def funcnamefilter(self, name): 
        return True
    def classnamefilter(self, name): 
        return True
    def buildname2items(self): 
        d = {} 
        finder = doctest.DocTestFinder()
        doctests = finder.find(self.obj)
        for test in doctests:
            if test.examples:
                d[test.name] = DoctestItem(test, self)
        return d





More information about the Pytest-dev mailing list