Integrating doctest with unittest

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Dec 11 09:32:03 EST 2010


I have a module with doctests, and a module that performs unit testing 
for it. The test module looks like this:


import doctest
import unittest

import module_to_test

# ...
# many test suites
# ...

if __name__ == '__main__':
    doctest.testmod(module_to_test)
    unittest.main()



but now I'd like to integrate the doctests with the unittests. I thought 
I could follow the instructions here:

http://docs.python.org/py3k/library/doctest.html#unittest-api


so I added a line:


doc_test_suite = doctest.DocTestSuite(module=module_to_test)


expecting that it would be found by unittest.main(), but it is not. I 
imagine this is because DocTestSuite returns a TestSuite instance, while 
the unittest test finder only looks for classes.

I realise that I could manually run the doc_test_suite with this:

unittest.TextTestRunner().run(doc_test_suite)

but this leads to two test outputs:

    Ran 100 tests in 3.037s
    OK

    Ran 10 tests in 0.012s
    OK


instead of combining them:

    Ran 110 tests in 3.049s
    OK


Is there a way to have unittest.main() find and run doc_test_suite 
together with the other test suites?



-- 
Steven



More information about the Python-list mailing list