Integrating doctest with unittest

S

Steven D'Aprano

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?
 
A

Aahz

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

You probably need to use nose or something. (That's what we're doing.)
 
S

SegundoBob

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@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@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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top