Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Running doctests with unittest

Reply
Thread Tools

Running doctests with unittest

 
 
Thomas Heller
Guest
Posts: n/a
 
      03-09-2005
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---
 
Reply With Quote
 
 
 
 
Jim Sizelove
Guest
Posts: n/a
 
      03-09-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
doctests/unittest problem with exception Paul Python 1 01-27-2013 03:49 AM
pydb output vanishes when running nosetests doctests jcervidae Python 1 05-18-2009 05:35 PM
Unittest - adding a doctest suite to unittest.main Paul Moore Python 1 10-14-2008 03:32 PM
unittest.main-workalike that runs doctests too? John J. Lee Python 0 05-05-2006 10:35 AM
unicode in doctests Bjorn Tillenius Python 0 11-29-2004 06:27 PM



Advertisments