Integrating doctest with unittest

Discussion in 'Python' started by Steven D'Aprano, Dec 11, 2010.

  1. 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
     
    Steven D'Aprano, Dec 11, 2010
    #1
    1. Advertising

  2. Steven D'Aprano

    Aahz Guest

    In article <4d038b63$0$30000$c3e8da3$>,
    Steven D'Aprano <> 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
     
    Aahz, Jan 9, 2011
    #2
    1. Advertising

  3. On Sun, 09 Jan 2011 08:56:52 -0800, Aahz wrote:

    > In article <4d038b63$0$30000$c3e8da3$>, Steven
    > D'Aprano <> 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
     
    Steven D'Aprano, Jan 10, 2011
    #3
  4. Steven D'Aprano

    SegundoBob Guest

    On Jan 9, 6:14 pm, 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?


    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.
     
    SegundoBob, Jan 11, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven Taschuk

    Re: Simulte user input using doctest

    Steven Taschuk, Jun 27, 2003, in forum: Python
    Replies:
    0
    Views:
    1,281
    Steven Taschuk
    Jun 27, 2003
  2. Paul M

    ps2, IDLE, and doctest

    Paul M, Oct 17, 2003, in forum: Python
    Replies:
    0
    Views:
    422
    Paul M
    Oct 17, 2003
  3. Replies:
    0
    Views:
    284
  4. Replies:
    24
    Views:
    818
    Ben Finney
    Mar 29, 2006
  5. Paul  Moore
    Replies:
    1
    Views:
    412
    Paul Moore
    Oct 14, 2008
Loading...

Share This Page