Unittest - adding a doctest suite to unittest.main

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul  Moore

    Unittest - adding a doctest suite to unittest.main

    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.DocFile Suite to
    unittest.main?

    Thanks,
    Paul.
  • Duncan Booth

    #2
    Re: Unittest - adding a doctest suite to unittest.main

    Paul Moore <p.f.moore@gmai l.comwrote:
    Just before I start diving into the gory details, have I missed a
    simple way of adding an additional doctest.DocFile Suite to
    unittest.main?
    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.

    Google for "DocFileSui te unittest.main" and you should find plenty of
    examples. e.g. http://mail.zope.org/pipermail/zope3-checkins/2008-
    May/029732.html or

    eventprovider/src/calcore/tests/test_cal.py?rev =50936

    --
    Duncan Booth http://kupuguy.blogspot.com

    Comment

    • Paul  Moore

      #3
      Re: Unittest - adding a doctest suite to unittest.main

      On 14 Oct, 16:09, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
      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.tes t 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.defaul tTestLoader.loa dTestsFromNames (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.TestPr ogram to
      intercept the bit where it runs the tests in __init__:

      class TestMain(unitte st.TestProgram) :
      def runTests(self):
      pass
      def parentRunTests( self):
      unittest.TestPr ogram.runTests( self)

      Then I create the class, fiddle with the testmain.test attribute, and
      then call testmain.parent RunTests(), but that feels like a really ugly
      hack :-(
      Google for "DocFileSui te 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.

      Comment

      • Ben Finney

        #4
        Re: Unittest - adding a doctest suite to unittest.main

        Duncan Booth <duncan.booth@i nvalid.invalidw rites:
        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.
        Better to name it ‘suite’, so that its name doesn't match the default
        search for individual test functions.

        --
        \ “Remember: every member of your ‘target audience’ also owns a |
        `\ broadcasting station. These ‘targets’ can shoot back.” —Michael |
        _o__) Rathbun to advertisers, news.admin.net-abuse.email |
        Ben Finney

        Comment

        Working...