Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Integrating doctest with unittest

Reply
Thread Tools

Integrating doctest with unittest

 
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-11-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/...l#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
 
Reply With Quote
 
 
 
 
Aahz
Guest
Posts: n/a
 
      01-09-2011
In article <4d038b63$0$30000$c3e8da3$ om>,
Steven D'Aprano <steve+> wrote:
>
>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.)
--
Aahz () <*> http://www.pythoncraft.com/

"Think of it as evolution in action." --Tony Rand
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-10-2011
On Sun, 09 Jan 2011 08:56:52 -0800, Aahz wrote:

> In article <4d038b63$0$30000$c3e8da3$ om>, Steven
> D'Aprano <steve+> wrote:
>>
>>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.)



Thanks for the reply Aahz, even though it wasn't what I wanted to hear


--
Steven
 
Reply With Quote
 
SegundoBob
Guest
Posts: n/a
 
      01-11-2011
On Jan 9, 6:14*pm, Steven D'Aprano <steve
+comp.lang.pyt...@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(TstTre eNode)

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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unittest - adding a doctest suite to unittest.main Paul Moore Python 1 10-14-2008 03:32 PM
doctest, unittest, or if __name__='__main__' john_sips_tea@yahoo.com Python 24 03-29-2006 10:41 AM
NEWBIE: doctest question engsolnom@ipns.com Python 0 01-05-2004 09:47 PM
ps2, IDLE, and doctest Paul M Python 0 10-17-2003 07:03 PM
Re: Simulte user input using doctest Steven Taschuk Python 0 06-27-2003 01:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57