Is it possible to run doctests using unit2
I recently switched from nose to the new unittest2 package for my python unit testing needs. It does everything I want, except from the fact that I can't get its "discover" command to recognize the doctests in my code - I still have to use nose to run them. Is this not implemented or is there som开发者_如何转开发ething I'm missing here?
Unit2 only discovers regular Python tests. In order to have it run your doctests, I'm afraid you would need to write some minimal boilerplate. Also: the upcoming plugin architecture will make it easy to automate some of these tasks.
In the meantime. you might want to take a look at tox (described here by unittest2 creator) http://www.voidspace.org.uk/python/weblog/arch_d7_2010_07_10.shtml
The boilerplate needed to tell unit2 about your doctests is actually given in the current doctest documentation, though it took me a few minutes to find it:
http://docs.python.org/library/doctest.html#unittest-api
Note that you can pass module names to the DocTestSuite
constructor instead of having to import the module yourself, which can cut the length of your boilerplate file in half; it just needs to look like:
from doctest import DocTestSuite
from unittest import TestSuite
def load_tests(loader, tests, pattern):
suite = TestSuite()
suite.addTests(DocTestSuite('my.module.one'))
suite.addTests(DocTestSuite('my.module.two'))
suite.addTests(DocTestSuite('my.module.three'))
return suite
精彩评论