Running doctests with unittest

Discussion in 'Python' started by Thomas Heller, Mar 9, 2005.

  1. I'm trying to integrate some doctest tests with unittest. The tests
    must be exposed as one or more subclasses of unittest.TestCase, so I'm
    collecting them with a call to doctest.DocTestSuite(), 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---
    """
    >>> print "Hi"
    >>> print 1213

    """

    def func():
    """
    >>> print "spam"
    >>> print blah

    """

    import doctest, unittest
    suite = doctest.DocTestSuite()

    class TestCase(unittest.TestCase):
    pass

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

    if __name__ == "__main__":
    if 1:
    import unittest
    unittest.main()
    else:
    import doctest
    doctest.testmod()
    ---snip---
    Thomas Heller, Mar 9, 2005
    #1
    1. Advertising

  2. Thomas Heller

    Jim Sizelove Guest

    Thomas Heller wrote:
    > I'm trying to integrate some doctest tests with unittest. The tests
    > must be exposed as one or more subclasses of unittest.TestCase, so I'm
    > collecting them with a call to doctest.DocTestSuite(), 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---
    > """
    >
    >>>>print "Hi"
    >>>>print 1213

    >
    > """
    >
    > def func():
    > """
    > >>> print "spam"
    > >>> print blah

    > """
    >
    > import doctest, unittest
    > suite = doctest.DocTestSuite()
    >
    > class TestCase(unittest.TestCase):
    > pass
    >
    > for index, test in enumerate(suite._tests):
    > setattr(TestCase, "test_%d" % index, test)
    >
    > if __name__ == "__main__":
    > if 1:
    > import unittest
    > unittest.main()
    > else:
    > import doctest
    > doctest.testmod()
    > ---snip---


    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.DocTestSuite()
    testRunner = unittest.TextTestRunner()
    testRunner.run(suite)

    HTH,
    Jim Sizelove
    Jim Sizelove, Mar 9, 2005
    #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. Bjorn Tillenius

    unicode in doctests

    Bjorn Tillenius, Nov 29, 2004, in forum: Python
    Replies:
    0
    Views:
    255
    Bjorn Tillenius
    Nov 29, 2004
  2. John J. Lee
    Replies:
    0
    Views:
    259
    John J. Lee
    May 5, 2006
  3. Paul  Moore
    Replies:
    1
    Views:
    384
    Paul Moore
    Oct 14, 2008
  4. jcervidae
    Replies:
    1
    Views:
    274
    Diez B. Roggisch
    May 18, 2009
  5. Paul
    Replies:
    1
    Views:
    122
    Steven D'Aprano
    Jan 27, 2013
Loading...

Share This Page