Running doctests with unittest

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Heller

    #1

    Running doctests with unittest

    I'm trying to integrate some doctest tests with unittest. The tests
    must be exposed as one or more subclasses of unittest.TestCa se, so I'm
    collecting them with a call to doctest.DocTest Suite(), and then add them
    to a TestCase class I have created.
    The tests seem to run, but they always seem to succeed - I have no idea
    why. Any ideas?

    Thomas

    ---snip---
    """[color=blue][color=green][color=darkred]
    >>> print "Hi"
    >>> print 1213[/color][/color][/color]
    """

    def func():
    """[color=blue][color=green][color=darkred]
    >>> print "spam"
    >>> print blah[/color][/color][/color]
    """

    import doctest, unittest
    suite = doctest.DocTest Suite()

    class TestCase(unitte st.TestCase):
    pass

    for index, test in enumerate(suite ._tests):
    setattr(TestCas e, "test_%d" % index, test)

    if __name__ == "__main__":
    if 1:
    import unittest
    unittest.main()
    else:
    import doctest
    doctest.testmod ()
    ---snip---
  • Jim Sizelove

    #2
    Re: Running doctests with unittest

    Thomas Heller wrote:[color=blue]
    > I'm trying to integrate some doctest tests with unittest. The tests
    > must be exposed as one or more subclasses of unittest.TestCa se, so I'm
    > collecting them with a call to doctest.DocTest Suite(), and then add them
    > to a TestCase class I have created.
    > The tests seem to run, but they always seem to succeed - I have no idea
    > why. Any ideas?
    >
    > Thomas
    >
    > ---snip---
    > """
    >[color=green][color=darkred]
    >>>>print "Hi"
    >>>>print 1213[/color][/color]
    >
    > """
    >
    > def func():
    > """[color=green][color=darkred]
    > >>> print "spam"
    > >>> print blah[/color][/color]
    > """
    >
    > import doctest, unittest
    > suite = doctest.DocTest Suite()
    >
    > class TestCase(unitte st.TestCase):
    > pass
    >
    > for index, test in enumerate(suite ._tests):
    > setattr(TestCas e, "test_%d" % index, test)
    >
    > if __name__ == "__main__":
    > if 1:
    > import unittest
    > unittest.main()
    > else:
    > import doctest
    > doctest.testmod ()
    > ---snip---[/color]

    I can't explain why all the tests seemed to pass, but I tried a
    different approach that works.

    Once you have a suite object, you just need to run it. The
    TextTestRunner works well for that. I got output errors and failures by
    doing the following:

    if __name__ == '__main__':
    import doctest, unittest
    suite = doctest.DocTest Suite()
    testRunner = unittest.TextTe stRunner()
    testRunner.run( suite)

    HTH,
    Jim Sizelove

    Comment

    Working...