Unittest - adding a doctest suite to unittest.main

P

Paul Moore

My normal testing consists of a tests.py script using unittest, with
the basic

if __name__ == '__main__':
unittest.main()

incantation to get things going.

But I now want to incorporate some other tests (specifically, a text
file containing doctests) and I find that there is no gradual process
for expanding on unittest.main. It seems that I need to jump straight
into the depths of test suites, test loaders, and the rest, and build
my main routine from those. I've looked at the source for
unittest.main, and it doesn't look particularly customisable.

Just before I start diving into the gory details, have I missed a
simple way of adding an additional doctest.DocFileSuite to
unittest.main?

Thanks,
Paul.
 
P

Paul Moore

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top