Running doctests with unittest

T

Thomas Heller

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---
""""""

def func():
""" """

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---
 
J

Jim Sizelove

Thomas said:
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---
"""


"""

def func():
"""
"""

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top