PageRenderTime 70ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/IronPython/27/Lib/test/test_doctest.py

http://github.com/IronLanguages/main
Python | 2756 lines | 2692 code | 10 blank | 54 comment | 0 complexity | 1a1b0d1fc632aa650040ede252f2345d MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception

Large files files are truncated, but you can click here to view the full file

  1. # -*- coding: utf-8 -*-
  2. """
  3. Test script for doctest.
  4. """
  5. import sys
  6. from test import test_support
  7. import doctest
  8. # NOTE: There are some additional tests relating to interaction with
  9. # zipimport in the test_zipimport_support test module.
  10. ######################################################################
  11. ## Sample Objects (used by test cases)
  12. ######################################################################
  13. def sample_func(v):
  14. """
  15. Blah blah
  16. >>> print sample_func(22)
  17. 44
  18. Yee ha!
  19. """
  20. return v+v
  21. class SampleClass:
  22. """
  23. >>> print 1
  24. 1
  25. >>> # comments get ignored. so are empty PS1 and PS2 prompts:
  26. >>>
  27. ...
  28. Multiline example:
  29. >>> sc = SampleClass(3)
  30. >>> for i in range(10):
  31. ... sc = sc.double()
  32. ... print sc.get(),
  33. 6 12 24 48 96 192 384 768 1536 3072
  34. """
  35. def __init__(self, val):
  36. """
  37. >>> print SampleClass(12).get()
  38. 12
  39. """
  40. self.val = val
  41. def double(self):
  42. """
  43. >>> print SampleClass(12).double().get()
  44. 24
  45. """
  46. return SampleClass(self.val + self.val)
  47. def get(self):
  48. """
  49. >>> print SampleClass(-5).get()
  50. -5
  51. """
  52. return self.val
  53. def a_staticmethod(v):
  54. """
  55. >>> print SampleClass.a_staticmethod(10)
  56. 11
  57. """
  58. return v+1
  59. a_staticmethod = staticmethod(a_staticmethod)
  60. def a_classmethod(cls, v):
  61. """
  62. >>> print SampleClass.a_classmethod(10)
  63. 12
  64. >>> print SampleClass(0).a_classmethod(10)
  65. 12
  66. """
  67. return v+2
  68. a_classmethod = classmethod(a_classmethod)
  69. a_property = property(get, doc="""
  70. >>> print SampleClass(22).a_property
  71. 22
  72. """)
  73. class NestedClass:
  74. """
  75. >>> x = SampleClass.NestedClass(5)
  76. >>> y = x.square()
  77. >>> print y.get()
  78. 25
  79. """
  80. def __init__(self, val=0):
  81. """
  82. >>> print SampleClass.NestedClass().get()
  83. 0
  84. """
  85. self.val = val
  86. def square(self):
  87. return SampleClass.NestedClass(self.val*self.val)
  88. def get(self):
  89. return self.val
  90. class SampleNewStyleClass(object):
  91. r"""
  92. >>> print '1\n2\n3'
  93. 1
  94. 2
  95. 3
  96. """
  97. def __init__(self, val):
  98. """
  99. >>> print SampleNewStyleClass(12).get()
  100. 12
  101. """
  102. self.val = val
  103. def double(self):
  104. """
  105. >>> print SampleNewStyleClass(12).double().get()
  106. 24
  107. """
  108. return SampleNewStyleClass(self.val + self.val)
  109. def get(self):
  110. """
  111. >>> print SampleNewStyleClass(-5).get()
  112. -5
  113. """
  114. return self.val
  115. ######################################################################
  116. ## Fake stdin (for testing interactive debugging)
  117. ######################################################################
  118. class _FakeInput:
  119. """
  120. A fake input stream for pdb's interactive debugger. Whenever a
  121. line is read, print it (to simulate the user typing it), and then
  122. return it. The set of lines to return is specified in the
  123. constructor; they should not have trailing newlines.
  124. """
  125. def __init__(self, lines):
  126. self.lines = lines
  127. def readline(self):
  128. line = self.lines.pop(0)
  129. print line
  130. return line+'\n'
  131. ######################################################################
  132. ## Test Cases
  133. ######################################################################
  134. def test_Example(): r"""
  135. Unit tests for the `Example` class.
  136. Example is a simple container class that holds:
  137. - `source`: A source string.
  138. - `want`: An expected output string.
  139. - `exc_msg`: An expected exception message string (or None if no
  140. exception is expected).
  141. - `lineno`: A line number (within the docstring).
  142. - `indent`: The example's indentation in the input string.
  143. - `options`: An option dictionary, mapping option flags to True or
  144. False.
  145. These attributes are set by the constructor. `source` and `want` are
  146. required; the other attributes all have default values:
  147. >>> example = doctest.Example('print 1', '1\n')
  148. >>> (example.source, example.want, example.exc_msg,
  149. ... example.lineno, example.indent, example.options)
  150. ('print 1\n', '1\n', None, 0, 0, {})
  151. The first three attributes (`source`, `want`, and `exc_msg`) may be
  152. specified positionally; the remaining arguments should be specified as
  153. keyword arguments:
  154. >>> exc_msg = 'IndexError: pop from an empty list'
  155. >>> example = doctest.Example('[].pop()', '', exc_msg,
  156. ... lineno=5, indent=4,
  157. ... options={doctest.ELLIPSIS: True})
  158. >>> (example.source, example.want, example.exc_msg,
  159. ... example.lineno, example.indent, example.options)
  160. ('[].pop()\n', '', 'IndexError: pop from an empty list\n', 5, 4, {8: True})
  161. The constructor normalizes the `source` string to end in a newline:
  162. Source spans a single line: no terminating newline.
  163. >>> e = doctest.Example('print 1', '1\n')
  164. >>> e.source, e.want
  165. ('print 1\n', '1\n')
  166. >>> e = doctest.Example('print 1\n', '1\n')
  167. >>> e.source, e.want
  168. ('print 1\n', '1\n')
  169. Source spans multiple lines: require terminating newline.
  170. >>> e = doctest.Example('print 1;\nprint 2\n', '1\n2\n')
  171. >>> e.source, e.want
  172. ('print 1;\nprint 2\n', '1\n2\n')
  173. >>> e = doctest.Example('print 1;\nprint 2', '1\n2\n')
  174. >>> e.source, e.want
  175. ('print 1;\nprint 2\n', '1\n2\n')
  176. Empty source string (which should never appear in real examples)
  177. >>> e = doctest.Example('', '')
  178. >>> e.source, e.want
  179. ('\n', '')
  180. The constructor normalizes the `want` string to end in a newline,
  181. unless it's the empty string:
  182. >>> e = doctest.Example('print 1', '1\n')
  183. >>> e.source, e.want
  184. ('print 1\n', '1\n')
  185. >>> e = doctest.Example('print 1', '1')
  186. >>> e.source, e.want
  187. ('print 1\n', '1\n')
  188. >>> e = doctest.Example('print', '')
  189. >>> e.source, e.want
  190. ('print\n', '')
  191. The constructor normalizes the `exc_msg` string to end in a newline,
  192. unless it's `None`:
  193. Message spans one line
  194. >>> exc_msg = 'IndexError: pop from an empty list'
  195. >>> e = doctest.Example('[].pop()', '', exc_msg)
  196. >>> e.exc_msg
  197. 'IndexError: pop from an empty list\n'
  198. >>> exc_msg = 'IndexError: pop from an empty list\n'
  199. >>> e = doctest.Example('[].pop()', '', exc_msg)
  200. >>> e.exc_msg
  201. 'IndexError: pop from an empty list\n'
  202. Message spans multiple lines
  203. >>> exc_msg = 'ValueError: 1\n 2'
  204. >>> e = doctest.Example('raise ValueError("1\n 2")', '', exc_msg)
  205. >>> e.exc_msg
  206. 'ValueError: 1\n 2\n'
  207. >>> exc_msg = 'ValueError: 1\n 2\n'
  208. >>> e = doctest.Example('raise ValueError("1\n 2")', '', exc_msg)
  209. >>> e.exc_msg
  210. 'ValueError: 1\n 2\n'
  211. Empty (but non-None) exception message (which should never appear
  212. in real examples)
  213. >>> exc_msg = ''
  214. >>> e = doctest.Example('raise X()', '', exc_msg)
  215. >>> e.exc_msg
  216. '\n'
  217. Compare `Example`:
  218. >>> example = doctest.Example('print 1', '1\n')
  219. >>> same_example = doctest.Example('print 1', '1\n')
  220. >>> other_example = doctest.Example('print 42', '42\n')
  221. >>> example == same_example
  222. True
  223. >>> example != same_example
  224. False
  225. >>> hash(example) == hash(same_example)
  226. True
  227. >>> example == other_example
  228. False
  229. >>> example != other_example
  230. True
  231. """
  232. def test_DocTest(): r"""
  233. Unit tests for the `DocTest` class.
  234. DocTest is a collection of examples, extracted from a docstring, along
  235. with information about where the docstring comes from (a name,
  236. filename, and line number). The docstring is parsed by the `DocTest`
  237. constructor:
  238. >>> docstring = '''
  239. ... >>> print 12
  240. ... 12
  241. ...
  242. ... Non-example text.
  243. ...
  244. ... >>> print 'another\example'
  245. ... another
  246. ... example
  247. ... '''
  248. >>> globs = {} # globals to run the test in.
  249. >>> parser = doctest.DocTestParser()
  250. >>> test = parser.get_doctest(docstring, globs, 'some_test',
  251. ... 'some_file', 20)
  252. >>> print test
  253. <DocTest some_test from some_file:20 (2 examples)>
  254. >>> len(test.examples)
  255. 2
  256. >>> e1, e2 = test.examples
  257. >>> (e1.source, e1.want, e1.lineno)
  258. ('print 12\n', '12\n', 1)
  259. >>> (e2.source, e2.want, e2.lineno)
  260. ("print 'another\\example'\n", 'another\nexample\n', 6)
  261. Source information (name, filename, and line number) is available as
  262. attributes on the doctest object:
  263. >>> (test.name, test.filename, test.lineno)
  264. ('some_test', 'some_file', 20)
  265. The line number of an example within its containing file is found by
  266. adding the line number of the example and the line number of its
  267. containing test:
  268. >>> test.lineno + e1.lineno
  269. 21
  270. >>> test.lineno + e2.lineno
  271. 26
  272. If the docstring contains inconsistent leading whitespace in the
  273. expected output of an example, then `DocTest` will raise a ValueError:
  274. >>> docstring = r'''
  275. ... >>> print 'bad\nindentation'
  276. ... bad
  277. ... indentation
  278. ... '''
  279. >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0)
  280. Traceback (most recent call last):
  281. ValueError: line 4 of the docstring for some_test has inconsistent leading whitespace: 'indentation'
  282. If the docstring contains inconsistent leading whitespace on
  283. continuation lines, then `DocTest` will raise a ValueError:
  284. >>> docstring = r'''
  285. ... >>> print ('bad indentation',
  286. ... ... 2)
  287. ... ('bad', 'indentation')
  288. ... '''
  289. >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0)
  290. Traceback (most recent call last):
  291. ValueError: line 2 of the docstring for some_test has inconsistent leading whitespace: '... 2)'
  292. If there's no blank space after a PS1 prompt ('>>>'), then `DocTest`
  293. will raise a ValueError:
  294. >>> docstring = '>>>print 1\n1'
  295. >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0)
  296. Traceback (most recent call last):
  297. ValueError: line 1 of the docstring for some_test lacks blank after >>>: '>>>print 1'
  298. If there's no blank space after a PS2 prompt ('...'), then `DocTest`
  299. will raise a ValueError:
  300. >>> docstring = '>>> if 1:\n...print 1\n1'
  301. >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0)
  302. Traceback (most recent call last):
  303. ValueError: line 2 of the docstring for some_test lacks blank after ...: '...print 1'
  304. Compare `DocTest`:
  305. >>> docstring = '''
  306. ... >>> print 12
  307. ... 12
  308. ... '''
  309. >>> test = parser.get_doctest(docstring, globs, 'some_test',
  310. ... 'some_test', 20)
  311. >>> same_test = parser.get_doctest(docstring, globs, 'some_test',
  312. ... 'some_test', 20)
  313. >>> test == same_test
  314. True
  315. >>> test != same_test
  316. False
  317. >>> hash(test) == hash(same_test)
  318. True
  319. >>> docstring = '''
  320. ... >>> print 42
  321. ... 42
  322. ... '''
  323. >>> other_test = parser.get_doctest(docstring, globs, 'other_test',
  324. ... 'other_file', 10)
  325. >>> test == other_test
  326. False
  327. >>> test != other_test
  328. True
  329. Compare `DocTestCase`:
  330. >>> DocTestCase = doctest.DocTestCase
  331. >>> test_case = DocTestCase(test)
  332. >>> same_test_case = DocTestCase(same_test)
  333. >>> other_test_case = DocTestCase(other_test)
  334. >>> test_case == same_test_case
  335. True
  336. >>> test_case != same_test_case
  337. False
  338. >>> hash(test_case) == hash(same_test_case)
  339. True
  340. >>> test == other_test_case
  341. False
  342. >>> test != other_test_case
  343. True
  344. """
  345. def test_DocTestFinder(): r"""
  346. Unit tests for the `DocTestFinder` class.
  347. DocTestFinder is used to extract DocTests from an object's docstring
  348. and the docstrings of its contained objects. It can be used with
  349. modules, functions, classes, methods, staticmethods, classmethods, and
  350. properties.
  351. Finding Tests in Functions
  352. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  353. For a function whose docstring contains examples, DocTestFinder.find()
  354. will return a single test (for that function's docstring):
  355. >>> finder = doctest.DocTestFinder()
  356. We'll simulate a __file__ attr that ends in pyc:
  357. >>> import test.test_doctest
  358. >>> old = test.test_doctest.__file__
  359. >>> test.test_doctest.__file__ = 'test_doctest.pyc'
  360. >>> tests = finder.find(sample_func)
  361. >>> print tests # doctest: +ELLIPSIS
  362. [<DocTest sample_func from ...:17 (1 example)>]
  363. The exact name depends on how test_doctest was invoked, so allow for
  364. leading path components.
  365. >>> tests[0].filename # doctest: +ELLIPSIS
  366. '...test_doctest.py'
  367. >>> test.test_doctest.__file__ = old
  368. >>> e = tests[0].examples[0]
  369. >>> (e.source, e.want, e.lineno)
  370. ('print sample_func(22)\n', '44\n', 3)
  371. By default, tests are created for objects with no docstring:
  372. >>> def no_docstring(v):
  373. ... pass
  374. >>> finder.find(no_docstring)
  375. []
  376. However, the optional argument `exclude_empty` to the DocTestFinder
  377. constructor can be used to exclude tests for objects with empty
  378. docstrings:
  379. >>> def no_docstring(v):
  380. ... pass
  381. >>> excl_empty_finder = doctest.DocTestFinder(exclude_empty=True)
  382. >>> excl_empty_finder.find(no_docstring)
  383. []
  384. If the function has a docstring with no examples, then a test with no
  385. examples is returned. (This lets `DocTestRunner` collect statistics
  386. about which functions have no tests -- but is that useful? And should
  387. an empty test also be created when there's no docstring?)
  388. >>> def no_examples(v):
  389. ... ''' no doctest examples '''
  390. >>> finder.find(no_examples) # doctest: +ELLIPSIS
  391. [<DocTest no_examples from ...:1 (no examples)>]
  392. Finding Tests in Classes
  393. ~~~~~~~~~~~~~~~~~~~~~~~~
  394. For a class, DocTestFinder will create a test for the class's
  395. docstring, and will recursively explore its contents, including
  396. methods, classmethods, staticmethods, properties, and nested classes.
  397. >>> finder = doctest.DocTestFinder()
  398. >>> tests = finder.find(SampleClass)
  399. >>> for t in tests:
  400. ... print '%2s %s' % (len(t.examples), t.name)
  401. 3 SampleClass
  402. 3 SampleClass.NestedClass
  403. 1 SampleClass.NestedClass.__init__
  404. 1 SampleClass.__init__
  405. 2 SampleClass.a_classmethod
  406. 1 SampleClass.a_property
  407. 1 SampleClass.a_staticmethod
  408. 1 SampleClass.double
  409. 1 SampleClass.get
  410. New-style classes are also supported:
  411. >>> tests = finder.find(SampleNewStyleClass)
  412. >>> for t in tests:
  413. ... print '%2s %s' % (len(t.examples), t.name)
  414. 1 SampleNewStyleClass
  415. 1 SampleNewStyleClass.__init__
  416. 1 SampleNewStyleClass.double
  417. 1 SampleNewStyleClass.get
  418. Finding Tests in Modules
  419. ~~~~~~~~~~~~~~~~~~~~~~~~
  420. For a module, DocTestFinder will create a test for the class's
  421. docstring, and will recursively explore its contents, including
  422. functions, classes, and the `__test__` dictionary, if it exists:
  423. >>> # A module
  424. >>> import types
  425. >>> m = types.ModuleType('some_module')
  426. >>> def triple(val):
  427. ... '''
  428. ... >>> print triple(11)
  429. ... 33
  430. ... '''
  431. ... return val*3
  432. >>> m.__dict__.update({
  433. ... 'sample_func': sample_func,
  434. ... 'SampleClass': SampleClass,
  435. ... '__doc__': '''
  436. ... Module docstring.
  437. ... >>> print 'module'
  438. ... module
  439. ... ''',
  440. ... '__test__': {
  441. ... 'd': '>>> print 6\n6\n>>> print 7\n7\n',
  442. ... 'c': triple}})
  443. >>> finder = doctest.DocTestFinder()
  444. >>> # Use module=test.test_doctest, to prevent doctest from
  445. >>> # ignoring the objects since they weren't defined in m.
  446. >>> import test.test_doctest
  447. >>> tests = finder.find(m, module=test.test_doctest)
  448. >>> for t in tests:
  449. ... print '%2s %s' % (len(t.examples), t.name)
  450. 1 some_module
  451. 3 some_module.SampleClass
  452. 3 some_module.SampleClass.NestedClass
  453. 1 some_module.SampleClass.NestedClass.__init__
  454. 1 some_module.SampleClass.__init__
  455. 2 some_module.SampleClass.a_classmethod
  456. 1 some_module.SampleClass.a_property
  457. 1 some_module.SampleClass.a_staticmethod
  458. 1 some_module.SampleClass.double
  459. 1 some_module.SampleClass.get
  460. 1 some_module.__test__.c
  461. 2 some_module.__test__.d
  462. 1 some_module.sample_func
  463. Duplicate Removal
  464. ~~~~~~~~~~~~~~~~~
  465. If a single object is listed twice (under different names), then tests
  466. will only be generated for it once:
  467. >>> from test import doctest_aliases
  468. >>> assert doctest_aliases.TwoNames.f
  469. >>> assert doctest_aliases.TwoNames.g
  470. >>> tests = excl_empty_finder.find(doctest_aliases)
  471. >>> print len(tests)
  472. 2
  473. >>> print tests[0].name
  474. test.doctest_aliases.TwoNames
  475. TwoNames.f and TwoNames.g are bound to the same object.
  476. We can't guess which will be found in doctest's traversal of
  477. TwoNames.__dict__ first, so we have to allow for either.
  478. >>> tests[1].name.split('.')[-1] in ['f', 'g']
  479. True
  480. Empty Tests
  481. ~~~~~~~~~~~
  482. By default, an object with no doctests doesn't create any tests:
  483. >>> tests = doctest.DocTestFinder().find(SampleClass)
  484. >>> for t in tests:
  485. ... print '%2s %s' % (len(t.examples), t.name)
  486. 3 SampleClass
  487. 3 SampleClass.NestedClass
  488. 1 SampleClass.NestedClass.__init__
  489. 1 SampleClass.__init__
  490. 2 SampleClass.a_classmethod
  491. 1 SampleClass.a_property
  492. 1 SampleClass.a_staticmethod
  493. 1 SampleClass.double
  494. 1 SampleClass.get
  495. By default, that excluded objects with no doctests. exclude_empty=False
  496. tells it to include (empty) tests for objects with no doctests. This feature
  497. is really to support backward compatibility in what doctest.master.summarize()
  498. displays.
  499. >>> tests = doctest.DocTestFinder(exclude_empty=False).find(SampleClass)
  500. >>> for t in tests:
  501. ... print '%2s %s' % (len(t.examples), t.name)
  502. 3 SampleClass
  503. 3 SampleClass.NestedClass
  504. 1 SampleClass.NestedClass.__init__
  505. 0 SampleClass.NestedClass.get
  506. 0 SampleClass.NestedClass.square
  507. 1 SampleClass.__init__
  508. 2 SampleClass.a_classmethod
  509. 1 SampleClass.a_property
  510. 1 SampleClass.a_staticmethod
  511. 1 SampleClass.double
  512. 1 SampleClass.get
  513. Turning off Recursion
  514. ~~~~~~~~~~~~~~~~~~~~~
  515. DocTestFinder can be told not to look for tests in contained objects
  516. using the `recurse` flag:
  517. >>> tests = doctest.DocTestFinder(recurse=False).find(SampleClass)
  518. >>> for t in tests:
  519. ... print '%2s %s' % (len(t.examples), t.name)
  520. 3 SampleClass
  521. Line numbers
  522. ~~~~~~~~~~~~
  523. DocTestFinder finds the line number of each example:
  524. >>> def f(x):
  525. ... '''
  526. ... >>> x = 12
  527. ...
  528. ... some text
  529. ...
  530. ... >>> # examples are not created for comments & bare prompts.
  531. ... >>>
  532. ... ...
  533. ...
  534. ... >>> for x in range(10):
  535. ... ... print x,
  536. ... 0 1 2 3 4 5 6 7 8 9
  537. ... >>> x//2
  538. ... 6
  539. ... '''
  540. >>> test = doctest.DocTestFinder().find(f)[0]
  541. >>> [e.lineno for e in test.examples]
  542. [1, 9, 12]
  543. """
  544. def test_DocTestParser(): r"""
  545. Unit tests for the `DocTestParser` class.
  546. DocTestParser is used to parse docstrings containing doctest examples.
  547. The `parse` method divides a docstring into examples and intervening
  548. text:
  549. >>> s = '''
  550. ... >>> x, y = 2, 3 # no output expected
  551. ... >>> if 1:
  552. ... ... print x
  553. ... ... print y
  554. ... 2
  555. ... 3
  556. ...
  557. ... Some text.
  558. ... >>> x+y
  559. ... 5
  560. ... '''
  561. >>> parser = doctest.DocTestParser()
  562. >>> for piece in parser.parse(s):
  563. ... if isinstance(piece, doctest.Example):
  564. ... print 'Example:', (piece.source, piece.want, piece.lineno)
  565. ... else:
  566. ... print ' Text:', `piece`
  567. Text: '\n'
  568. Example: ('x, y = 2, 3 # no output expected\n', '', 1)
  569. Text: ''
  570. Example: ('if 1:\n print x\n print y\n', '2\n3\n', 2)
  571. Text: '\nSome text.\n'
  572. Example: ('x+y\n', '5\n', 9)
  573. Text: ''
  574. The `get_examples` method returns just the examples:
  575. >>> for piece in parser.get_examples(s):
  576. ... print (piece.source, piece.want, piece.lineno)
  577. ('x, y = 2, 3 # no output expected\n', '', 1)
  578. ('if 1:\n print x\n print y\n', '2\n3\n', 2)
  579. ('x+y\n', '5\n', 9)
  580. The `get_doctest` method creates a Test from the examples, along with the
  581. given arguments:
  582. >>> test = parser.get_doctest(s, {}, 'name', 'filename', lineno=5)
  583. >>> (test.name, test.filename, test.lineno)
  584. ('name', 'filename', 5)
  585. >>> for piece in test.examples:
  586. ... print (piece.source, piece.want, piece.lineno)
  587. ('x, y = 2, 3 # no output expected\n', '', 1)
  588. ('if 1:\n print x\n print y\n', '2\n3\n', 2)
  589. ('x+y\n', '5\n', 9)
  590. """
  591. class test_DocTestRunner:
  592. def basics(): r"""
  593. Unit tests for the `DocTestRunner` class.
  594. DocTestRunner is used to run DocTest test cases, and to accumulate
  595. statistics. Here's a simple DocTest case we can use:
  596. >>> def f(x):
  597. ... '''
  598. ... >>> x = 12
  599. ... >>> print x
  600. ... 12
  601. ... >>> x//2
  602. ... 6
  603. ... '''
  604. >>> test = doctest.DocTestFinder().find(f)[0]
  605. The main DocTestRunner interface is the `run` method, which runs a
  606. given DocTest case in a given namespace (globs). It returns a tuple
  607. `(f,t)`, where `f` is the number of failed tests and `t` is the number
  608. of tried tests.
  609. >>> doctest.DocTestRunner(verbose=False).run(test)
  610. TestResults(failed=0, attempted=3)
  611. If any example produces incorrect output, then the test runner reports
  612. the failure and proceeds to the next example:
  613. >>> def f(x):
  614. ... '''
  615. ... >>> x = 12
  616. ... >>> print x
  617. ... 14
  618. ... >>> x//2
  619. ... 6
  620. ... '''
  621. >>> test = doctest.DocTestFinder().find(f)[0]
  622. >>> doctest.DocTestRunner(verbose=True).run(test)
  623. ... # doctest: +ELLIPSIS
  624. Trying:
  625. x = 12
  626. Expecting nothing
  627. ok
  628. Trying:
  629. print x
  630. Expecting:
  631. 14
  632. **********************************************************************
  633. File ..., line 4, in f
  634. Failed example:
  635. print x
  636. Expected:
  637. 14
  638. Got:
  639. 12
  640. Trying:
  641. x//2
  642. Expecting:
  643. 6
  644. ok
  645. TestResults(failed=1, attempted=3)
  646. """
  647. def verbose_flag(): r"""
  648. The `verbose` flag makes the test runner generate more detailed
  649. output:
  650. >>> def f(x):
  651. ... '''
  652. ... >>> x = 12
  653. ... >>> print x
  654. ... 12
  655. ... >>> x//2
  656. ... 6
  657. ... '''
  658. >>> test = doctest.DocTestFinder().find(f)[0]
  659. >>> doctest.DocTestRunner(verbose=True).run(test)
  660. Trying:
  661. x = 12
  662. Expecting nothing
  663. ok
  664. Trying:
  665. print x
  666. Expecting:
  667. 12
  668. ok
  669. Trying:
  670. x//2
  671. Expecting:
  672. 6
  673. ok
  674. TestResults(failed=0, attempted=3)
  675. If the `verbose` flag is unspecified, then the output will be verbose
  676. iff `-v` appears in sys.argv:
  677. >>> # Save the real sys.argv list.
  678. >>> old_argv = sys.argv
  679. >>> # If -v does not appear in sys.argv, then output isn't verbose.
  680. >>> sys.argv = ['test']
  681. >>> doctest.DocTestRunner().run(test)
  682. TestResults(failed=0, attempted=3)
  683. >>> # If -v does appear in sys.argv, then output is verbose.
  684. >>> sys.argv = ['test', '-v']
  685. >>> doctest.DocTestRunner().run(test)
  686. Trying:
  687. x = 12
  688. Expecting nothing
  689. ok
  690. Trying:
  691. print x
  692. Expecting:
  693. 12
  694. ok
  695. Trying:
  696. x//2
  697. Expecting:
  698. 6
  699. ok
  700. TestResults(failed=0, attempted=3)
  701. >>> # Restore sys.argv
  702. >>> sys.argv = old_argv
  703. In the remaining examples, the test runner's verbosity will be
  704. explicitly set, to ensure that the test behavior is consistent.
  705. """
  706. def exceptions(): r"""
  707. Tests of `DocTestRunner`'s exception handling.
  708. An expected exception is specified with a traceback message. The
  709. lines between the first line and the type/value may be omitted or
  710. replaced with any other string:
  711. >>> def f(x):
  712. ... '''
  713. ... >>> x = 12
  714. ... >>> print x//0
  715. ... Traceback (most recent call last):
  716. ... ZeroDivisionError: integer division or modulo by zero
  717. ... '''
  718. >>> test = doctest.DocTestFinder().find(f)[0]
  719. >>> doctest.DocTestRunner(verbose=False).run(test)
  720. TestResults(failed=0, attempted=2)
  721. An example may not generate output before it raises an exception; if
  722. it does, then the traceback message will not be recognized as
  723. signaling an expected exception, so the example will be reported as an
  724. unexpected exception:
  725. >>> def f(x):
  726. ... '''
  727. ... >>> x = 12
  728. ... >>> print 'pre-exception output', x//0
  729. ... pre-exception output
  730. ... Traceback (most recent call last):
  731. ... ZeroDivisionError: integer division or modulo by zero
  732. ... '''
  733. >>> test = doctest.DocTestFinder().find(f)[0]
  734. >>> doctest.DocTestRunner(verbose=False).run(test)
  735. ... # doctest: +ELLIPSIS
  736. **********************************************************************
  737. File ..., line 4, in f
  738. Failed example:
  739. print 'pre-exception output', x//0
  740. Exception raised:
  741. ...
  742. ZeroDivisionError: integer division or modulo by zero
  743. TestResults(failed=1, attempted=2)
  744. Exception messages may contain newlines:
  745. >>> def f(x):
  746. ... r'''
  747. ... >>> raise ValueError, 'multi\nline\nmessage'
  748. ... Traceback (most recent call last):
  749. ... ValueError: multi
  750. ... line
  751. ... message
  752. ... '''
  753. >>> test = doctest.DocTestFinder().find(f)[0]
  754. >>> doctest.DocTestRunner(verbose=False).run(test)
  755. TestResults(failed=0, attempted=1)
  756. If an exception is expected, but an exception with the wrong type or
  757. message is raised, then it is reported as a failure:
  758. >>> def f(x):
  759. ... r'''
  760. ... >>> raise ValueError, 'message'
  761. ... Traceback (most recent call last):
  762. ... ValueError: wrong message
  763. ... '''
  764. >>> test = doctest.DocTestFinder().find(f)[0]
  765. >>> doctest.DocTestRunner(verbose=False).run(test)
  766. ... # doctest: +ELLIPSIS
  767. **********************************************************************
  768. File ..., line 3, in f
  769. Failed example:
  770. raise ValueError, 'message'
  771. Expected:
  772. Traceback (most recent call last):
  773. ValueError: wrong message
  774. Got:
  775. Traceback (most recent call last):
  776. ...
  777. ValueError: message
  778. TestResults(failed=1, attempted=1)
  779. However, IGNORE_EXCEPTION_DETAIL can be used to allow a mismatch in the
  780. detail:
  781. >>> def f(x):
  782. ... r'''
  783. ... >>> raise ValueError, 'message' #doctest: +IGNORE_EXCEPTION_DETAIL
  784. ... Traceback (most recent call last):
  785. ... ValueError: wrong message
  786. ... '''
  787. >>> test = doctest.DocTestFinder().find(f)[0]
  788. >>> doctest.DocTestRunner(verbose=False).run(test)
  789. TestResults(failed=0, attempted=1)
  790. IGNORE_EXCEPTION_DETAIL also ignores difference in exception formatting
  791. between Python versions. For example, in Python 3.x, the module path of
  792. the exception is in the output, but this will fail under Python 2:
  793. >>> def f(x):
  794. ... r'''
  795. ... >>> from httplib import HTTPException
  796. ... >>> raise HTTPException('message')
  797. ... Traceback (most recent call last):
  798. ... httplib.HTTPException: message
  799. ... '''
  800. >>> test = doctest.DocTestFinder().find(f)[0]
  801. >>> doctest.DocTestRunner(verbose=False).run(test)
  802. ... # doctest: +ELLIPSIS
  803. **********************************************************************
  804. File ..., line 4, in f
  805. Failed example:
  806. raise HTTPException('message')
  807. Expected:
  808. Traceback (most recent call last):
  809. httplib.HTTPException: message
  810. Got:
  811. Traceback (most recent call last):
  812. ...
  813. HTTPException: message
  814. TestResults(failed=1, attempted=2)
  815. But in Python 2 the module path is not included, and therefore a test must look
  816. like the following test to succeed in Python 2. But that test will fail under
  817. Python 3.
  818. >>> def f(x):
  819. ... r'''
  820. ... >>> from httplib import HTTPException
  821. ... >>> raise HTTPException('message')
  822. ... Traceback (most recent call last):
  823. ... HTTPException: message
  824. ... '''
  825. >>> test = doctest.DocTestFinder().find(f)[0]
  826. >>> doctest.DocTestRunner(verbose=False).run(test)
  827. TestResults(failed=0, attempted=2)
  828. However, with IGNORE_EXCEPTION_DETAIL, the module name of the exception
  829. (if any) will be ignored:
  830. >>> def f(x):
  831. ... r'''
  832. ... >>> from httplib import HTTPException
  833. ... >>> raise HTTPException('message') #doctest: +IGNORE_EXCEPTION_DETAIL
  834. ... Traceback (most recent call last):
  835. ... HTTPException: message
  836. ... '''
  837. >>> test = doctest.DocTestFinder().find(f)[0]
  838. >>> doctest.DocTestRunner(verbose=False).run(test)
  839. TestResults(failed=0, attempted=2)
  840. The module path will be completely ignored, so two different module paths will
  841. still pass if IGNORE_EXCEPTION_DETAIL is given. This is intentional, so it can
  842. be used when exceptions have changed module.
  843. >>> def f(x):
  844. ... r'''
  845. ... >>> from httplib import HTTPException
  846. ... >>> raise HTTPException('message') #doctest: +IGNORE_EXCEPTION_DETAIL
  847. ... Traceback (most recent call last):
  848. ... foo.bar.HTTPException: message
  849. ... '''
  850. >>> test = doctest.DocTestFinder().find(f)[0]
  851. >>> doctest.DocTestRunner(verbose=False).run(test)
  852. TestResults(failed=0, attempted=2)
  853. But IGNORE_EXCEPTION_DETAIL does not allow a mismatch in the exception type:
  854. >>> def f(x):
  855. ... r'''
  856. ... >>> raise ValueError, 'message' #doctest: +IGNORE_EXCEPTION_DETAIL
  857. ... Traceback (most recent call last):
  858. ... TypeError: wrong type
  859. ... '''
  860. >>> test = doctest.DocTestFinder().find(f)[0]
  861. >>> doctest.DocTestRunner(verbose=False).run(test)
  862. ... # doctest: +ELLIPSIS
  863. **********************************************************************
  864. File ..., line 3, in f
  865. Failed example:
  866. raise ValueError, 'message' #doctest: +IGNORE_EXCEPTION_DETAIL
  867. Expected:
  868. Traceback (most recent call last):
  869. TypeError: wrong type
  870. Got:
  871. Traceback (most recent call last):
  872. ...
  873. ValueError: message
  874. TestResults(failed=1, attempted=1)
  875. If the exception does not have a message, you can still use
  876. IGNORE_EXCEPTION_DETAIL to normalize the modules between Python 2 and 3:
  877. >>> def f(x):
  878. ... r'''
  879. ... >>> from Queue import Empty
  880. ... >>> raise Empty() #doctest: +IGNORE_EXCEPTION_DETAIL
  881. ... Traceback (most recent call last):
  882. ... foo.bar.Empty
  883. ... '''
  884. >>> test = doctest.DocTestFinder().find(f)[0]
  885. >>> doctest.DocTestRunner(verbose=False).run(test)
  886. TestResults(failed=0, attempted=2)
  887. Note that a trailing colon doesn't matter either:
  888. >>> def f(x):
  889. ... r'''
  890. ... >>> from Queue import Empty
  891. ... >>> raise Empty() #doctest: +IGNORE_EXCEPTION_DETAIL
  892. ... Traceback (most recent call last):
  893. ... foo.bar.Empty:
  894. ... '''
  895. >>> test = doctest.DocTestFinder().find(f)[0]
  896. >>> doctest.DocTestRunner(verbose=False).run(test)
  897. TestResults(failed=0, attempted=2)
  898. If an exception is raised but not expected, then it is reported as an
  899. unexpected exception:
  900. >>> def f(x):
  901. ... r'''
  902. ... >>> 1//0
  903. ... 0
  904. ... '''
  905. >>> test = doctest.DocTestFinder().find(f)[0]
  906. >>> doctest.DocTestRunner(verbose=False).run(test)
  907. ... # doctest: +ELLIPSIS
  908. **********************************************************************
  909. File ..., line 3, in f
  910. Failed example:
  911. 1//0
  912. Exception raised:
  913. Traceback (most recent call last):
  914. ...
  915. ZeroDivisionError: integer division or modulo by zero
  916. TestResults(failed=1, attempted=1)
  917. """
  918. def displayhook(): r"""
  919. Test that changing sys.displayhook doesn't matter for doctest.
  920. >>> import sys
  921. >>> orig_displayhook = sys.displayhook
  922. >>> def my_displayhook(x):
  923. ... print('hi!')
  924. >>> sys.displayhook = my_displayhook
  925. >>> def f():
  926. ... '''
  927. ... >>> 3
  928. ... 3
  929. ... '''
  930. >>> test = doctest.DocTestFinder().find(f)[0]
  931. >>> r = doctest.DocTestRunner(verbose=False).run(test)
  932. >>> post_displayhook = sys.displayhook
  933. We need to restore sys.displayhook now, so that we'll be able to test
  934. results.
  935. >>> sys.displayhook = orig_displayhook
  936. Ok, now we can check that everything is ok.
  937. >>> r
  938. TestResults(failed=0, attempted=1)
  939. >>> post_displayhook is my_displayhook
  940. True
  941. """
  942. def optionflags(): r"""
  943. Tests of `DocTestRunner`'s option flag handling.
  944. Several option flags can be used to customize the behavior of the test
  945. runner. These are defined as module constants in doctest, and passed
  946. to the DocTestRunner constructor (multiple constants should be ORed
  947. together).
  948. The DONT_ACCEPT_TRUE_FOR_1 flag disables matches between True/False
  949. and 1/0:
  950. >>> def f(x):
  951. ... '>>> True\n1\n'
  952. >>> # Without the flag:
  953. >>> test = doctest.DocTestFinder().find(f)[0]
  954. >>> doctest.DocTestRunner(verbose=False).run(test)
  955. TestResults(failed=0, attempted=1)
  956. >>> # With the flag:
  957. >>> test = doctest.DocTestFinder().find(f)[0]
  958. >>> flags = doctest.DONT_ACCEPT_TRUE_FOR_1
  959. >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
  960. ... # doctest: +ELLIPSIS
  961. **********************************************************************
  962. File ..., line 2, in f
  963. Failed example:
  964. True
  965. Expected:
  966. 1
  967. Got:
  968. True
  969. TestResults(failed=1, attempted=1)
  970. The DONT_ACCEPT_BLANKLINE flag disables the match between blank lines
  971. and the '<BLANKLINE>' marker:
  972. >>> def f(x):
  973. ... '>>> print "a\\n\\nb"\na\n<BLANKLINE>\nb\n'
  974. >>> # Without the flag:
  975. >>> test = doctest.DocTestFinder().find(f)[0]
  976. >>> doctest.DocTestRunner(verbose=False).run(test)
  977. TestResults(failed=0, attempted=1)
  978. >>> # With the flag:
  979. >>> test = doctest.DocTestFinder().find(f)[0]
  980. >>> flags = doctest.DONT_ACCEPT_BLANKLINE
  981. >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
  982. ... # doctest: +ELLIPSIS
  983. **********************************************************************
  984. File ..., line 2, in f
  985. Failed example:
  986. print "a\n\nb"
  987. Expected:
  988. a
  989. <BLANKLINE>
  990. b
  991. Got:
  992. a
  993. <BLANKLINE>
  994. b
  995. TestResults(failed=1, attempted=1)
  996. The NORMALIZE_WHITESPACE flag causes all sequences of whitespace to be
  997. treated as equal:
  998. >>> def f(x):
  999. ... '>>> print 1, 2, 3\n 1 2\n 3'
  1000. >>> # Without the flag:
  1001. >>> test = doctest.DocTestFinder().find(f)[0]
  1002. >>> doctest.DocTestRunner(verbose=False).run(test)
  1003. ... # doctest: +ELLIPSIS
  1004. **********************************************************************
  1005. File ..., line 2, in f
  1006. Failed example:
  1007. print 1, 2, 3
  1008. Expected:
  1009. 1 2
  1010. 3
  1011. Got:
  1012. 1 2 3
  1013. TestResults(failed=1, attempted=1)
  1014. >>> # With the flag:
  1015. >>> test = doctest.DocTestFinder().find(f)[0]
  1016. >>> flags = doctest.NORMALIZE_WHITESPACE
  1017. >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
  1018. TestResults(failed=0, attempted=1)
  1019. An example from the docs:
  1020. >>> print range(20) #doctest: +NORMALIZE_WHITESPACE
  1021. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  1022. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
  1023. The ELLIPSIS flag causes ellipsis marker ("...") in the expected
  1024. output to match any substring in the actual output:
  1025. >>> def f(x):
  1026. ... '>>> print range(15)\n[0, 1, 2, ..., 14]\n'
  1027. >>> # Without the flag:
  1028. >>> test = doctest.DocTestFinder().find(f)[0]
  1029. >>> doctest.DocTestRunner(verbose=False).run(test)
  1030. ... # doctest: +ELLIPSIS
  1031. **********************************************************************
  1032. File ..., line 2, in f
  1033. Failed example:
  1034. print range(15)
  1035. Expected:
  1036. [0, 1, 2, ..., 14]
  1037. Got:
  1038. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
  1039. TestResults(failed=1, attempted=1)
  1040. >>> # With the flag:
  1041. >>> test = doctest.DocTestFinder().find(f)[0]
  1042. >>> flags = doctest.ELLIPSIS
  1043. >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
  1044. TestResults(failed=0, attempted=1)
  1045. ... also matches nothing:
  1046. >>> for i in range(100):
  1047. ... print i**2, #doctest: +ELLIPSIS
  1048. 0 1...4...9 16 ... 36 49 64 ... 9801
  1049. ... can be surprising; e.g., this test passes:
  1050. >>> for i in range(21): #doctest: +ELLIPSIS
  1051. ... print i,
  1052. 0 1 2 ...1...2...0
  1053. Examples from the docs:
  1054. >>> print range(20) # doctest:+ELLIPSIS
  1055. [0, 1, ..., 18, 19]
  1056. >>> print range(20) # doctest: +ELLIPSIS
  1057. ... # doctest: +NORMALIZE_WHITESPACE
  1058. [0, 1, ..., 18, 19]
  1059. The SKIP flag causes an example to be skipped entirely. I.e., the
  1060. example is not run. It can be useful in contexts where doctest
  1061. examples serve as both documentation and test cases, and an example
  1062. should be included for documentation purposes, but should not be
  1063. checked (e.g., because its output is random, or depends on resources
  1064. which would be unavailable.) The SKIP flag can also be used for
  1065. 'commenting out' broken examples.
  1066. >>> import unavailable_resource # doctest: +SKIP
  1067. >>> unavailable_resource.do_something() # doctest: +SKIP
  1068. >>> unavailable_resource.blow_up() # doctest: +SKIP
  1069. Traceback (most recent call last):
  1070. ...
  1071. UncheckedBlowUpError: Nobody checks me.
  1072. >>> import random
  1073. >>> print random.random() # doctest: +SKIP
  1074. 0.721216923889
  1075. The REPORT_UDIFF flag causes failures that involve multi-line expected
  1076. and actual outputs to be displayed using a unified diff:
  1077. >>> def f(x):
  1078. ... r'''
  1079. ... >>> print '\n'.join('abcdefg')
  1080. ... a
  1081. ... B
  1082. ... c
  1083. ... d
  1084. ... f
  1085. ... g
  1086. ... h
  1087. ... '''
  1088. >>> # Without the flag:
  1089. >>> test = doctest.DocTestFinder().find(f)[0]
  1090. >>> doctest.DocTestRunner(verbose=False).run(test)
  1091. ... # doctest: +ELLIPSIS
  1092. **********************************************************************
  1093. File ..., line 3, in f
  1094. Failed example:
  1095. print '\n'.join('abcdefg')
  1096. Expected:
  1097. a
  1098. B
  1099. c
  1100. d
  1101. f
  1102. g
  1103. h
  1104. Got:
  1105. a
  1106. b
  1107. c
  1108. d
  1109. e
  1110. f
  1111. g
  1112. TestResults(failed=1, attempted=1)
  1113. >>> # With the flag:
  1114. >>> test = doctest.DocTestFinder().find(f)[0]
  1115. >>> flags = doctest.REPORT_UDIFF
  1116. >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
  1117. ... # doctest: +ELLIPSIS
  1118. **********************************************************************
  1119. File ..., line 3, in f
  1120. Failed example:
  1121. print '\n'.join('abcdefg')
  1122. Differences (unified diff with -expected +actual):
  1123. @@ -1,7 +1,7 @@
  1124. a
  1125. -B
  1126. +b
  1127. c
  1128. d
  1129. +e
  1130. f
  1131. g
  1132. -h
  1133. TestResults(failed=1, attempted=1)
  1134. The REPORT_CDIFF flag causes failures that involve multi-line expected
  1135. and actual outputs to be displayed using a context diff:
  1136. >>> # Reuse f() from the REPORT_UDIFF example, above.
  1137. >>> test = doctest.DocTestFinder().find(f)[0]
  1138. >>> flags = doctest.REPORT_CDIFF
  1139. >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
  1140. ... # doctest: +ELLIPSIS
  1141. **********************************************************************
  1142. File ..., line 3, in f
  1143. Failed example:
  1144. print '\n'.join('abcdefg')
  1145. Differences (context diff with expected followed by actual):
  1146. ***************
  1147. *** 1,7 ****
  1148. a
  1149. ! B
  1150. c
  1151. d
  1152. f
  1153. g
  1154. - h
  1155. --- 1,7 ----
  1156. a
  1157. ! b
  1158. c
  1159. d
  1160. + e
  1161. f
  1162. g
  1163. TestResults(failed=1, attempted=1)
  1164. The REPORT_NDIFF flag causes failures to use the difflib.Differ algorithm
  1165. used by the popular ndiff.py utility. This does intraline difference
  1166. marking, as well as interline differences.
  1167. >>> def f(x):
  1168. ... r'''
  1169. ... >>> print "a b c d e f g h i j k l m"
  1170. ... a b c d e f g h i j k 1 m
  1171. ... '''
  1172. >>> test = doctest.DocTestFinder().find(f)[0]
  1173. >>> flags = doctest.REPORT_NDIFF
  1174. >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
  1175. ... # doctest: +ELLIPSIS
  1176. **********************************************************************
  1177. File ..., line 3, in f
  1178. Failed example:
  1179. print "a b c d e f g h i j k l m"
  1180. Differences (ndiff with -expected +actual):
  1181. - a b c d e f g h i j k 1 m
  1182. ? ^
  1183. + a b c d e f g h i j k l m
  1184. ? + ++ ^
  1185. TestResults(failed=1, attempted=1)
  1186. The REPORT_ONLY_FIRST_FAILURE suppresses result output after the first
  1187. failing example:
  1188. >>> def f(x):
  1189. ... r'''
  1190. ... >>> print 1 # first success
  1191. ... 1
  1192. ... >>> print 2 # first failure
  1193. ... 200
  1194. ... >>> print 3 # second failure
  1195. ... 300
  1196. ... >>> print 4 # second success
  1197. ... 4
  1198. ... >>> print 5 # third failure
  1199. ... 500
  1200. ... '''
  1201. >>> test = doctest.DocTestFinder().find(f)[0]
  1202. >>> flags = doctest.REPORT_ONLY_FIRST_FAILURE
  1203. >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
  1204. ... # doctest: +ELLIPSIS
  1205. **********************************************************************
  1206. File ..., line 5, in f
  1207. Failed example:
  1208. print 2 # first failure
  1209. Expected:
  1210. 200
  1211. Got:
  1212. 2
  1213. TestResults(failed=3, attempted=5)
  1214. However, output from `report_start` is not suppressed:
  1215. >>> doctest.DocTestRunner(verbose=True, optionflags=flags).run(test)
  1216. ... # doctest: +ELLIPSIS
  1217. Trying:
  1218. print 1 # first success
  1219. Expecting:
  1220. 1
  1221. ok
  1222. Trying:
  1223. print 2 # first failure
  1224. Expecting:
  1225. 200
  1226. **********************************************************************
  1227. File ..., line 5, in f
  1228. Failed example:
  1229. print 2 # first failure
  1230. Expected:
  1231. 200
  1232. Got:
  1233. 2
  1234. TestResults(failed=3, attempted=5)
  1235. For the purposes of REPORT_ONLY_FIRST_FAILURE, unexpected exceptions
  1236. count as failures:
  1237. >>> def f(x):
  1238. ... r'''
  1239. ... >>> print 1 # first success
  1240. ... 1
  1241. ... >>> raise ValueError(2) # first failure
  1242. ... 200
  1243. ... >>> print 3 # second failure
  1244. ... 300
  1245. ... >>> print 4 # second success
  1246. ... 4
  1247. ... >>> print 5 # third failure
  1248. ... 500
  1249. ... '''
  1250. >>> test = doctest.DocTestFinder().find(f)[0]
  1251. >>> flags = doctest.REPORT_ONLY_FIRST_FAILURE
  1252. >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
  1253. ... # doctest: +ELLIPSIS
  1254. **********************************************************************
  1255. File ..., line 5, in f
  1256. Failed example:
  1257. raise ValueError(2) # first failure
  1258. Exception raised:
  1259. ...
  1260. ValueError: 2
  1261. TestResults(failed=3, attempted=5)
  1262. New option flags can also be registered, via register_optionflag(). Here
  1263. we reach into doctest's internals a bit.
  1264. >>> unlikely = "UNLIKELY_OPTION_NAME"
  1265. >>> unlikely in doctest.OPTIONFLAGS_BY_NAME
  1266. False
  1267. >>> new_flag_value = doctest.register_optionflag(unlikely)
  1268. >>> unlikely in doctest.OPTIONFLAGS_BY_NAME
  1269. True
  1270. Before 2.4.4/2.5, registering a name more than once erroneously created
  1271. more than one flag value. Here we verify that's fixed:
  1272. >>> redundant_flag_value = doctest.register_optionflag(unlikely)
  1273. >>> redundant_flag_value == new_flag_value
  1274. True
  1275. Clean up.
  1276. >>> del doctest.OPTIONFLAGS_BY_NAME[unlikely]
  1277. """
  1278. def option_directives(): r"""
  1279. Tests of `DocTestRunner`'s option directive mechanism.
  1280. Option directives can be used to turn option flags on or off for a
  1281. single example. To turn an option on for an example, follow that
  1282. example with a comment of the form ``# doctest: +OPTION``:
  1283. >>> def f(x): r'''
  1284. ... >>> print range(10) # should fail: no ellipsis
  1285. ... [0, 1, ..., 9]
  1286. ...
  1287. ... >>> print range(10) # doctest: +ELLIPSIS
  1288. ... [0, 1, ..., 9]
  1289. ... '''
  1290. >>> test = doctest.DocTestFinder().find(f)[0]
  1291. >>> doctest.DocTestRunner(verbose=False).run(test)
  1292. ... # doctest: +ELLIPSIS
  1293. **********************************************************************
  1294. File ..., line 2, in f
  1295. Failed example:
  1296. print range(10) # should fail: no ellipsis
  1297. Expected:
  1298. [0, 1, ..., 9]
  1299. Got:
  1300. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  1301. TestResults(failed=1, attempted=2)
  1302. To turn an option off for an example, follow that example with a
  1303. comment of the form ``# doctest: -OPTION``:
  1304. >>> def f(x): r'''
  1305. ... >>> print range(10)
  1306. ... [0, 1, ..., 9]
  1307. ...
  1308. ... >>> # should fail: no ellipsis
  1309. ... >>> print range(10) # doctest: -ELLIPSIS
  1310. ... [0, 1, ..., 9]
  1311. ... '''
  1312. >>> test = doctest.DocTestFinder().find(f)[0]
  1313. >>> doctest.DocTestRunner(verbose=False,
  1314. ... optionflags=doctest.ELLIPSIS).run(test)
  1315. ... # doctest: +ELLIPSIS
  1316. **********************************************************************
  1317. File ..., line 6, in f
  1318. Failed example:
  1319. print range(10) # doctest: -ELLIPSIS
  1320. Expected:
  1321. [0, 1, ..., 9]
  1322. Got:
  1323. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  1324. TestResults(failed=1, attempted=2)
  1325. Option directives affect only the example that they appear with; they
  1326. do not change the options for surrounding examples:
  1327. >>> def f(x): r'''
  1328. ... >>> print range(10) # Should fail: no ellipsis
  1329. ... [0, 1, ..., 9]
  1330. ...
  1331. ... >>> print range(10) # doctest: +ELLIPSIS
  1332. ... [0, 1, ..., 9]
  1333. ...
  1334. ... >>> print range(10) # Should fail: no ellipsis
  1335. ... [0, 1, ..., 9]
  1336. ... '''
  1337. >>> test = doctest.DocTestFinder().find(f)[0]
  1338. >>> doctest.DocTestRunner(verbose=False).run(test)
  1339. ... # doctest: +ELLIPSIS
  1340. **********************************************************************
  1341. File ..., line 2, in f
  1342. Failed example:
  1343. print range(10) # Should fail: no ellipsis
  1344. Expected:
  1345. [0, 1, ..., 9]
  1346. Got:
  1347. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  1348. **********************************************************************
  1349. File ..., line 8, in f
  1350. Failed example:
  1351. print range(10) # Should fail: no ellipsis
  1352. Expected:
  1353. [0, 1, ..., 9]
  1354. Got:
  1355. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  1356. TestResults(failed=2, attempted=3)
  1357. Multiple options may be modified by a single option directive. They
  1358. may be separated by whitespace, commas, or both:
  1359. >>> def f(x): r'''
  1360. ... >>> print range(10) # Should fail
  1361. ... [0, 1, ..., 9]
  1362. ... >>> print range(10) # Should succeed
  1363. ... ... # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
  1364. ... [0, 1, ..., 9]
  1365. ... '''
  1366. >>> test = doctest.DocTestFinder().find(f)[0]
  1367. >>> doctest.DocTestRunner(verbose=False).run(test)
  1368. ... # doctest: +ELLIPSIS
  1369. **********************************************************************
  1370. File ..., line 2, in f
  1371. Failed example:
  1372. print range(10) # Should fail
  1373. Expected:
  1374. [0, 1, ..., 9]
  1375. Got:
  1376. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  1377. TestResults(failed=1, attempted=2)
  1378. >>> def f(x): r'''
  1379. ... >>> print range(10) # Should fail
  1380. ... [0, 1, ..., 9]
  1381. ... >>> print range(10) # Should succeed
  1382. ... ... # doctest: +ELLIPSIS,+NORMALIZE_WHITESPACE
  1383. ... [0, 1, ..., 9]
  1384. ... '''
  1385. >>> test = doctest.DocTestFinder().find(f)[0]
  1386. >>> doctest.DocTestRunner(verbose=False).run(test)
  1387. ... # doctest: +ELLIPSIS
  1388. **********************************************************************
  1389. File ..., line 2, in f
  1390. Failed example:
  1391. print range(10) # Should fail
  1392. Expected:
  1393. [0, 1, ..., 9]
  1394. Got:
  1395. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  1396. TestResults(failed=1, attempted=2)
  1397. >>> def f(x): r'''
  1398. ... >>> print range(10) # Should fail
  1399. ... [0, 1, ..., 9]
  1400. ... >>> print range(10) # Should succeed
  1401. ... ... # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
  1402. ... [0, 1, ..., 9]
  1403. ... '''
  1404. >>> test = doctest.DocTestFinder().find(f)[0]
  1405. >>> doctest.DocTestRunner(verbose=False).run(test)
  1406. ... # doctest: +ELLIPSIS
  1407. **********************************************************************
  1408. File ..., line 2, in f
  1409. Failed example:
  1410. print range(10) # Should fail
  1411. Expected:
  1412. [0, 1, ..., 9]
  1413. Got:
  1414. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  1415. TestResults(failed=1, attempted=2)
  1416. The option directive may be put on the line following the source, as
  1417. long as a continuation prompt is used:
  1418. >>> def f(x): r'''
  1419. ... >>> print range(10)
  1420. ... ... # doctest: +ELLIPSIS
  1421. ... [0, 1, ..., 9]
  1422. ... '''
  1423. >>> test = doctest.DocTestFinder().find(f)[0]
  1424. >>> doctest.DocTestRunner(verbose=False).run(test)
  1425. TestResults(failed=0, attempted=1)
  1426. For examples with multi-line source, the option directive may appear
  1427. at the end of any line:
  1428. >>> def f(x): r'''
  1429. ... >>> for x in range(10): # doctest: +ELLIPSIS
  1430. ... ..

Large files files are truncated, but you can click here to view the full file