[TIP] Building a unittest.TestCase from a doctest

Titus Brown titus at caltech.edu
Tue Dec 18 21:59:30 PST 2007


-> >My real goal is to get the tests to *run* under the aegis of a  
-> >different module;
-> 
-> I don't know what you mean by that.

Here is a functioning example (source files attached, or download from

	http://iorich.caltech.edu/~t/transfer/doctest-ut.tar.gz

and run 'test.py', reproduced below):

---
import unittest
from doctest import DocTestSuite

### load doctests from another directory

import sys
sys.path.insert(0, 'other-directory')
import mod
doctest_suite = DocTestSuite(mod)

###

class MyTest(unittest.TestCase):
    def test_something(self):
        assert 1 == 1

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(MyTest)

    for t in doctest_suite:
        suite.addTest(t)

    unittest.TextTestRunner(verbosity=2).run(suite)
---

I would like to be able to achieve this same effect without explicitly
creating a TestSuite and adding tests to it individually.  For example,
code like this would let me simply call 'unittest.main()' because the
doctest test cases would be recognized as unittest.TestCase objects:

--
import mod
doctest_suite = DocTestSuite(mod)
testcase_mimicking_doctest_suite = Wrapper(doctest_suite)
---

I suspect there's no good way to do this, but I thought I'd ask ;)

cheers,
--titus
-------------- next part --------------
A non-text attachment was scrubbed...
Name: doctest-ut.tar.gz
Type: application/octet-stream
Size: 519 bytes
Desc: not available
Url : http://lists.idyll.org/pipermail/testing-in-python/attachments/20071218/f988968e/attachment.obj 


More information about the testing-in-python mailing list