doctests.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. """Use the Doctest plugin with ``--with-doctest`` or the NOSE_WITH_DOCTEST
  2. environment variable to enable collection and execution of :mod:`doctests
  3. <doctest>`. Because doctests are usually included in the tested package
  4. (instead of being grouped into packages or modules of their own), nose only
  5. looks for them in the non-test packages it discovers in the working directory.
  6. Doctests may also be placed into files other than python modules, in which
  7. case they can be collected and executed by using the ``--doctest-extension``
  8. switch or NOSE_DOCTEST_EXTENSION environment variable to indicate which file
  9. extension(s) to load.
  10. When loading doctests from non-module files, use the ``--doctest-fixtures``
  11. switch to specify how to find modules containing fixtures for the tests. A
  12. module name will be produced by appending the value of that switch to the base
  13. name of each doctest file loaded. For example, a doctest file "widgets.rst"
  14. with the switch ``--doctest_fixtures=_fixt`` will load fixtures from the module
  15. ``widgets_fixt.py``.
  16. A fixtures module may define any or all of the following functions:
  17. * setup([module]) or setup_module([module])
  18. Called before the test runs. You may raise SkipTest to skip all tests.
  19. * teardown([module]) or teardown_module([module])
  20. Called after the test runs, if setup/setup_module did not raise an
  21. unhandled exception.
  22. * setup_test(test)
  23. Called before the test. NOTE: the argument passed is a
  24. doctest.DocTest instance, *not* a unittest.TestCase.
  25. * teardown_test(test)
  26. Called after the test, if setup_test did not raise an exception. NOTE: the
  27. argument passed is a doctest.DocTest instance, *not* a unittest.TestCase.
  28. Doctests are run like any other test, with the exception that output
  29. capture does not work; doctest does its own output capture while running a
  30. test.
  31. .. note ::
  32. See :doc:`../doc_tests/test_doctest_fixtures/doctest_fixtures` for
  33. additional documentation and examples.
  34. """
  35. from __future__ import generators
  36. import logging
  37. import os
  38. import sys
  39. import unittest
  40. from inspect import getmodule
  41. from nose.plugins.base import Plugin
  42. from nose.suite import ContextList
  43. from nose.util import anyp, getpackage, test_address, resolve_name, \
  44. src, tolist, isproperty
  45. try:
  46. from cStringIO import StringIO
  47. except ImportError:
  48. from StringIO import StringIO
  49. import sys
  50. import __builtin__ as builtin_mod
  51. log = logging.getLogger(__name__)
  52. try:
  53. import doctest
  54. doctest.DocTestCase
  55. # system version of doctest is acceptable, but needs a monkeypatch
  56. except (ImportError, AttributeError):
  57. # system version is too old
  58. import nose.ext.dtcompat as doctest
  59. #
  60. # Doctest and coverage don't get along, so we need to create
  61. # a monkeypatch that will replace the part of doctest that
  62. # interferes with coverage reports.
  63. #
  64. # The monkeypatch is based on this zope patch:
  65. # http://svn.zope.org/Zope3/trunk/src/zope/testing/doctest.py?rev=28679&r1=28703&r2=28705
  66. #
  67. _orp = doctest._OutputRedirectingPdb
  68. class NoseOutputRedirectingPdb(_orp):
  69. def __init__(self, out):
  70. self.__debugger_used = False
  71. _orp.__init__(self, out)
  72. def set_trace(self):
  73. self.__debugger_used = True
  74. _orp.set_trace(self, sys._getframe().f_back)
  75. def set_continue(self):
  76. # Calling set_continue unconditionally would break unit test
  77. # coverage reporting, as Bdb.set_continue calls sys.settrace(None).
  78. if self.__debugger_used:
  79. _orp.set_continue(self)
  80. doctest._OutputRedirectingPdb = NoseOutputRedirectingPdb
  81. class DoctestSuite(unittest.TestSuite):
  82. """
  83. Doctest suites are parallelizable at the module or file level only,
  84. since they may be attached to objects that are not individually
  85. addressable (like properties). This suite subclass is used when
  86. loading doctests from a module to ensure that behavior.
  87. This class is used only if the plugin is not fully prepared;
  88. in normal use, the loader's suiteClass is used.
  89. """
  90. can_split = False
  91. def __init__(self, tests=(), context=None, can_split=False):
  92. self.context = context
  93. self.can_split = can_split
  94. unittest.TestSuite.__init__(self, tests=tests)
  95. def address(self):
  96. return test_address(self.context)
  97. def __iter__(self):
  98. # 2.3 compat
  99. return iter(self._tests)
  100. def __str__(self):
  101. return str(self._tests)
  102. class Doctest(Plugin):
  103. """
  104. Activate doctest plugin to find and run doctests in non-test modules.
  105. """
  106. extension = None
  107. suiteClass = DoctestSuite
  108. def options(self, parser, env):
  109. """Register commmandline options.
  110. """
  111. Plugin.options(self, parser, env)
  112. parser.add_option('--doctest-tests', action='store_true',
  113. dest='doctest_tests',
  114. default=env.get('NOSE_DOCTEST_TESTS'),
  115. help="Also look for doctests in test modules. "
  116. "Note that classes, methods and functions should "
  117. "have either doctests or non-doctest tests, "
  118. "not both. [NOSE_DOCTEST_TESTS]")
  119. parser.add_option('--doctest-extension', action="append",
  120. dest="doctestExtension",
  121. metavar="EXT",
  122. help="Also look for doctests in files with "
  123. "this extension [NOSE_DOCTEST_EXTENSION]")
  124. parser.add_option('--doctest-result-variable',
  125. dest='doctest_result_var',
  126. default=env.get('NOSE_DOCTEST_RESULT_VAR'),
  127. metavar="VAR",
  128. help="Change the variable name set to the result of "
  129. "the last interpreter command from the default '_'. "
  130. "Can be used to avoid conflicts with the _() "
  131. "function used for text translation. "
  132. "[NOSE_DOCTEST_RESULT_VAR]")
  133. parser.add_option('--doctest-fixtures', action="store",
  134. dest="doctestFixtures",
  135. metavar="SUFFIX",
  136. help="Find fixtures for a doctest file in module "
  137. "with this name appended to the base name "
  138. "of the doctest file")
  139. parser.add_option('--doctest-options', action="append",
  140. dest="doctestOptions",
  141. metavar="OPTIONS",
  142. help="Specify options to pass to doctest. " +
  143. "Eg. '+ELLIPSIS,+NORMALIZE_WHITESPACE'")
  144. # Set the default as a list, if given in env; otherwise
  145. # an additional value set on the command line will cause
  146. # an error.
  147. env_setting = env.get('NOSE_DOCTEST_EXTENSION')
  148. if env_setting is not None:
  149. parser.set_defaults(doctestExtension=tolist(env_setting))
  150. def configure(self, options, config):
  151. """Configure plugin.
  152. """
  153. Plugin.configure(self, options, config)
  154. self.doctest_result_var = options.doctest_result_var
  155. self.doctest_tests = options.doctest_tests
  156. self.extension = tolist(options.doctestExtension)
  157. self.fixtures = options.doctestFixtures
  158. self.finder = doctest.DocTestFinder()
  159. self.optionflags = 0
  160. if options.doctestOptions:
  161. flags = ",".join(options.doctestOptions).split(',')
  162. for flag in flags:
  163. if not flag or flag[0] not in '+-':
  164. raise ValueError(
  165. "Must specify doctest options with starting " +
  166. "'+' or '-'. Got %s" % (flag,))
  167. mode, option_name = flag[0], flag[1:]
  168. option_flag = doctest.OPTIONFLAGS_BY_NAME.get(option_name)
  169. if not option_flag:
  170. raise ValueError("Unknown doctest option %s" %
  171. (option_name,))
  172. if mode == '+':
  173. self.optionflags |= option_flag
  174. elif mode == '-':
  175. self.optionflags &= ~option_flag
  176. def prepareTestLoader(self, loader):
  177. """Capture loader's suiteClass.
  178. This is used to create test suites from doctest files.
  179. """
  180. self.suiteClass = loader.suiteClass
  181. def loadTestsFromModule(self, module):
  182. """Load doctests from the module.
  183. """
  184. log.debug("loading from %s", module)
  185. if not self.matches(module.__name__):
  186. log.debug("Doctest doesn't want module %s", module)
  187. return
  188. try:
  189. tests = self.finder.find(module)
  190. except AttributeError:
  191. log.exception("Attribute error loading from %s", module)
  192. # nose allows module.__test__ = False; doctest does not and throws
  193. # AttributeError
  194. return
  195. if not tests:
  196. log.debug("No tests found in %s", module)
  197. return
  198. tests.sort()
  199. module_file = src(module.__file__)
  200. # FIXME this breaks the id plugin somehow (tests probably don't
  201. # get wrapped in result proxy or something)
  202. cases = []
  203. for test in tests:
  204. if not test.examples:
  205. continue
  206. if not test.filename:
  207. test.filename = module_file
  208. cases.append(DocTestCase(test,
  209. optionflags=self.optionflags,
  210. result_var=self.doctest_result_var))
  211. if cases:
  212. yield self.suiteClass(cases, context=module, can_split=False)
  213. def loadTestsFromFile(self, filename):
  214. """Load doctests from the file.
  215. Tests are loaded only if filename's extension matches
  216. configured doctest extension.
  217. """
  218. if self.extension and anyp(filename.endswith, self.extension):
  219. name = os.path.basename(filename)
  220. dh = open(filename)
  221. try:
  222. doc = dh.read()
  223. finally:
  224. dh.close()
  225. fixture_context = None
  226. globs = {'__file__': filename}
  227. if self.fixtures:
  228. base, ext = os.path.splitext(name)
  229. dirname = os.path.dirname(filename)
  230. sys.path.append(dirname)
  231. fixt_mod = base + self.fixtures
  232. try:
  233. fixture_context = __import__(
  234. fixt_mod, globals(), locals(), ["nop"])
  235. except ImportError, e:
  236. log.debug(
  237. "Could not import %s: %s (%s)", fixt_mod, e, sys.path)
  238. log.debug("Fixture module %s resolved to %s",
  239. fixt_mod, fixture_context)
  240. if hasattr(fixture_context, 'globs'):
  241. globs = fixture_context.globs(globs)
  242. parser = doctest.DocTestParser()
  243. test = parser.get_doctest(
  244. doc, globs=globs, name=name,
  245. filename=filename, lineno=0)
  246. if test.examples:
  247. case = DocFileCase(
  248. test,
  249. optionflags=self.optionflags,
  250. setUp=getattr(fixture_context, 'setup_test', None),
  251. tearDown=getattr(fixture_context, 'teardown_test', None),
  252. result_var=self.doctest_result_var)
  253. if fixture_context:
  254. yield ContextList((case,), context=fixture_context)
  255. else:
  256. yield case
  257. else:
  258. yield False # no tests to load
  259. def makeTest(self, obj, parent):
  260. """Look for doctests in the given object, which will be a
  261. function, method or class.
  262. """
  263. name = getattr(obj, '__name__', 'Unnammed %s' % type(obj))
  264. doctests = self.finder.find(obj, module=getmodule(parent), name=name)
  265. if doctests:
  266. for test in doctests:
  267. if len(test.examples) == 0:
  268. continue
  269. yield DocTestCase(test, obj=obj, optionflags=self.optionflags,
  270. result_var=self.doctest_result_var)
  271. def matches(self, name):
  272. # FIXME this seems wrong -- nothing is ever going to
  273. # fail this test, since we're given a module NAME not FILE
  274. if name == '__init__.py':
  275. return False
  276. # FIXME don't think we need include/exclude checks here?
  277. return ((self.doctest_tests or not self.conf.testMatch.search(name)
  278. or (self.conf.include
  279. and filter(None,
  280. [inc.search(name)
  281. for inc in self.conf.include])))
  282. and (not self.conf.exclude
  283. or not filter(None,
  284. [exc.search(name)
  285. for exc in self.conf.exclude])))
  286. def wantFile(self, file):
  287. """Override to select all modules and any file ending with
  288. configured doctest extension.
  289. """
  290. # always want .py files
  291. if file.endswith('.py'):
  292. return True
  293. # also want files that match my extension
  294. if (self.extension
  295. and anyp(file.endswith, self.extension)
  296. and (not self.conf.exclude
  297. or not filter(None,
  298. [exc.search(file)
  299. for exc in self.conf.exclude]))):
  300. return True
  301. return None
  302. class DocTestCase(doctest.DocTestCase):
  303. """Overrides DocTestCase to
  304. provide an address() method that returns the correct address for
  305. the doctest case. To provide hints for address(), an obj may also
  306. be passed -- this will be used as the test object for purposes of
  307. determining the test address, if it is provided.
  308. """
  309. def __init__(self, test, optionflags=0, setUp=None, tearDown=None,
  310. checker=None, obj=None, result_var='_'):
  311. self._result_var = result_var
  312. self._nose_obj = obj
  313. super(DocTestCase, self).__init__(
  314. test, optionflags=optionflags, setUp=setUp, tearDown=tearDown,
  315. checker=checker)
  316. def address(self):
  317. if self._nose_obj is not None:
  318. return test_address(self._nose_obj)
  319. obj = resolve_name(self._dt_test.name)
  320. if isproperty(obj):
  321. # properties have no connection to the class they are in
  322. # so we can't just look 'em up, we have to first look up
  323. # the class, then stick the prop on the end
  324. parts = self._dt_test.name.split('.')
  325. class_name = '.'.join(parts[:-1])
  326. cls = resolve_name(class_name)
  327. base_addr = test_address(cls)
  328. return (base_addr[0], base_addr[1],
  329. '.'.join([base_addr[2], parts[-1]]))
  330. else:
  331. return test_address(obj)
  332. # doctests loaded via find(obj) omit the module name
  333. # so we need to override id, __repr__ and shortDescription
  334. # bonus: this will squash a 2.3 vs 2.4 incompatiblity
  335. def id(self):
  336. name = self._dt_test.name
  337. filename = self._dt_test.filename
  338. if filename is not None:
  339. pk = getpackage(filename)
  340. if pk is None:
  341. return name
  342. if not name.startswith(pk):
  343. name = "%s.%s" % (pk, name)
  344. return name
  345. def __repr__(self):
  346. name = self.id()
  347. name = name.split('.')
  348. return "%s (%s)" % (name[-1], '.'.join(name[:-1]))
  349. __str__ = __repr__
  350. def shortDescription(self):
  351. return 'Doctest: %s' % self.id()
  352. def setUp(self):
  353. if self._result_var is not None:
  354. self._old_displayhook = sys.displayhook
  355. sys.displayhook = self._displayhook
  356. super(DocTestCase, self).setUp()
  357. def _displayhook(self, value):
  358. if value is None:
  359. return
  360. setattr(builtin_mod, self._result_var, value)
  361. print repr(value)
  362. def tearDown(self):
  363. super(DocTestCase, self).tearDown()
  364. if self._result_var is not None:
  365. sys.displayhook = self._old_displayhook
  366. delattr(builtin_mod, self._result_var)
  367. class DocFileCase(doctest.DocFileCase):
  368. """Overrides to provide address() method that returns the correct
  369. address for the doc file case.
  370. """
  371. def __init__(self, test, optionflags=0, setUp=None, tearDown=None,
  372. checker=None, result_var='_'):
  373. self._result_var = result_var
  374. super(DocFileCase, self).__init__(
  375. test, optionflags=optionflags, setUp=setUp, tearDown=tearDown,
  376. checker=None)
  377. def address(self):
  378. return (self._dt_test.filename, None, None)
  379. def setUp(self):
  380. if self._result_var is not None:
  381. self._old_displayhook = sys.displayhook
  382. sys.displayhook = self._displayhook
  383. super(DocFileCase, self).setUp()
  384. def _displayhook(self, value):
  385. if value is None:
  386. return
  387. setattr(builtin_mod, self._result_var, value)
  388. print repr(value)
  389. def tearDown(self):
  390. super(DocFileCase, self).tearDown()
  391. if self._result_var is not None:
  392. sys.displayhook = self._old_displayhook
  393. delattr(builtin_mod, self._result_var)