Unittest - adding a doctest suite to unittest.main

Discussion in 'Python' started by Paul Moore, Oct 14, 2008.

  1. Paul  Moore

    Paul Moore Guest

    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.
    Paul Moore, Oct 14, 2008
    #1
    1. Advertising

  2. Paul  Moore

    Paul Moore Guest

    On 14 Oct, 16:09, Duncan Booth <> 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.
    Paul Moore, Oct 14, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John M
    Replies:
    1
    Views:
    1,101
    Kumar Reddi
    May 29, 2005
  2. Replies:
    24
    Views:
    781
    Ben Finney
    Mar 29, 2006
  3. John J. Lee
    Replies:
    0
    Views:
    254
    John J. Lee
    May 5, 2006
  4. Steven D'Aprano

    Integrating doctest with unittest

    Steven D'Aprano, Dec 11, 2010, in forum: Python
    Replies:
    3
    Views:
    303
    SegundoBob
    Jan 11, 2011
  5. Paul  Moore
    Replies:
    3
    Views:
    86
    Terry Reedy
    Dec 20, 2013
Loading...

Share This Page