Integrating doctest with unittest

SegundoBob SegundoBob at earthlink.net
Tue Jan 11 17:30:40 EST 2011


On Jan 9, 6:14 pm, Steven D'Aprano <steve
+comp.lang.pyt... at pearwood.info> wrote:

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

I only recently began using unittest, so I only know a little about
it.  There are almost certainly more clever ways to what you want, but
what I have done may satisfy you.

allTests.py:

import unittest

import PalmDS.test.test_tree_node as test_tree_node
import PalmDS.test.test_plugin_manager as test_plugin_manager
import PalmDS.test.test_ds_utils as test_ds_utils
import PalmDS.test.test_main as test_main
import PalmDS.test.test_root as test_root

all = unittest.TestSuite()
for module in [test_tree_node,
    test_plugin_manager,
    test_ds_utils,
    test_root,
    ]:
        all.addTest(module.suite())

if __name__ == '__main__':
    unittest.main()

Note:  This requires me to put a suite() function in every unittest
module, such as this
one from my test_tree_node.py module:

def suite():
    return unittest.TestLoader().loadTestsFromTestCase(TstTreeNode)

Note:  I must change TstTreeNode appropriately when I copy suite() to
a new module.

Terminal contents after a run:

bob at BobBuilt01:~/svnMyWork/PalmDS/test$ ./all_tests.py -v all
testDs2tree01 (PalmDS.test.test_tree_node.TstTreeNode) ... ok
testDs2tree02 (PalmDS.test.test_tree_node.TstTreeNode) ... ok
testPlug01 (PalmDS.test.test_plugin_manager.TstPluginManager) ... ok
testPlug02 (PalmDS.test.test_plugin_manager.TstPluginManager) ... ok
testBitstringBytes (PalmDS.test.test_ds_utils.TstDsUtils) ... ok
testComputeLoadDir (PalmDS.test.test_ds_utils.TstDsUtils) ... ok
testDs2fmtStr (PalmDS.test.test_ds_utils.TstDsUtils) ... ok
testPalmDateDecode (PalmDS.test.test_root.TstRoot) ... ok
testPalmDateEncode (PalmDS.test.test_root.TstRoot) ... ok

----------------------------------------------------------------------
Ran 9 tests in 0.016s

OK
bob at BobBuilt01:~/svnMyWork/PalmDS/test$

My guess at an answer to your specific question:
    At the end of allTests.py add
        all.addTest(doctest.DocTestSuite(module=module_to_test)))

Then I think your DocTest suite will be run with the unittest suites
when you specify "all" on the command line.



More information about the Python-list mailing list