Unittest - adding a doctest suite to unittest.main

Paul Moore p.f.moore at gmail.com
Tue Oct 14 11:32:01 EDT 2008


On 14 Oct, 16:09, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
> Create a function named test_suite which creates a test suite containing
> all your tests including the doctests. Pass that to main as the defaultTest
> argument.

Ah, thanks. I see now - a suite is itself a test. That makes sense.

But how do I name the default "run all the unit tests in this module"
suite, so I can add to it rather than replace it? It's available as
the TestProgram.test attribute, but TestProgram's nasty behaviour of
running the test suite as part of its __init__ means I don't get a
chance to grab it before the tests run :-( (That's what I meant when I
said the default isn't very customisable).

Nearest I can gather, it's
unittest.defaultTestLoader.loadTestsFromNames(self.testNames,self.module)
but I can't get at those 2 attributes easily either! Pretty soon, I'll
have cut & pasted the whole class and modified it as I need.

The best approach I could find was to subclass unittest.TestProgram to
intercept the bit where it runs the tests in __init__:

    class TestMain(unittest.TestProgram):
        def runTests(self):
            pass
        def parentRunTests(self):
            unittest.TestProgram.runTests(self)

Then I create the class, fiddle with the testmain.test attribute, and
then call testmain.parentRunTests(), but that feels like a really ugly
hack :-(

> Google for "DocFileSuite unittest.main" and you should find plenty of
> examples. e.g.http://mail.zope.org/pipermail/zope3-checkins/2008-
> May/029732.html orhttp://svn.nuxeo.org/trac/pub/browser/Python/CalCore/branches/snowspr...
> eventprovider/src/calcore/tests/test_cal.py?rev=50936

Thanks. I really should have done that (I searched the documentation
but not Google). Ironically, that query now pops up this thread as the
number one hit. Not sure what to make of that...

Paul.



More information about the Python-list mailing list