gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2067 lines
204KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="doctest — Test interactive Python examples" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/doctest.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/doctest.py The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown. Th..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/doctest.py The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown. Th..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>doctest — Test interactive Python examples &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="unittest — Unit testing framework" href="unittest.html" />
  33. <link rel="prev" title="Python Development Mode" href="devmode.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/doctest.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> — Test interactive Python examples</a><ul>
  86. <li><a class="reference internal" href="#simple-usage-checking-examples-in-docstrings">Simple Usage: Checking Examples in Docstrings</a></li>
  87. <li><a class="reference internal" href="#simple-usage-checking-examples-in-a-text-file">Simple Usage: Checking Examples in a Text File</a></li>
  88. <li><a class="reference internal" href="#how-it-works">How It Works</a><ul>
  89. <li><a class="reference internal" href="#which-docstrings-are-examined">Which Docstrings Are Examined?</a></li>
  90. <li><a class="reference internal" href="#how-are-docstring-examples-recognized">How are Docstring Examples Recognized?</a></li>
  91. <li><a class="reference internal" href="#what-s-the-execution-context">What’s the Execution Context?</a></li>
  92. <li><a class="reference internal" href="#what-about-exceptions">What About Exceptions?</a></li>
  93. <li><a class="reference internal" href="#option-flags">Option Flags</a></li>
  94. <li><a class="reference internal" href="#directives">Directives</a></li>
  95. <li><a class="reference internal" href="#warnings">Warnings</a></li>
  96. </ul>
  97. </li>
  98. <li><a class="reference internal" href="#basic-api">Basic API</a></li>
  99. <li><a class="reference internal" href="#unittest-api">Unittest API</a></li>
  100. <li><a class="reference internal" href="#advanced-api">Advanced API</a><ul>
  101. <li><a class="reference internal" href="#doctest-objects">DocTest Objects</a></li>
  102. <li><a class="reference internal" href="#example-objects">Example Objects</a></li>
  103. <li><a class="reference internal" href="#doctestfinder-objects">DocTestFinder objects</a></li>
  104. <li><a class="reference internal" href="#doctestparser-objects">DocTestParser objects</a></li>
  105. <li><a class="reference internal" href="#doctestrunner-objects">DocTestRunner objects</a></li>
  106. <li><a class="reference internal" href="#outputchecker-objects">OutputChecker objects</a></li>
  107. </ul>
  108. </li>
  109. <li><a class="reference internal" href="#debugging">Debugging</a></li>
  110. <li><a class="reference internal" href="#soapbox">Soapbox</a></li>
  111. </ul>
  112. </li>
  113. </ul>
  114. </div>
  115. <div>
  116. <h4>Previous topic</h4>
  117. <p class="topless"><a href="devmode.html"
  118. title="previous chapter">Python Development Mode</a></p>
  119. </div>
  120. <div>
  121. <h4>Next topic</h4>
  122. <p class="topless"><a href="unittest.html"
  123. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code> — Unit testing framework</a></p>
  124. </div>
  125. <div role="note" aria-label="source link">
  126. <h3>This Page</h3>
  127. <ul class="this-page-menu">
  128. <li><a href="../bugs.html">Report a Bug</a></li>
  129. <li>
  130. <a href="https://github.com/python/cpython/blob/main/Doc/library/doctest.rst"
  131. rel="nofollow">Show Source
  132. </a>
  133. </li>
  134. </ul>
  135. </div>
  136. </nav>
  137. </div>
  138. </div>
  139. <div class="related" role="navigation" aria-label="related navigation">
  140. <h3>Navigation</h3>
  141. <ul>
  142. <li class="right" style="margin-right: 10px">
  143. <a href="../genindex.html" title="General Index"
  144. accesskey="I">index</a></li>
  145. <li class="right" >
  146. <a href="../py-modindex.html" title="Python Module Index"
  147. >modules</a> |</li>
  148. <li class="right" >
  149. <a href="unittest.html" title="unittest — Unit testing framework"
  150. accesskey="N">next</a> |</li>
  151. <li class="right" >
  152. <a href="devmode.html" title="Python Development Mode"
  153. accesskey="P">previous</a> |</li>
  154. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  155. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  156. <li class="switchers">
  157. <div class="language_switcher_placeholder"></div>
  158. <div class="version_switcher_placeholder"></div>
  159. </li>
  160. <li>
  161. </li>
  162. <li id="cpython-language-and-version">
  163. <a href="../index.html">3.12.3 Documentation</a> &#187;
  164. </li>
  165. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  166. <li class="nav-item nav-item-2"><a href="development.html" accesskey="U">Development Tools</a> &#187;</li>
  167. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> — Test interactive Python examples</a></li>
  168. <li class="right">
  169. <div class="inline-search" role="search">
  170. <form class="inline-search" action="../search.html" method="get">
  171. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  172. <input type="submit" value="Go" />
  173. </form>
  174. </div>
  175. |
  176. </li>
  177. <li class="right">
  178. <label class="theme-selector-label">
  179. Theme
  180. <select class="theme-selector" oninput="activateTheme(this.value)">
  181. <option value="auto" selected>Auto</option>
  182. <option value="light">Light</option>
  183. <option value="dark">Dark</option>
  184. </select>
  185. </label> |</li>
  186. </ul>
  187. </div>
  188. <div class="document">
  189. <div class="documentwrapper">
  190. <div class="bodywrapper">
  191. <div class="body" role="main">
  192. <section id="module-doctest">
  193. <span id="doctest-test-interactive-python-examples"></span><h1><a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> — Test interactive Python examples<a class="headerlink" href="#module-doctest" title="Link to this heading">¶</a></h1>
  194. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/doctest.py">Lib/doctest.py</a></p>
  195. <hr class="docutils" />
  196. <p>The <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> module searches for pieces of text that look like interactive
  197. Python sessions, and then executes those sessions to verify that they work
  198. exactly as shown. There are several common ways to use doctest:</p>
  199. <ul class="simple">
  200. <li><p>To check that a module’s docstrings are up-to-date by verifying that all
  201. interactive examples still work as documented.</p></li>
  202. <li><p>To perform regression testing by verifying that interactive examples from a
  203. test file or a test object work as expected.</p></li>
  204. <li><p>To write tutorial documentation for a package, liberally illustrated with
  205. input-output examples. Depending on whether the examples or the expository text
  206. are emphasized, this has the flavor of “literate testing” or “executable
  207. documentation”.</p></li>
  208. </ul>
  209. <p>Here’s a complete but small example module:</p>
  210. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="sd">&quot;&quot;&quot;</span>
  211. <span class="sd">This is the &quot;example&quot; module.</span>
  212. <span class="sd">The example module supplies one function, factorial(). For example,</span>
  213. <span class="sd">&gt;&gt;&gt; factorial(5)</span>
  214. <span class="sd">120</span>
  215. <span class="sd">&quot;&quot;&quot;</span>
  216. <span class="k">def</span> <span class="nf">factorial</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
  217. <span class="w"> </span><span class="sd">&quot;&quot;&quot;Return the factorial of n, an exact integer &gt;= 0.</span>
  218. <span class="sd"> &gt;&gt;&gt; [factorial(n) for n in range(6)]</span>
  219. <span class="sd"> [1, 1, 2, 6, 24, 120]</span>
  220. <span class="sd"> &gt;&gt;&gt; factorial(30)</span>
  221. <span class="sd"> 265252859812191058636308480000000</span>
  222. <span class="sd"> &gt;&gt;&gt; factorial(-1)</span>
  223. <span class="sd"> Traceback (most recent call last):</span>
  224. <span class="sd"> ...</span>
  225. <span class="sd"> ValueError: n must be &gt;= 0</span>
  226. <span class="sd"> Factorials of floats are OK, but the float must be an exact integer:</span>
  227. <span class="sd"> &gt;&gt;&gt; factorial(30.1)</span>
  228. <span class="sd"> Traceback (most recent call last):</span>
  229. <span class="sd"> ...</span>
  230. <span class="sd"> ValueError: n must be exact integer</span>
  231. <span class="sd"> &gt;&gt;&gt; factorial(30.0)</span>
  232. <span class="sd"> 265252859812191058636308480000000</span>
  233. <span class="sd"> It must also not be ridiculously large:</span>
  234. <span class="sd"> &gt;&gt;&gt; factorial(1e100)</span>
  235. <span class="sd"> Traceback (most recent call last):</span>
  236. <span class="sd"> ...</span>
  237. <span class="sd"> OverflowError: n too large</span>
  238. <span class="sd"> &quot;&quot;&quot;</span>
  239. <span class="kn">import</span> <span class="nn">math</span>
  240. <span class="k">if</span> <span class="ow">not</span> <span class="n">n</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">:</span>
  241. <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;n must be &gt;= 0&quot;</span><span class="p">)</span>
  242. <span class="k">if</span> <span class="n">math</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="o">!=</span> <span class="n">n</span><span class="p">:</span>
  243. <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;n must be exact integer&quot;</span><span class="p">)</span>
  244. <span class="k">if</span> <span class="n">n</span><span class="o">+</span><span class="mi">1</span> <span class="o">==</span> <span class="n">n</span><span class="p">:</span> <span class="c1"># catch a value like 1e300</span>
  245. <span class="k">raise</span> <span class="ne">OverflowError</span><span class="p">(</span><span class="s2">&quot;n too large&quot;</span><span class="p">)</span>
  246. <span class="n">result</span> <span class="o">=</span> <span class="mi">1</span>
  247. <span class="n">factor</span> <span class="o">=</span> <span class="mi">2</span>
  248. <span class="k">while</span> <span class="n">factor</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">:</span>
  249. <span class="n">result</span> <span class="o">*=</span> <span class="n">factor</span>
  250. <span class="n">factor</span> <span class="o">+=</span> <span class="mi">1</span>
  251. <span class="k">return</span> <span class="n">result</span>
  252. <span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
  253. <span class="kn">import</span> <span class="nn">doctest</span>
  254. <span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">()</span>
  255. </pre></div>
  256. </div>
  257. <p>If you run <code class="file docutils literal notranslate"><span class="pre">example.py</span></code> directly from the command line, <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a>
  258. works its magic:</p>
  259. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>example.py
  260. <span class="gp">$</span>
  261. </pre></div>
  262. </div>
  263. <p>There’s no output! That’s normal, and it means all the examples worked. Pass
  264. <code class="docutils literal notranslate"><span class="pre">-v</span></code> to the script, and <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> prints a detailed log of what
  265. it’s trying, and prints a summary at the end:</p>
  266. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>example.py<span class="w"> </span>-v
  267. <span class="go">Trying:</span>
  268. <span class="go"> factorial(5)</span>
  269. <span class="go">Expecting:</span>
  270. <span class="go"> 120</span>
  271. <span class="go">ok</span>
  272. <span class="go">Trying:</span>
  273. <span class="go"> [factorial(n) for n in range(6)]</span>
  274. <span class="go">Expecting:</span>
  275. <span class="go"> [1, 1, 2, 6, 24, 120]</span>
  276. <span class="go">ok</span>
  277. </pre></div>
  278. </div>
  279. <p>And so on, eventually ending with:</p>
  280. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Trying:
  281. factorial(1e100)
  282. Expecting:
  283. Traceback (most recent call last):
  284. ...
  285. OverflowError: n too large
  286. ok
  287. 2 items passed all tests:
  288. 1 tests in __main__
  289. 8 tests in __main__.factorial
  290. 9 tests in 2 items.
  291. 9 passed and 0 failed.
  292. Test passed.
  293. $
  294. </pre></div>
  295. </div>
  296. <p>That’s all you need to know to start making productive use of <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a>!
  297. Jump in. The following sections provide full details. Note that there are many
  298. examples of doctests in the standard Python test suite and libraries.
  299. Especially useful examples can be found in the standard test file
  300. <code class="file docutils literal notranslate"><span class="pre">Lib/test/test_doctest/test_doctest.py</span></code>.</p>
  301. <section id="simple-usage-checking-examples-in-docstrings">
  302. <span id="doctest-simple-testmod"></span><h2>Simple Usage: Checking Examples in Docstrings<a class="headerlink" href="#simple-usage-checking-examples-in-docstrings" title="Link to this heading">¶</a></h2>
  303. <p>The simplest way to start using doctest (but not necessarily the way you’ll
  304. continue to do it) is to end each module <code class="xref py py-mod docutils literal notranslate"><span class="pre">M</span></code> with:</p>
  305. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
  306. <span class="kn">import</span> <span class="nn">doctest</span>
  307. <span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">()</span>
  308. </pre></div>
  309. </div>
  310. <p><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> then examines docstrings in module <code class="xref py py-mod docutils literal notranslate"><span class="pre">M</span></code>.</p>
  311. <p>Running the module as a script causes the examples in the docstrings to get
  312. executed and verified:</p>
  313. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">M</span><span class="o">.</span><span class="n">py</span>
  314. </pre></div>
  315. </div>
  316. <p>This won’t display anything unless an example fails, in which case the failing
  317. example(s) and the cause(s) of the failure(s) are printed to stdout, and the
  318. final line of output is <code class="docutils literal notranslate"><span class="pre">***Test</span> <span class="pre">Failed***</span> <span class="pre">N</span> <span class="pre">failures.</span></code>, where <em>N</em> is the
  319. number of examples that failed.</p>
  320. <p>Run it with the <code class="docutils literal notranslate"><span class="pre">-v</span></code> switch instead:</p>
  321. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">M</span><span class="o">.</span><span class="n">py</span> <span class="o">-</span><span class="n">v</span>
  322. </pre></div>
  323. </div>
  324. <p>and a detailed report of all examples tried is printed to standard output, along
  325. with assorted summaries at the end.</p>
  326. <p>You can force verbose mode by passing <code class="docutils literal notranslate"><span class="pre">verbose=True</span></code> to <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>, or
  327. prohibit it by passing <code class="docutils literal notranslate"><span class="pre">verbose=False</span></code>. In either of those cases,
  328. <code class="docutils literal notranslate"><span class="pre">sys.argv</span></code> is not examined by <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a> (so passing <code class="docutils literal notranslate"><span class="pre">-v</span></code> or not
  329. has no effect).</p>
  330. <p>There is also a command line shortcut for running <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>. You can
  331. instruct the Python interpreter to run the doctest module directly from the
  332. standard library and pass the module name(s) on the command line:</p>
  333. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">doctest</span> <span class="o">-</span><span class="n">v</span> <span class="n">example</span><span class="o">.</span><span class="n">py</span>
  334. </pre></div>
  335. </div>
  336. <p>This will import <code class="file docutils literal notranslate"><span class="pre">example.py</span></code> as a standalone module and run
  337. <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a> on it. Note that this may not work correctly if the file is
  338. part of a package and imports other submodules from that package.</p>
  339. <p>For more information on <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>, see section <a class="reference internal" href="#doctest-basic-api"><span class="std std-ref">Basic API</span></a>.</p>
  340. </section>
  341. <section id="simple-usage-checking-examples-in-a-text-file">
  342. <span id="doctest-simple-testfile"></span><h2>Simple Usage: Checking Examples in a Text File<a class="headerlink" href="#simple-usage-checking-examples-in-a-text-file" title="Link to this heading">¶</a></h2>
  343. <p>Another simple application of doctest is testing interactive examples in a text
  344. file. This can be done with the <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a> function:</p>
  345. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">doctest</span>
  346. <span class="n">doctest</span><span class="o">.</span><span class="n">testfile</span><span class="p">(</span><span class="s2">&quot;example.txt&quot;</span><span class="p">)</span>
  347. </pre></div>
  348. </div>
  349. <p>That short script executes and verifies any interactive Python examples
  350. contained in the file <code class="file docutils literal notranslate"><span class="pre">example.txt</span></code>. The file content is treated as if it
  351. were a single giant docstring; the file doesn’t need to contain a Python
  352. program! For example, perhaps <code class="file docutils literal notranslate"><span class="pre">example.txt</span></code> contains this:</p>
  353. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>The ``example`` module
  354. ======================
  355. Using ``factorial``
  356. -------------------
  357. This is an example text file in reStructuredText format. First import
  358. ``factorial`` from the ``example`` module:
  359. &gt;&gt;&gt; from example import factorial
  360. Now use it:
  361. &gt;&gt;&gt; factorial(6)
  362. 120
  363. </pre></div>
  364. </div>
  365. <p>Running <code class="docutils literal notranslate"><span class="pre">doctest.testfile(&quot;example.txt&quot;)</span></code> then finds the error in this
  366. documentation:</p>
  367. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">File</span> <span class="s2">&quot;./example.txt&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">14</span><span class="p">,</span> <span class="ow">in</span> <span class="n">example</span><span class="o">.</span><span class="n">txt</span>
  368. <span class="n">Failed</span> <span class="n">example</span><span class="p">:</span>
  369. <span class="n">factorial</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
  370. <span class="n">Expected</span><span class="p">:</span>
  371. <span class="mi">120</span>
  372. <span class="n">Got</span><span class="p">:</span>
  373. <span class="mi">720</span>
  374. </pre></div>
  375. </div>
  376. <p>As with <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>, <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a> won’t display anything unless an
  377. example fails. If an example does fail, then the failing example(s) and the
  378. cause(s) of the failure(s) are printed to stdout, using the same format as
  379. <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>.</p>
  380. <p>By default, <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a> looks for files in the calling module’s directory.
  381. See section <a class="reference internal" href="#doctest-basic-api"><span class="std std-ref">Basic API</span></a> for a description of the optional arguments
  382. that can be used to tell it to look for files in other locations.</p>
  383. <p>Like <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>, <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a>’s verbosity can be set with the
  384. <code class="docutils literal notranslate"><span class="pre">-v</span></code> command-line switch or with the optional keyword argument
  385. <em>verbose</em>.</p>
  386. <p>There is also a command line shortcut for running <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a>. You can
  387. instruct the Python interpreter to run the doctest module directly from the
  388. standard library and pass the file name(s) on the command line:</p>
  389. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">doctest</span> <span class="o">-</span><span class="n">v</span> <span class="n">example</span><span class="o">.</span><span class="n">txt</span>
  390. </pre></div>
  391. </div>
  392. <p>Because the file name does not end with <code class="file docutils literal notranslate"><span class="pre">.py</span></code>, <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> infers that
  393. it must be run with <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a>, not <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>.</p>
  394. <p>For more information on <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a>, see section <a class="reference internal" href="#doctest-basic-api"><span class="std std-ref">Basic API</span></a>.</p>
  395. </section>
  396. <section id="how-it-works">
  397. <span id="doctest-how-it-works"></span><h2>How It Works<a class="headerlink" href="#how-it-works" title="Link to this heading">¶</a></h2>
  398. <p>This section examines in detail how doctest works: which docstrings it looks at,
  399. how it finds interactive examples, what execution context it uses, how it
  400. handles exceptions, and how option flags can be used to control its behavior.
  401. This is the information that you need to know to write doctest examples; for
  402. information about actually running doctest on these examples, see the following
  403. sections.</p>
  404. <section id="which-docstrings-are-examined">
  405. <span id="doctest-which-docstrings"></span><h3>Which Docstrings Are Examined?<a class="headerlink" href="#which-docstrings-are-examined" title="Link to this heading">¶</a></h3>
  406. <p>The module docstring, and all function, class and method docstrings are
  407. searched. Objects imported into the module are not searched.</p>
  408. <p>In addition, there are cases when you want tests to be part of a module but not part
  409. of the help text, which requires that the tests not be included in the docstring.
  410. Doctest looks for a module-level variable called <code class="docutils literal notranslate"><span class="pre">__test__</span></code> and uses it to locate other
  411. tests. If <code class="docutils literal notranslate"><span class="pre">M.__test__</span></code> exists, it must be a dict, and each
  412. entry maps a (string) name to a function object, class object, or string.
  413. Function and class object docstrings found from <code class="docutils literal notranslate"><span class="pre">M.__test__</span></code> are searched, and
  414. strings are treated as if they were docstrings. In output, a key <code class="docutils literal notranslate"><span class="pre">K</span></code> in
  415. <code class="docutils literal notranslate"><span class="pre">M.__test__</span></code> appears with name <code class="docutils literal notranslate"><span class="pre">M.__test__.K</span></code>.</p>
  416. <p>For example, place this block of code at the top of <code class="file docutils literal notranslate"><span class="pre">example.py</span></code>:</p>
  417. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">__test__</span> <span class="o">=</span> <span class="p">{</span>
  418. <span class="s1">&#39;numbers&#39;</span><span class="p">:</span> <span class="s2">&quot;&quot;&quot;</span>
  419. <span class="s2">&gt;&gt;&gt; factorial(6)</span>
  420. <span class="s2">720</span>
  421. <span class="s2">&gt;&gt;&gt; [factorial(n) for n in range(6)]</span>
  422. <span class="s2">[1, 1, 2, 6, 24, 120]</span>
  423. <span class="s2">&quot;&quot;&quot;</span>
  424. <span class="p">}</span>
  425. </pre></div>
  426. </div>
  427. <p>The value of <code class="docutils literal notranslate"><span class="pre">example.__test__[&quot;numbers&quot;]</span></code> will be treated as a
  428. docstring and all the tests inside it will be run. It is
  429. important to note that the value can be mapped to a function,
  430. class object, or module; if so, <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code>
  431. searches them recursively for docstrings, which are then scanned for tests.</p>
  432. <p>Any classes found are recursively searched similarly, to test docstrings in
  433. their contained methods and nested classes.</p>
  434. </section>
  435. <section id="how-are-docstring-examples-recognized">
  436. <span id="doctest-finding-examples"></span><h3>How are Docstring Examples Recognized?<a class="headerlink" href="#how-are-docstring-examples-recognized" title="Link to this heading">¶</a></h3>
  437. <p>In most cases a copy-and-paste of an interactive console session works fine,
  438. but doctest isn’t trying to do an exact emulation of any specific Python shell.</p>
  439. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># comments are ignored</span>
  440. <span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="mi">12</span>
  441. <span class="gp">&gt;&gt;&gt; </span><span class="n">x</span>
  442. <span class="go">12</span>
  443. <span class="gp">&gt;&gt;&gt; </span><span class="k">if</span> <span class="n">x</span> <span class="o">==</span> <span class="mi">13</span><span class="p">:</span>
  444. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;yes&quot;</span><span class="p">)</span>
  445. <span class="gp">... </span><span class="k">else</span><span class="p">:</span>
  446. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;no&quot;</span><span class="p">)</span>
  447. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;NO&quot;</span><span class="p">)</span>
  448. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;NO!!!&quot;</span><span class="p">)</span>
  449. <span class="gp">...</span>
  450. <span class="go">no</span>
  451. <span class="go">NO</span>
  452. <span class="go">NO!!!</span>
  453. <span class="gp">&gt;&gt;&gt;</span>
  454. </pre></div>
  455. </div>
  456. <p id="index-0">Any expected output must immediately follow the final <code class="docutils literal notranslate"><span class="pre">'&gt;&gt;&gt;</span> <span class="pre">'</span></code> or <code class="docutils literal notranslate"><span class="pre">'...</span> <span class="pre">'</span></code>
  457. line containing the code, and the expected output (if any) extends to the next
  458. <code class="docutils literal notranslate"><span class="pre">'&gt;&gt;&gt;</span> <span class="pre">'</span></code> or all-whitespace line.</p>
  459. <p>The fine print:</p>
  460. <ul>
  461. <li><p>Expected output cannot contain an all-whitespace line, since such a line is
  462. taken to signal the end of expected output. If expected output does contain a
  463. blank line, put <code class="docutils literal notranslate"><span class="pre">&lt;BLANKLINE&gt;</span></code> in your doctest example each place a blank line
  464. is expected.</p></li>
  465. <li><p>All hard tab characters are expanded to spaces, using 8-column tab stops.
  466. Tabs in output generated by the tested code are not modified. Because any
  467. hard tabs in the sample output <em>are</em> expanded, this means that if the code
  468. output includes hard tabs, the only way the doctest can pass is if the
  469. <a class="reference internal" href="#doctest.NORMALIZE_WHITESPACE" title="doctest.NORMALIZE_WHITESPACE"><code class="xref py py-const docutils literal notranslate"><span class="pre">NORMALIZE_WHITESPACE</span></code></a> option or <a class="reference internal" href="#doctest-directives"><span class="std std-ref">directive</span></a>
  470. is in effect.
  471. Alternatively, the test can be rewritten to capture the output and compare it
  472. to an expected value as part of the test. This handling of tabs in the
  473. source was arrived at through trial and error, and has proven to be the least
  474. error prone way of handling them. It is possible to use a different
  475. algorithm for handling tabs by writing a custom <a class="reference internal" href="#doctest.DocTestParser" title="doctest.DocTestParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestParser</span></code></a> class.</p></li>
  476. <li><p>Output to stdout is captured, but not output to stderr (exception tracebacks
  477. are captured via a different means).</p></li>
  478. <li><p>If you continue a line via backslashing in an interactive session, or for any
  479. other reason use a backslash, you should use a raw docstring, which will
  480. preserve your backslashes exactly as you type them:</p>
  481. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
  482. <span class="gp">... </span><span class="w"> </span><span class="sa">r</span><span class="sd">&#39;&#39;&#39;Backslashes in a raw docstring: m\n&#39;&#39;&#39;</span>
  483. <span class="gp">...</span>
  484. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="vm">__doc__</span><span class="p">)</span>
  485. <span class="go">Backslashes in a raw docstring: m\n</span>
  486. </pre></div>
  487. </div>
  488. <p>Otherwise, the backslash will be interpreted as part of the string. For example,
  489. the <code class="docutils literal notranslate"><span class="pre">\n</span></code> above would be interpreted as a newline character. Alternatively, you
  490. can double each backslash in the doctest version (and not use a raw string):</p>
  491. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
  492. <span class="gp">... </span><span class="w"> </span><span class="sd">&#39;&#39;&#39;Backslashes in a raw docstring: m\\n&#39;&#39;&#39;</span>
  493. <span class="gp">...</span>
  494. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="vm">__doc__</span><span class="p">)</span>
  495. <span class="go">Backslashes in a raw docstring: m\n</span>
  496. </pre></div>
  497. </div>
  498. </li>
  499. <li><p>The starting column doesn’t matter:</p>
  500. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">assert</span> <span class="s2">&quot;Easy!&quot;</span>
  501. <span class="go"> &gt;&gt;&gt; import math</span>
  502. <span class="go"> &gt;&gt;&gt; math.floor(1.9)</span>
  503. <span class="go"> 1</span>
  504. </pre></div>
  505. </div>
  506. <p>and as many leading whitespace characters are stripped from the expected output
  507. as appeared in the initial <code class="docutils literal notranslate"><span class="pre">'&gt;&gt;&gt;</span> <span class="pre">'</span></code> line that started the example.</p>
  508. </li>
  509. </ul>
  510. </section>
  511. <section id="what-s-the-execution-context">
  512. <span id="doctest-execution-context"></span><h3>What’s the Execution Context?<a class="headerlink" href="#what-s-the-execution-context" title="Link to this heading">¶</a></h3>
  513. <p>By default, each time <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> finds a docstring to test, it uses a
  514. <em>shallow copy</em> of <code class="xref py py-mod docutils literal notranslate"><span class="pre">M</span></code>’s globals, so that running tests doesn’t change the
  515. module’s real globals, and so that one test in <code class="xref py py-mod docutils literal notranslate"><span class="pre">M</span></code> can’t leave behind
  516. crumbs that accidentally allow another test to work. This means examples can
  517. freely use any names defined at top-level in <code class="xref py py-mod docutils literal notranslate"><span class="pre">M</span></code>, and names defined earlier
  518. in the docstring being run. Examples cannot see names defined in other
  519. docstrings.</p>
  520. <p>You can force use of your own dict as the execution context by passing
  521. <code class="docutils literal notranslate"><span class="pre">globs=your_dict</span></code> to <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a> or <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a> instead.</p>
  522. </section>
  523. <section id="what-about-exceptions">
  524. <span id="doctest-exceptions"></span><h3>What About Exceptions?<a class="headerlink" href="#what-about-exceptions" title="Link to this heading">¶</a></h3>
  525. <p>No problem, provided that the traceback is the only output produced by the
  526. example: just paste in the traceback. <a class="footnote-reference brackets" href="#id2" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> Since tracebacks contain details
  527. that are likely to change rapidly (for example, exact file paths and line
  528. numbers), this is one case where doctest works hard to be flexible in what it
  529. accepts.</p>
  530. <p>Simple example:</p>
  531. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
  532. <span class="gt">Traceback (most recent call last):</span>
  533. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  534. <span class="gr">ValueError</span>: <span class="n">list.remove(x): x not in list</span>
  535. </pre></div>
  536. </div>
  537. <p>That doctest succeeds if <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised, with the <code class="docutils literal notranslate"><span class="pre">list.remove(x):</span>
  538. <span class="pre">x</span> <span class="pre">not</span> <span class="pre">in</span> <span class="pre">list</span></code> detail as shown.</p>
  539. <p>The expected output for an exception must start with a traceback header, which
  540. may be either of the following two lines, indented the same as the first line of
  541. the example:</p>
  542. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
  543. <span class="n">Traceback</span> <span class="p">(</span><span class="n">innermost</span> <span class="n">last</span><span class="p">):</span>
  544. </pre></div>
  545. </div>
  546. <p>The traceback header is followed by an optional traceback stack, whose contents
  547. are ignored by doctest. The traceback stack is typically omitted, or copied
  548. verbatim from an interactive session.</p>
  549. <p>The traceback stack is followed by the most interesting part: the line(s)
  550. containing the exception type and detail. This is usually the last line of a
  551. traceback, but can extend across multiple lines if the exception has a
  552. multi-line detail:</p>
  553. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;multi</span><span class="se">\n</span><span class="s1"> line</span><span class="se">\n</span><span class="s1">detail&#39;</span><span class="p">)</span>
  554. <span class="gt">Traceback (most recent call last):</span>
  555. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  556. <span class="gr">ValueError</span>: <span class="n">multi</span>
  557. <span class="x"> line</span>
  558. <span class="x">detail</span>
  559. </pre></div>
  560. </div>
  561. <p>The last three lines (starting with <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>) are compared against the
  562. exception’s type and detail, and the rest are ignored.</p>
  563. <p>Best practice is to omit the traceback stack, unless it adds significant
  564. documentation value to the example. So the last example is probably better as:</p>
  565. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;multi</span><span class="se">\n</span><span class="s1"> line</span><span class="se">\n</span><span class="s1">detail&#39;</span><span class="p">)</span>
  566. <span class="gt">Traceback (most recent call last):</span>
  567. <span class="w"> </span><span class="o">...</span>
  568. <span class="gr">ValueError</span>: <span class="n">multi</span>
  569. <span class="x"> line</span>
  570. <span class="x">detail</span>
  571. </pre></div>
  572. </div>
  573. <p>Note that tracebacks are treated very specially. In particular, in the
  574. rewritten example, the use of <code class="docutils literal notranslate"><span class="pre">...</span></code> is independent of doctest’s
  575. <a class="reference internal" href="#doctest.ELLIPSIS" title="doctest.ELLIPSIS"><code class="xref py py-const docutils literal notranslate"><span class="pre">ELLIPSIS</span></code></a> option. The ellipsis in that example could be left out, or
  576. could just as well be three (or three hundred) commas or digits, or an indented
  577. transcript of a Monty Python skit.</p>
  578. <p>Some details you should read once, but won’t need to remember:</p>
  579. <ul class="simple">
  580. <li><p>Doctest can’t guess whether your expected output came from an exception
  581. traceback or from ordinary printing. So, e.g., an example that expects
  582. <code class="docutils literal notranslate"><span class="pre">ValueError:</span> <span class="pre">42</span> <span class="pre">is</span> <span class="pre">prime</span></code> will pass whether <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is actually
  583. raised or if the example merely prints that traceback text. In practice,
  584. ordinary output rarely begins with a traceback header line, so this doesn’t
  585. create real problems.</p></li>
  586. <li><p>Each line of the traceback stack (if present) must be indented further than
  587. the first line of the example, <em>or</em> start with a non-alphanumeric character.
  588. The first line following the traceback header indented the same and starting
  589. with an alphanumeric is taken to be the start of the exception detail. Of
  590. course this does the right thing for genuine tracebacks.</p></li>
  591. <li><p>When the <a class="reference internal" href="#doctest.IGNORE_EXCEPTION_DETAIL" title="doctest.IGNORE_EXCEPTION_DETAIL"><code class="xref py py-const docutils literal notranslate"><span class="pre">IGNORE_EXCEPTION_DETAIL</span></code></a> doctest option is specified,
  592. everything following the leftmost colon and any module information in the
  593. exception name is ignored.</p></li>
  594. <li><p>The interactive shell omits the traceback header line for some
  595. <a class="reference internal" href="exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a>s. But doctest uses the traceback header line to
  596. distinguish exceptions from non-exceptions. So in the rare case where you need
  597. to test a <a class="reference internal" href="exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> that omits the traceback header, you will need to
  598. manually add the traceback header line to your test example.</p></li>
  599. </ul>
  600. <ul id="index-1">
  601. <li><p>For some exceptions, Python displays the position of the error using <code class="docutils literal notranslate"><span class="pre">^</span></code>
  602. markers and tildes:</p>
  603. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="mi">1</span> <span class="o">+</span> <span class="kc">None</span>
  604. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>
  605. <span class="w"> </span><span class="mi">1</span> <span class="o">+</span> <span class="kc">None</span>
  606. <span class="w"> </span><span class="pm">~~^~~~~~</span>
  607. <span class="gr">TypeError</span>: <span class="n">unsupported operand type(s) for +: &#39;int&#39; and &#39;NoneType&#39;</span>
  608. </pre></div>
  609. </div>
  610. <p>Since the lines showing the position of the error come before the exception type
  611. and detail, they are not checked by doctest. For example, the following test
  612. would pass, even though it puts the <code class="docutils literal notranslate"><span class="pre">^</span></code> marker in the wrong location:</p>
  613. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="mi">1</span> <span class="o">+</span> <span class="kc">None</span>
  614. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>
  615. <span class="w"> </span><span class="mi">1</span> <span class="o">+</span> <span class="kc">None</span>
  616. <span class="w"> </span><span class="pm">^~~~~~~~</span>
  617. <span class="gr">TypeError</span>: <span class="n">unsupported operand type(s) for +: &#39;int&#39; and &#39;NoneType&#39;</span>
  618. </pre></div>
  619. </div>
  620. </li>
  621. </ul>
  622. </section>
  623. <section id="option-flags">
  624. <span id="doctest-options"></span><span id="option-flags-and-directives"></span><h3>Option Flags<a class="headerlink" href="#option-flags" title="Link to this heading">¶</a></h3>
  625. <p>A number of option flags control various aspects of doctest’s behavior.
  626. Symbolic names for the flags are supplied as module constants, which can be
  627. <a class="reference internal" href="../reference/expressions.html#bitwise"><span class="std std-ref">bitwise ORed</span></a> together and passed to various functions.
  628. The names can also be used in <a class="reference internal" href="#doctest-directives"><span class="std std-ref">doctest directives</span></a>,
  629. and may be passed to the doctest command line interface via the <code class="docutils literal notranslate"><span class="pre">-o</span></code> option.</p>
  630. <div class="versionadded">
  631. <p><span class="versionmodified added">New in version 3.4: </span>The <code class="docutils literal notranslate"><span class="pre">-o</span></code> command line option.</p>
  632. </div>
  633. <p>The first group of options define test semantics, controlling aspects of how
  634. doctest decides whether actual output matches an example’s expected output:</p>
  635. <dl class="py data">
  636. <dt class="sig sig-object py" id="doctest.DONT_ACCEPT_TRUE_FOR_1">
  637. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DONT_ACCEPT_TRUE_FOR_1</span></span><a class="headerlink" href="#doctest.DONT_ACCEPT_TRUE_FOR_1" title="Link to this definition">¶</a></dt>
  638. <dd><p>By default, if an expected output block contains just <code class="docutils literal notranslate"><span class="pre">1</span></code>, an actual output
  639. block containing just <code class="docutils literal notranslate"><span class="pre">1</span></code> or just <code class="docutils literal notranslate"><span class="pre">True</span></code> is considered to be a match, and
  640. similarly for <code class="docutils literal notranslate"><span class="pre">0</span></code> versus <code class="docutils literal notranslate"><span class="pre">False</span></code>. When <a class="reference internal" href="#doctest.DONT_ACCEPT_TRUE_FOR_1" title="doctest.DONT_ACCEPT_TRUE_FOR_1"><code class="xref py py-const docutils literal notranslate"><span class="pre">DONT_ACCEPT_TRUE_FOR_1</span></code></a> is
  641. specified, neither substitution is allowed. The default behavior caters to that
  642. Python changed the return type of many functions from integer to boolean;
  643. doctests expecting “little integer” output still work in these cases. This
  644. option will probably go away, but not for several years.</p>
  645. </dd></dl>
  646. <dl class="py data" id="index-2">
  647. <dt class="sig sig-object py" id="doctest.DONT_ACCEPT_BLANKLINE">
  648. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DONT_ACCEPT_BLANKLINE</span></span><a class="headerlink" href="#doctest.DONT_ACCEPT_BLANKLINE" title="Link to this definition">¶</a></dt>
  649. <dd><p>By default, if an expected output block contains a line containing only the
  650. string <code class="docutils literal notranslate"><span class="pre">&lt;BLANKLINE&gt;</span></code>, then that line will match a blank line in the actual
  651. output. Because a genuinely blank line delimits the expected output, this is
  652. the only way to communicate that a blank line is expected. When
  653. <a class="reference internal" href="#doctest.DONT_ACCEPT_BLANKLINE" title="doctest.DONT_ACCEPT_BLANKLINE"><code class="xref py py-const docutils literal notranslate"><span class="pre">DONT_ACCEPT_BLANKLINE</span></code></a> is specified, this substitution is not allowed.</p>
  654. </dd></dl>
  655. <dl class="py data">
  656. <dt class="sig sig-object py" id="doctest.NORMALIZE_WHITESPACE">
  657. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">NORMALIZE_WHITESPACE</span></span><a class="headerlink" href="#doctest.NORMALIZE_WHITESPACE" title="Link to this definition">¶</a></dt>
  658. <dd><p>When specified, all sequences of whitespace (blanks and newlines) are treated as
  659. equal. Any sequence of whitespace within the expected output will match any
  660. sequence of whitespace within the actual output. By default, whitespace must
  661. match exactly. <a class="reference internal" href="#doctest.NORMALIZE_WHITESPACE" title="doctest.NORMALIZE_WHITESPACE"><code class="xref py py-const docutils literal notranslate"><span class="pre">NORMALIZE_WHITESPACE</span></code></a> is especially useful when a line of
  662. expected output is very long, and you want to wrap it across multiple lines in
  663. your source.</p>
  664. </dd></dl>
  665. <dl class="py data" id="index-3">
  666. <dt class="sig sig-object py" id="doctest.ELLIPSIS">
  667. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">ELLIPSIS</span></span><a class="headerlink" href="#doctest.ELLIPSIS" title="Link to this definition">¶</a></dt>
  668. <dd><p>When specified, an ellipsis marker (<code class="docutils literal notranslate"><span class="pre">...</span></code>) in the expected output can match
  669. any substring in the actual output. This includes substrings that span line
  670. boundaries, and empty substrings, so it’s best to keep usage of this simple.
  671. Complicated uses can lead to the same kinds of “oops, it matched too much!”
  672. surprises that <code class="docutils literal notranslate"><span class="pre">.*</span></code> is prone to in regular expressions.</p>
  673. </dd></dl>
  674. <dl class="py data">
  675. <dt class="sig sig-object py" id="doctest.IGNORE_EXCEPTION_DETAIL">
  676. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">IGNORE_EXCEPTION_DETAIL</span></span><a class="headerlink" href="#doctest.IGNORE_EXCEPTION_DETAIL" title="Link to this definition">¶</a></dt>
  677. <dd><p>When specified, doctests expecting exceptions pass so long as an exception
  678. of the expected type is raised, even if the details
  679. (message and fully qualified exception name) don’t match.</p>
  680. <p>For example, an example expecting <code class="docutils literal notranslate"><span class="pre">ValueError:</span> <span class="pre">42</span></code> will pass if the actual
  681. exception raised is <code class="docutils literal notranslate"><span class="pre">ValueError:</span> <span class="pre">3*14</span></code>, but will fail if, say, a
  682. <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised instead.
  683. It will also ignore any fully qualified name included before the
  684. exception class, which can vary between implementations and versions
  685. of Python and the code/libraries in use.
  686. Hence, all three of these variations will work with the flag specified:</p>
  687. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;message&#39;</span><span class="p">)</span>
  688. <span class="gt">Traceback (most recent call last):</span>
  689. <span class="gr">Exception</span>: <span class="n">message</span>
  690. <span class="gp">&gt;&gt;&gt; </span><span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;message&#39;</span><span class="p">)</span>
  691. <span class="gt">Traceback (most recent call last):</span>
  692. <span class="gr">builtins.Exception</span>: <span class="n">message</span>
  693. <span class="gp">&gt;&gt;&gt; </span><span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;message&#39;</span><span class="p">)</span>
  694. <span class="gt">Traceback (most recent call last):</span>
  695. <span class="gr">__main__.Exception</span>: <span class="n">message</span>
  696. </pre></div>
  697. </div>
  698. <p>Note that <a class="reference internal" href="#doctest.ELLIPSIS" title="doctest.ELLIPSIS"><code class="xref py py-const docutils literal notranslate"><span class="pre">ELLIPSIS</span></code></a> can also be used to ignore the
  699. details of the exception message, but such a test may still fail based
  700. on whether the module name is present or matches exactly.</p>
  701. <div class="versionchanged">
  702. <p><span class="versionmodified changed">Changed in version 3.2: </span><a class="reference internal" href="#doctest.IGNORE_EXCEPTION_DETAIL" title="doctest.IGNORE_EXCEPTION_DETAIL"><code class="xref py py-const docutils literal notranslate"><span class="pre">IGNORE_EXCEPTION_DETAIL</span></code></a> now also ignores any information relating
  703. to the module containing the exception under test.</p>
  704. </div>
  705. </dd></dl>
  706. <dl class="py data">
  707. <dt class="sig sig-object py" id="doctest.SKIP">
  708. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">SKIP</span></span><a class="headerlink" href="#doctest.SKIP" title="Link to this definition">¶</a></dt>
  709. <dd><p>When specified, do not run the example at all. This can be useful in contexts
  710. where doctest examples serve as both documentation and test cases, and an
  711. example should be included for documentation purposes, but should not be
  712. checked. E.g., the example’s output might be random; or the example might
  713. depend on resources which would be unavailable to the test driver.</p>
  714. <p>The SKIP flag can also be used for temporarily “commenting out” examples.</p>
  715. </dd></dl>
  716. <dl class="py data">
  717. <dt class="sig sig-object py" id="doctest.COMPARISON_FLAGS">
  718. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">COMPARISON_FLAGS</span></span><a class="headerlink" href="#doctest.COMPARISON_FLAGS" title="Link to this definition">¶</a></dt>
  719. <dd><p>A bitmask or’ing together all the comparison flags above.</p>
  720. </dd></dl>
  721. <p>The second group of options controls how test failures are reported:</p>
  722. <dl class="py data">
  723. <dt class="sig sig-object py" id="doctest.REPORT_UDIFF">
  724. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">REPORT_UDIFF</span></span><a class="headerlink" href="#doctest.REPORT_UDIFF" title="Link to this definition">¶</a></dt>
  725. <dd><p>When specified, failures that involve multi-line expected and actual outputs are
  726. displayed using a unified diff.</p>
  727. </dd></dl>
  728. <dl class="py data">
  729. <dt class="sig sig-object py" id="doctest.REPORT_CDIFF">
  730. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">REPORT_CDIFF</span></span><a class="headerlink" href="#doctest.REPORT_CDIFF" title="Link to this definition">¶</a></dt>
  731. <dd><p>When specified, failures that involve multi-line expected and actual outputs
  732. will be displayed using a context diff.</p>
  733. </dd></dl>
  734. <dl class="py data">
  735. <dt class="sig sig-object py" id="doctest.REPORT_NDIFF">
  736. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">REPORT_NDIFF</span></span><a class="headerlink" href="#doctest.REPORT_NDIFF" title="Link to this definition">¶</a></dt>
  737. <dd><p>When specified, differences are computed by <code class="docutils literal notranslate"><span class="pre">difflib.Differ</span></code>, using the same
  738. algorithm as the popular <code class="file docutils literal notranslate"><span class="pre">ndiff.py</span></code> utility. This is the only method that
  739. marks differences within lines as well as across lines. For example, if a line
  740. of expected output contains digit <code class="docutils literal notranslate"><span class="pre">1</span></code> where actual output contains letter
  741. <code class="docutils literal notranslate"><span class="pre">l</span></code>, a line is inserted with a caret marking the mismatching column positions.</p>
  742. </dd></dl>
  743. <dl class="py data">
  744. <dt class="sig sig-object py" id="doctest.REPORT_ONLY_FIRST_FAILURE">
  745. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">REPORT_ONLY_FIRST_FAILURE</span></span><a class="headerlink" href="#doctest.REPORT_ONLY_FIRST_FAILURE" title="Link to this definition">¶</a></dt>
  746. <dd><p>When specified, display the first failing example in each doctest, but suppress
  747. output for all remaining examples. This will prevent doctest from reporting
  748. correct examples that break because of earlier failures; but it might also hide
  749. incorrect examples that fail independently of the first failure. When
  750. <a class="reference internal" href="#doctest.REPORT_ONLY_FIRST_FAILURE" title="doctest.REPORT_ONLY_FIRST_FAILURE"><code class="xref py py-const docutils literal notranslate"><span class="pre">REPORT_ONLY_FIRST_FAILURE</span></code></a> is specified, the remaining examples are
  751. still run, and still count towards the total number of failures reported; only
  752. the output is suppressed.</p>
  753. </dd></dl>
  754. <dl class="py data">
  755. <dt class="sig sig-object py" id="doctest.FAIL_FAST">
  756. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">FAIL_FAST</span></span><a class="headerlink" href="#doctest.FAIL_FAST" title="Link to this definition">¶</a></dt>
  757. <dd><p>When specified, exit after the first failing example and don’t attempt to run
  758. the remaining examples. Thus, the number of failures reported will be at most
  759. 1. This flag may be useful during debugging, since examples after the first
  760. failure won’t even produce debugging output.</p>
  761. <p>The doctest command line accepts the option <code class="docutils literal notranslate"><span class="pre">-f</span></code> as a shorthand for <code class="docutils literal notranslate"><span class="pre">-o</span>
  762. <span class="pre">FAIL_FAST</span></code>.</p>
  763. <div class="versionadded">
  764. <p><span class="versionmodified added">New in version 3.4.</span></p>
  765. </div>
  766. </dd></dl>
  767. <dl class="py data">
  768. <dt class="sig sig-object py" id="doctest.REPORTING_FLAGS">
  769. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">REPORTING_FLAGS</span></span><a class="headerlink" href="#doctest.REPORTING_FLAGS" title="Link to this definition">¶</a></dt>
  770. <dd><p>A bitmask or’ing together all the reporting flags above.</p>
  771. </dd></dl>
  772. <p>There is also a way to register new option flag names, though this isn’t
  773. useful unless you intend to extend <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> internals via subclassing:</p>
  774. <dl class="py function">
  775. <dt class="sig sig-object py" id="doctest.register_optionflag">
  776. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">register_optionflag</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.register_optionflag" title="Link to this definition">¶</a></dt>
  777. <dd><p>Create a new option flag with a given name, and return the new flag’s integer
  778. value. <a class="reference internal" href="#doctest.register_optionflag" title="doctest.register_optionflag"><code class="xref py py-func docutils literal notranslate"><span class="pre">register_optionflag()</span></code></a> can be used when subclassing
  779. <a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a> or <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> to create new options that are
  780. supported by your subclasses. <a class="reference internal" href="#doctest.register_optionflag" title="doctest.register_optionflag"><code class="xref py py-func docutils literal notranslate"><span class="pre">register_optionflag()</span></code></a> should always be
  781. called using the following idiom:</p>
  782. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">MY_FLAG</span> <span class="o">=</span> <span class="n">register_optionflag</span><span class="p">(</span><span class="s1">&#39;MY_FLAG&#39;</span><span class="p">)</span>
  783. </pre></div>
  784. </div>
  785. </dd></dl>
  786. </section>
  787. <section id="directives">
  788. <span id="doctest-directives"></span><span id="index-4"></span><h3>Directives<a class="headerlink" href="#directives" title="Link to this heading">¶</a></h3>
  789. <p>Doctest directives may be used to modify the <a class="reference internal" href="#doctest-options"><span class="std std-ref">option flags</span></a> for an individual example. Doctest directives are
  790. special Python comments following an example’s source code:</p>
  791. <pre>
  792. <strong id="grammar-token-doctest-directive">directive </strong> ::= &quot;#&quot; &quot;doctest:&quot; <a class="reference internal" href="#grammar-token-doctest-directive_options"><code class="xref docutils literal notranslate"><span class="pre">directive_options</span></code></a>
  793. <strong id="grammar-token-doctest-directive_options">directive_options </strong> ::= <a class="reference internal" href="#grammar-token-doctest-directive_option"><code class="xref docutils literal notranslate"><span class="pre">directive_option</span></code></a> (&quot;,&quot; <a class="reference internal" href="#grammar-token-doctest-directive_option"><code class="xref docutils literal notranslate"><span class="pre">directive_option</span></code></a>)*
  794. <strong id="grammar-token-doctest-directive_option">directive_option </strong> ::= <a class="reference internal" href="#grammar-token-doctest-on_or_off"><code class="xref docutils literal notranslate"><span class="pre">on_or_off</span></code></a> <a class="reference internal" href="#grammar-token-doctest-directive_option_name"><code class="xref docutils literal notranslate"><span class="pre">directive_option_name</span></code></a>
  795. <strong id="grammar-token-doctest-on_or_off">on_or_off </strong> ::= &quot;+&quot; | &quot;-&quot;
  796. <strong id="grammar-token-doctest-directive_option_name">directive_option_name</strong> ::= &quot;DONT_ACCEPT_BLANKLINE&quot; | &quot;NORMALIZE_WHITESPACE&quot; | ...
  797. </pre>
  798. <p>Whitespace is not allowed between the <code class="docutils literal notranslate"><span class="pre">+</span></code> or <code class="docutils literal notranslate"><span class="pre">-</span></code> and the directive option
  799. name. The directive option name can be any of the option flag names explained
  800. above.</p>
  801. <p>An example’s doctest directives modify doctest’s behavior for that single
  802. example. Use <code class="docutils literal notranslate"><span class="pre">+</span></code> to enable the named behavior, or <code class="docutils literal notranslate"><span class="pre">-</span></code> to disable it.</p>
  803. <p>For example, this test passes:</p>
  804. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">20</span><span class="p">)))</span> <span class="c1"># doctest: +NORMALIZE_WHITESPACE</span>
  805. <span class="go">[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,</span>
  806. <span class="go">10, 11, 12, 13, 14, 15, 16, 17, 18, 19]</span>
  807. </pre></div>
  808. </div>
  809. <p>Without the directive it would fail, both because the actual output doesn’t have
  810. two blanks before the single-digit list elements, and because the actual output
  811. is on a single line. This test also passes, and also requires a directive to do
  812. so:</p>
  813. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">20</span><span class="p">)))</span> <span class="c1"># doctest: +ELLIPSIS</span>
  814. <span class="go">[0, 1, ..., 18, 19]</span>
  815. </pre></div>
  816. </div>
  817. <p>Multiple directives can be used on a single physical line, separated by
  818. commas:</p>
  819. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">20</span><span class="p">)))</span> <span class="c1"># doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE</span>
  820. <span class="go">[0, 1, ..., 18, 19]</span>
  821. </pre></div>
  822. </div>
  823. <p>If multiple directive comments are used for a single example, then they are
  824. combined:</p>
  825. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">20</span><span class="p">)))</span> <span class="c1"># doctest: +ELLIPSIS</span>
  826. <span class="gp">... </span> <span class="c1"># doctest: +NORMALIZE_WHITESPACE</span>
  827. <span class="go">[0, 1, ..., 18, 19]</span>
  828. </pre></div>
  829. </div>
  830. <p>As the previous example shows, you can add <code class="docutils literal notranslate"><span class="pre">...</span></code> lines to your example
  831. containing only directives. This can be useful when an example is too long for
  832. a directive to comfortably fit on the same line:</p>
  833. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span> <span class="o">+</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">))</span> <span class="o">+</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">30</span><span class="p">,</span> <span class="mi">40</span><span class="p">)))</span>
  834. <span class="gp">... </span><span class="c1"># doctest: +ELLIPSIS</span>
  835. <span class="go">[0, ..., 4, 10, ..., 19, 30, ..., 39]</span>
  836. </pre></div>
  837. </div>
  838. <p>Note that since all options are disabled by default, and directives apply only
  839. to the example they appear in, enabling options (via <code class="docutils literal notranslate"><span class="pre">+</span></code> in a directive) is
  840. usually the only meaningful choice. However, option flags can also be passed to
  841. functions that run doctests, establishing different defaults. In such cases,
  842. disabling an option via <code class="docutils literal notranslate"><span class="pre">-</span></code> in a directive can be useful.</p>
  843. </section>
  844. <section id="warnings">
  845. <span id="doctest-warnings"></span><h3>Warnings<a class="headerlink" href="#warnings" title="Link to this heading">¶</a></h3>
  846. <p><a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> is serious about requiring exact matches in expected output. If
  847. even a single character doesn’t match, the test fails. This will probably
  848. surprise you a few times, as you learn exactly what Python does and doesn’t
  849. guarantee about output. For example, when printing a set, Python doesn’t
  850. guarantee that the element is printed in any particular order, so a test like</p>
  851. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">foo</span><span class="p">()</span>
  852. <span class="go">{&quot;Hermione&quot;, &quot;Harry&quot;}</span>
  853. </pre></div>
  854. </div>
  855. <p>is vulnerable! One workaround is to do</p>
  856. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">foo</span><span class="p">()</span> <span class="o">==</span> <span class="p">{</span><span class="s2">&quot;Hermione&quot;</span><span class="p">,</span> <span class="s2">&quot;Harry&quot;</span><span class="p">}</span>
  857. <span class="go">True</span>
  858. </pre></div>
  859. </div>
  860. <p>instead. Another is to do</p>
  861. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">foo</span><span class="p">())</span>
  862. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span>
  863. <span class="go">[&#39;Harry&#39;, &#39;Hermione&#39;]</span>
  864. </pre></div>
  865. </div>
  866. <p>There are others, but you get the idea.</p>
  867. <p>Another bad idea is to print things that embed an object address, like</p>
  868. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">id</span><span class="p">(</span><span class="mf">1.0</span><span class="p">)</span> <span class="c1"># certain to fail some of the time </span>
  869. <span class="go">7948648</span>
  870. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span> <span class="k">pass</span>
  871. <span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">()</span> <span class="c1"># the default repr() for instances embeds an address </span>
  872. <span class="go">&lt;C object at 0x00AC18F0&gt;</span>
  873. </pre></div>
  874. </div>
  875. <p>The <a class="reference internal" href="#doctest.ELLIPSIS" title="doctest.ELLIPSIS"><code class="xref py py-const docutils literal notranslate"><span class="pre">ELLIPSIS</span></code></a> directive gives a nice approach for the last example:</p>
  876. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">()</span> <span class="c1"># doctest: +ELLIPSIS</span>
  877. <span class="go">&lt;C object at 0x...&gt;</span>
  878. </pre></div>
  879. </div>
  880. <p>Floating-point numbers are also subject to small output variations across
  881. platforms, because Python defers to the platform C library for float formatting,
  882. and C libraries vary widely in quality here.</p>
  883. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="mf">1.</span><span class="o">/</span><span class="mi">7</span> <span class="c1"># risky</span>
  884. <span class="go">0.14285714285714285</span>
  885. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="mi">7</span><span class="p">)</span> <span class="c1"># safer</span>
  886. <span class="go">0.142857142857</span>
  887. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="mi">7</span><span class="p">,</span> <span class="mi">6</span><span class="p">))</span> <span class="c1"># much safer</span>
  888. <span class="go">0.142857</span>
  889. </pre></div>
  890. </div>
  891. <p>Numbers of the form <code class="docutils literal notranslate"><span class="pre">I/2.**J</span></code> are safe across all platforms, and I often
  892. contrive doctest examples to produce numbers of that form:</p>
  893. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="mf">3.</span><span class="o">/</span><span class="mi">4</span> <span class="c1"># utterly safe</span>
  894. <span class="go">0.75</span>
  895. </pre></div>
  896. </div>
  897. <p>Simple fractions are also easier for people to understand, and that makes for
  898. better documentation.</p>
  899. </section>
  900. </section>
  901. <section id="basic-api">
  902. <span id="doctest-basic-api"></span><h2>Basic API<a class="headerlink" href="#basic-api" title="Link to this heading">¶</a></h2>
  903. <p>The functions <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a> and <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a> provide a simple interface to
  904. doctest that should be sufficient for most basic uses. For a less formal
  905. introduction to these two functions, see sections <a class="reference internal" href="#doctest-simple-testmod"><span class="std std-ref">Simple Usage: Checking Examples in Docstrings</span></a>
  906. and <a class="reference internal" href="#doctest-simple-testfile"><span class="std std-ref">Simple Usage: Checking Examples in a Text File</span></a>.</p>
  907. <dl class="py function">
  908. <dt class="sig sig-object py" id="doctest.testfile">
  909. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">testfile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">module_relative</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">package</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">report</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optionflags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">extraglobs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">raise_on_error</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">DocTestParser()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.testfile" title="Link to this definition">¶</a></dt>
  910. <dd><p>All arguments except <em>filename</em> are optional, and should be specified in keyword
  911. form.</p>
  912. <p>Test examples in the file named <em>filename</em>. Return <code class="docutils literal notranslate"><span class="pre">(failure_count,</span>
  913. <span class="pre">test_count)</span></code>.</p>
  914. <p>Optional argument <em>module_relative</em> specifies how the filename should be
  915. interpreted:</p>
  916. <ul class="simple">
  917. <li><p>If <em>module_relative</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default), then <em>filename</em> specifies an
  918. OS-independent module-relative path. By default, this path is relative to the
  919. calling module’s directory; but if the <em>package</em> argument is specified, then it
  920. is relative to that package. To ensure OS-independence, <em>filename</em> should use
  921. <code class="docutils literal notranslate"><span class="pre">/</span></code> characters to separate path segments, and may not be an absolute path
  922. (i.e., it may not begin with <code class="docutils literal notranslate"><span class="pre">/</span></code>).</p></li>
  923. <li><p>If <em>module_relative</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, then <em>filename</em> specifies an OS-specific
  924. path. The path may be absolute or relative; relative paths are resolved with
  925. respect to the current working directory.</p></li>
  926. </ul>
  927. <p>Optional argument <em>name</em> gives the name of the test; by default, or if <code class="docutils literal notranslate"><span class="pre">None</span></code>,
  928. <code class="docutils literal notranslate"><span class="pre">os.path.basename(filename)</span></code> is used.</p>
  929. <p>Optional argument <em>package</em> is a Python package or the name of a Python package
  930. whose directory should be used as the base directory for a module-relative
  931. filename. If no package is specified, then the calling module’s directory is
  932. used as the base directory for module-relative filenames. It is an error to
  933. specify <em>package</em> if <em>module_relative</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  934. <p>Optional argument <em>globs</em> gives a dict to be used as the globals when executing
  935. examples. A new shallow copy of this dict is created for the doctest, so its
  936. examples start with a clean slate. By default, or if <code class="docutils literal notranslate"><span class="pre">None</span></code>, a new empty dict
  937. is used.</p>
  938. <p>Optional argument <em>extraglobs</em> gives a dict merged into the globals used to
  939. execute examples. This works like <a class="reference internal" href="stdtypes.html#dict.update" title="dict.update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.update()</span></code></a>: if <em>globs</em> and
  940. <em>extraglobs</em> have a common key, the associated value in <em>extraglobs</em> appears in
  941. the combined dict. By default, or if <code class="docutils literal notranslate"><span class="pre">None</span></code>, no extra globals are used. This
  942. is an advanced feature that allows parameterization of doctests. For example, a
  943. doctest can be written for a base class, using a generic name for the class,
  944. then reused to test any number of subclasses by passing an <em>extraglobs</em> dict
  945. mapping the generic name to the subclass to be tested.</p>
  946. <p>Optional argument <em>verbose</em> prints lots of stuff if true, and prints only
  947. failures if false; by default, or if <code class="docutils literal notranslate"><span class="pre">None</span></code>, it’s true if and only if <code class="docutils literal notranslate"><span class="pre">'-v'</span></code>
  948. is in <code class="docutils literal notranslate"><span class="pre">sys.argv</span></code>.</p>
  949. <p>Optional argument <em>report</em> prints a summary at the end when true, else prints
  950. nothing at the end. In verbose mode, the summary is detailed, else the summary
  951. is very brief (in fact, empty if all tests passed).</p>
  952. <p>Optional argument <em>optionflags</em> (default value 0) takes the
  953. <a class="reference internal" href="../reference/expressions.html#bitwise"><span class="std std-ref">bitwise OR</span></a> of option flags.
  954. See section <a class="reference internal" href="#doctest-options"><span class="std std-ref">Option Flags</span></a>.</p>
  955. <p>Optional argument <em>raise_on_error</em> defaults to false. If true, an exception is
  956. raised upon the first failure or unexpected exception in an example. This
  957. allows failures to be post-mortem debugged. Default behavior is to continue
  958. running examples.</p>
  959. <p>Optional argument <em>parser</em> specifies a <a class="reference internal" href="#doctest.DocTestParser" title="doctest.DocTestParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestParser</span></code></a> (or subclass) that
  960. should be used to extract tests from the files. It defaults to a normal parser
  961. (i.e., <code class="docutils literal notranslate"><span class="pre">DocTestParser()</span></code>).</p>
  962. <p>Optional argument <em>encoding</em> specifies an encoding that should be used to
  963. convert the file to unicode.</p>
  964. </dd></dl>
  965. <dl class="py function">
  966. <dt class="sig sig-object py" id="doctest.testmod">
  967. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">testmod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">m</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">report</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optionflags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">extraglobs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">raise_on_error</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exclude_empty</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.testmod" title="Link to this definition">¶</a></dt>
  968. <dd><p>All arguments are optional, and all except for <em>m</em> should be specified in
  969. keyword form.</p>
  970. <p>Test examples in docstrings in functions and classes reachable from module <em>m</em>
  971. (or module <a class="reference internal" href="__main__.html#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> if <em>m</em> is not supplied or is <code class="docutils literal notranslate"><span class="pre">None</span></code>), starting with
  972. <code class="docutils literal notranslate"><span class="pre">m.__doc__</span></code>.</p>
  973. <p>Also test examples reachable from dict <code class="docutils literal notranslate"><span class="pre">m.__test__</span></code>, if it exists.
  974. <code class="docutils literal notranslate"><span class="pre">m.__test__</span></code> maps names (strings) to functions, classes and
  975. strings; function and class docstrings are searched for examples; strings are
  976. searched directly, as if they were docstrings.</p>
  977. <p>Only docstrings attached to objects belonging to module <em>m</em> are searched.</p>
  978. <p>Return <code class="docutils literal notranslate"><span class="pre">(failure_count,</span> <span class="pre">test_count)</span></code>.</p>
  979. <p>Optional argument <em>name</em> gives the name of the module; by default, or if
  980. <code class="docutils literal notranslate"><span class="pre">None</span></code>, <code class="docutils literal notranslate"><span class="pre">m.__name__</span></code> is used.</p>
  981. <p>Optional argument <em>exclude_empty</em> defaults to false. If true, objects for which
  982. no doctests are found are excluded from consideration. The default is a backward
  983. compatibility hack, so that code still using
  984. <a class="reference internal" href="#doctest.DocTestRunner.summarize" title="doctest.DocTestRunner.summarize"><code class="xref py py-meth docutils literal notranslate"><span class="pre">doctest.master.summarize</span></code></a> in
  985. conjunction with <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a> continues to get output for objects with no
  986. tests. The <em>exclude_empty</em> argument to the newer <a class="reference internal" href="#doctest.DocTestFinder" title="doctest.DocTestFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestFinder</span></code></a>
  987. constructor defaults to true.</p>
  988. <p>Optional arguments <em>extraglobs</em>, <em>verbose</em>, <em>report</em>, <em>optionflags</em>,
  989. <em>raise_on_error</em>, and <em>globs</em> are the same as for function <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a>
  990. above, except that <em>globs</em> defaults to <code class="docutils literal notranslate"><span class="pre">m.__dict__</span></code>.</p>
  991. </dd></dl>
  992. <dl class="py function">
  993. <dt class="sig sig-object py" id="doctest.run_docstring_examples">
  994. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">run_docstring_examples</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">f</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'NoName'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compileflags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optionflags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.run_docstring_examples" title="Link to this definition">¶</a></dt>
  995. <dd><p>Test examples associated with object <em>f</em>; for example, <em>f</em> may be a string,
  996. a module, a function, or a class object.</p>
  997. <p>A shallow copy of dictionary argument <em>globs</em> is used for the execution context.</p>
  998. <p>Optional argument <em>name</em> is used in failure messages, and defaults to
  999. <code class="docutils literal notranslate"><span class="pre">&quot;NoName&quot;</span></code>.</p>
  1000. <p>If optional argument <em>verbose</em> is true, output is generated even if there are no
  1001. failures. By default, output is generated only in case of an example failure.</p>
  1002. <p>Optional argument <em>compileflags</em> gives the set of flags that should be used by
  1003. the Python compiler when running the examples. By default, or if <code class="docutils literal notranslate"><span class="pre">None</span></code>,
  1004. flags are deduced corresponding to the set of future features found in <em>globs</em>.</p>
  1005. <p>Optional argument <em>optionflags</em> works as for function <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a> above.</p>
  1006. </dd></dl>
  1007. </section>
  1008. <section id="unittest-api">
  1009. <span id="doctest-unittest-api"></span><h2>Unittest API<a class="headerlink" href="#unittest-api" title="Link to this heading">¶</a></h2>
  1010. <p>As your collection of doctest’ed modules grows, you’ll want a way to run all
  1011. their doctests systematically. <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> provides two functions that can
  1012. be used to create <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> test suites from modules and text files
  1013. containing doctests. To integrate with <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> test discovery, include
  1014. a <a class="reference internal" href="unittest.html#load-tests-protocol"><span class="std std-ref">load_tests</span></a> function in your test module:</p>
  1015. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">unittest</span>
  1016. <span class="kn">import</span> <span class="nn">doctest</span>
  1017. <span class="kn">import</span> <span class="nn">my_module_with_doctests</span>
  1018. <span class="k">def</span> <span class="nf">load_tests</span><span class="p">(</span><span class="n">loader</span><span class="p">,</span> <span class="n">tests</span><span class="p">,</span> <span class="n">ignore</span><span class="p">):</span>
  1019. <span class="n">tests</span><span class="o">.</span><span class="n">addTests</span><span class="p">(</span><span class="n">doctest</span><span class="o">.</span><span class="n">DocTestSuite</span><span class="p">(</span><span class="n">my_module_with_doctests</span><span class="p">))</span>
  1020. <span class="k">return</span> <span class="n">tests</span>
  1021. </pre></div>
  1022. </div>
  1023. <p>There are two main functions for creating <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a> instances
  1024. from text files and modules with doctests:</p>
  1025. <dl class="py function">
  1026. <dt class="sig sig-object py" id="doctest.DocFileSuite">
  1027. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DocFileSuite</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">paths</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">module_relative</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">package</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">setUp</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tearDown</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optionflags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">DocTestParser()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocFileSuite" title="Link to this definition">¶</a></dt>
  1028. <dd><p>Convert doctest tests from one or more text files to a
  1029. <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a>.</p>
  1030. <p>The returned <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a> is to be run by the unittest framework
  1031. and runs the interactive examples in each file. If an example in any file
  1032. fails, then the synthesized unit test fails, and a <a class="reference internal" href="#doctest.failureException" title="doctest.failureException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">failureException</span></code></a>
  1033. exception is raised showing the name of the file containing the test and a
  1034. (sometimes approximate) line number.</p>
  1035. <p>Pass one or more paths (as strings) to text files to be examined.</p>
  1036. <p>Options may be provided as keyword arguments:</p>
  1037. <p>Optional argument <em>module_relative</em> specifies how the filenames in <em>paths</em>
  1038. should be interpreted:</p>
  1039. <ul class="simple">
  1040. <li><p>If <em>module_relative</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default), then each filename in
  1041. <em>paths</em> specifies an OS-independent module-relative path. By default, this
  1042. path is relative to the calling module’s directory; but if the <em>package</em>
  1043. argument is specified, then it is relative to that package. To ensure
  1044. OS-independence, each filename should use <code class="docutils literal notranslate"><span class="pre">/</span></code> characters to separate path
  1045. segments, and may not be an absolute path (i.e., it may not begin with
  1046. <code class="docutils literal notranslate"><span class="pre">/</span></code>).</p></li>
  1047. <li><p>If <em>module_relative</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, then each filename in <em>paths</em> specifies
  1048. an OS-specific path. The path may be absolute or relative; relative paths
  1049. are resolved with respect to the current working directory.</p></li>
  1050. </ul>
  1051. <p>Optional argument <em>package</em> is a Python package or the name of a Python
  1052. package whose directory should be used as the base directory for
  1053. module-relative filenames in <em>paths</em>. If no package is specified, then the
  1054. calling module’s directory is used as the base directory for module-relative
  1055. filenames. It is an error to specify <em>package</em> if <em>module_relative</em> is
  1056. <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  1057. <p>Optional argument <em>setUp</em> specifies a set-up function for the test suite.
  1058. This is called before running the tests in each file. The <em>setUp</em> function
  1059. will be passed a <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object. The setUp function can access the
  1060. test globals as the <em>globs</em> attribute of the test passed.</p>
  1061. <p>Optional argument <em>tearDown</em> specifies a tear-down function for the test
  1062. suite. This is called after running the tests in each file. The <em>tearDown</em>
  1063. function will be passed a <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object. The setUp function can
  1064. access the test globals as the <em>globs</em> attribute of the test passed.</p>
  1065. <p>Optional argument <em>globs</em> is a dictionary containing the initial global
  1066. variables for the tests. A new copy of this dictionary is created for each
  1067. test. By default, <em>globs</em> is a new empty dictionary.</p>
  1068. <p>Optional argument <em>optionflags</em> specifies the default doctest options for the
  1069. tests, created by or-ing together individual option flags. See section
  1070. <a class="reference internal" href="#doctest-options"><span class="std std-ref">Option Flags</span></a>. See function <a class="reference internal" href="#doctest.set_unittest_reportflags" title="doctest.set_unittest_reportflags"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_unittest_reportflags()</span></code></a> below
  1071. for a better way to set reporting options.</p>
  1072. <p>Optional argument <em>parser</em> specifies a <a class="reference internal" href="#doctest.DocTestParser" title="doctest.DocTestParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestParser</span></code></a> (or subclass)
  1073. that should be used to extract tests from the files. It defaults to a normal
  1074. parser (i.e., <code class="docutils literal notranslate"><span class="pre">DocTestParser()</span></code>).</p>
  1075. <p>Optional argument <em>encoding</em> specifies an encoding that should be used to
  1076. convert the file to unicode.</p>
  1077. <p>The global <code class="docutils literal notranslate"><span class="pre">__file__</span></code> is added to the globals provided to doctests loaded
  1078. from a text file using <a class="reference internal" href="#doctest.DocFileSuite" title="doctest.DocFileSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocFileSuite()</span></code></a>.</p>
  1079. </dd></dl>
  1080. <dl class="py function">
  1081. <dt class="sig sig-object py" id="doctest.DocTestSuite">
  1082. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DocTestSuite</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">extraglobs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">test_finder</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">setUp</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tearDown</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optionflags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">checker</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestSuite" title="Link to this definition">¶</a></dt>
  1083. <dd><p>Convert doctest tests for a module to a <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a>.</p>
  1084. <p>The returned <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a> is to be run by the unittest framework
  1085. and runs each doctest in the module. If any of the doctests fail, then the
  1086. synthesized unit test fails, and a <a class="reference internal" href="#doctest.failureException" title="doctest.failureException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">failureException</span></code></a> exception is raised
  1087. showing the name of the file containing the test and a (sometimes approximate)
  1088. line number.</p>
  1089. <p>Optional argument <em>module</em> provides the module to be tested. It can be a module
  1090. object or a (possibly dotted) module name. If not specified, the module calling
  1091. this function is used.</p>
  1092. <p>Optional argument <em>globs</em> is a dictionary containing the initial global
  1093. variables for the tests. A new copy of this dictionary is created for each
  1094. test. By default, <em>globs</em> is a new empty dictionary.</p>
  1095. <p>Optional argument <em>extraglobs</em> specifies an extra set of global variables, which
  1096. is merged into <em>globs</em>. By default, no extra globals are used.</p>
  1097. <p>Optional argument <em>test_finder</em> is the <a class="reference internal" href="#doctest.DocTestFinder" title="doctest.DocTestFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestFinder</span></code></a> object (or a
  1098. drop-in replacement) that is used to extract doctests from the module.</p>
  1099. <p>Optional arguments <em>setUp</em>, <em>tearDown</em>, and <em>optionflags</em> are the same as for
  1100. function <a class="reference internal" href="#doctest.DocFileSuite" title="doctest.DocFileSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocFileSuite()</span></code></a> above.</p>
  1101. <p>This function uses the same search technique as <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>.</p>
  1102. <div class="versionchanged">
  1103. <p><span class="versionmodified changed">Changed in version 3.5: </span><a class="reference internal" href="#doctest.DocTestSuite" title="doctest.DocTestSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocTestSuite()</span></code></a> returns an empty <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a> if <em>module</em>
  1104. contains no docstrings instead of raising <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
  1105. </div>
  1106. </dd></dl>
  1107. <dl class="py exception">
  1108. <dt class="sig sig-object py" id="doctest.failureException">
  1109. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">failureException</span></span><a class="headerlink" href="#doctest.failureException" title="Link to this definition">¶</a></dt>
  1110. <dd><p>When doctests which have been converted to unit tests by <a class="reference internal" href="#doctest.DocFileSuite" title="doctest.DocFileSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocFileSuite()</span></code></a>
  1111. or <a class="reference internal" href="#doctest.DocTestSuite" title="doctest.DocTestSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocTestSuite()</span></code></a> fail, this exception is raised showing the name of
  1112. the file containing the test and a (sometimes approximate) line number.</p>
  1113. </dd></dl>
  1114. <p>Under the covers, <a class="reference internal" href="#doctest.DocTestSuite" title="doctest.DocTestSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocTestSuite()</span></code></a> creates a <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a> out
  1115. of <code class="xref py py-class docutils literal notranslate"><span class="pre">doctest.DocTestCase</span></code> instances, and <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestCase</span></code> is a
  1116. subclass of <a class="reference internal" href="unittest.html#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestCase</span></code></a>. <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestCase</span></code> isn’t documented
  1117. here (it’s an internal detail), but studying its code can answer questions about
  1118. the exact details of <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> integration.</p>
  1119. <p>Similarly, <a class="reference internal" href="#doctest.DocFileSuite" title="doctest.DocFileSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocFileSuite()</span></code></a> creates a <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a> out of
  1120. <code class="xref py py-class docutils literal notranslate"><span class="pre">doctest.DocFileCase</span></code> instances, and <code class="xref py py-class docutils literal notranslate"><span class="pre">DocFileCase</span></code> is a subclass
  1121. of <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestCase</span></code>.</p>
  1122. <p>So both ways of creating a <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a> run instances of
  1123. <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestCase</span></code>. This is important for a subtle reason: when you run
  1124. <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> functions yourself, you can control the <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> options in
  1125. use directly, by passing option flags to <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> functions. However, if
  1126. you’re writing a <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> framework, <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> ultimately controls
  1127. when and how tests get run. The framework author typically wants to control
  1128. <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> reporting options (perhaps, e.g., specified by command line
  1129. options), but there’s no way to pass options through <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> to
  1130. <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> test runners.</p>
  1131. <p>For this reason, <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> also supports a notion of <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a>
  1132. reporting flags specific to <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> support, via this function:</p>
  1133. <dl class="py function">
  1134. <dt class="sig sig-object py" id="doctest.set_unittest_reportflags">
  1135. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">set_unittest_reportflags</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">flags</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.set_unittest_reportflags" title="Link to this definition">¶</a></dt>
  1136. <dd><p>Set the <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> reporting flags to use.</p>
  1137. <p>Argument <em>flags</em> takes the <a class="reference internal" href="../reference/expressions.html#bitwise"><span class="std std-ref">bitwise OR</span></a> of option flags. See
  1138. section <a class="reference internal" href="#doctest-options"><span class="std std-ref">Option Flags</span></a>. Only “reporting flags” can be used.</p>
  1139. <p>This is a module-global setting, and affects all future doctests run by module
  1140. <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a>: the <code class="xref py py-meth docutils literal notranslate"><span class="pre">runTest()</span></code> method of <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestCase</span></code> looks at
  1141. the option flags specified for the test case when the <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestCase</span></code>
  1142. instance was constructed. If no reporting flags were specified (which is the
  1143. typical and expected case), <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code>’s <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> reporting flags are
  1144. <a class="reference internal" href="../reference/expressions.html#bitwise"><span class="std std-ref">bitwise ORed</span></a> into the option flags, and the option flags
  1145. so augmented are passed to the <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> instance created to
  1146. run the doctest. If any reporting flags were specified when the
  1147. <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestCase</span></code> instance was constructed, <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code>’s
  1148. <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> reporting flags are ignored.</p>
  1149. <p>The value of the <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> reporting flags in effect before the function
  1150. was called is returned by the function.</p>
  1151. </dd></dl>
  1152. </section>
  1153. <section id="advanced-api">
  1154. <span id="doctest-advanced-api"></span><h2>Advanced API<a class="headerlink" href="#advanced-api" title="Link to this heading">¶</a></h2>
  1155. <p>The basic API is a simple wrapper that’s intended to make doctest easy to use.
  1156. It is fairly flexible, and should meet most users’ needs; however, if you
  1157. require more fine-grained control over testing, or wish to extend doctest’s
  1158. capabilities, then you should use the advanced API.</p>
  1159. <p>The advanced API revolves around two container classes, which are used to store
  1160. the interactive examples extracted from doctest cases:</p>
  1161. <ul class="simple">
  1162. <li><p><a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a>: A single Python <a class="reference internal" href="../glossary.html#term-statement"><span class="xref std std-term">statement</span></a>, paired with its expected
  1163. output.</p></li>
  1164. <li><p><a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>: A collection of <a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a>s, typically extracted
  1165. from a single docstring or text file.</p></li>
  1166. </ul>
  1167. <p>Additional processing classes are defined to find, parse, and run, and check
  1168. doctest examples:</p>
  1169. <ul class="simple">
  1170. <li><p><a class="reference internal" href="#doctest.DocTestFinder" title="doctest.DocTestFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestFinder</span></code></a>: Finds all docstrings in a given module, and uses a
  1171. <a class="reference internal" href="#doctest.DocTestParser" title="doctest.DocTestParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestParser</span></code></a> to create a <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> from every docstring that
  1172. contains interactive examples.</p></li>
  1173. <li><p><a class="reference internal" href="#doctest.DocTestParser" title="doctest.DocTestParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestParser</span></code></a>: Creates a <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object from a string (such
  1174. as an object’s docstring).</p></li>
  1175. <li><p><a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a>: Executes the examples in a <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>, and uses
  1176. an <a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a> to verify their output.</p></li>
  1177. <li><p><a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a>: Compares the actual output from a doctest example with
  1178. the expected output, and decides whether they match.</p></li>
  1179. </ul>
  1180. <p>The relationships among these processing classes are summarized in the following
  1181. diagram:</p>
  1182. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span> <span class="nb">list</span> <span class="n">of</span><span class="p">:</span>
  1183. <span class="o">+------+</span> <span class="o">+---------+</span>
  1184. <span class="o">|</span><span class="n">module</span><span class="o">|</span> <span class="o">--</span><span class="n">DocTestFinder</span><span class="o">-&gt;</span> <span class="o">|</span> <span class="n">DocTest</span> <span class="o">|</span> <span class="o">--</span><span class="n">DocTestRunner</span><span class="o">-&gt;</span> <span class="n">results</span>
  1185. <span class="o">+------+</span> <span class="o">|</span> <span class="o">^</span> <span class="o">+---------+</span> <span class="o">|</span> <span class="o">^</span> <span class="p">(</span><span class="n">printed</span><span class="p">)</span>
  1186. <span class="o">|</span> <span class="o">|</span> <span class="o">|</span> <span class="n">Example</span> <span class="o">|</span> <span class="o">|</span> <span class="o">|</span>
  1187. <span class="n">v</span> <span class="o">|</span> <span class="o">|</span> <span class="o">...</span> <span class="o">|</span> <span class="n">v</span> <span class="o">|</span>
  1188. <span class="n">DocTestParser</span> <span class="o">|</span> <span class="n">Example</span> <span class="o">|</span> <span class="n">OutputChecker</span>
  1189. <span class="o">+---------+</span>
  1190. </pre></div>
  1191. </div>
  1192. <section id="doctest-objects">
  1193. <span id="doctest-doctest"></span><h3>DocTest Objects<a class="headerlink" href="#doctest-objects" title="Link to this heading">¶</a></h3>
  1194. <dl class="py class">
  1195. <dt class="sig sig-object py" id="doctest.DocTest">
  1196. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DocTest</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">examples</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">lineno</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">docstring</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTest" title="Link to this definition">¶</a></dt>
  1197. <dd><p>A collection of doctest examples that should be run in a single namespace. The
  1198. constructor arguments are used to initialize the attributes of the same names.</p>
  1199. <p><a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> defines the following attributes. They are initialized by
  1200. the constructor, and should not be modified directly.</p>
  1201. <dl class="py attribute">
  1202. <dt class="sig sig-object py" id="doctest.DocTest.examples">
  1203. <span class="sig-name descname"><span class="pre">examples</span></span><a class="headerlink" href="#doctest.DocTest.examples" title="Link to this definition">¶</a></dt>
  1204. <dd><p>A list of <a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a> objects encoding the individual interactive Python
  1205. examples that should be run by this test.</p>
  1206. </dd></dl>
  1207. <dl class="py attribute">
  1208. <dt class="sig sig-object py" id="doctest.DocTest.globs">
  1209. <span class="sig-name descname"><span class="pre">globs</span></span><a class="headerlink" href="#doctest.DocTest.globs" title="Link to this definition">¶</a></dt>
  1210. <dd><p>The namespace (aka globals) that the examples should be run in. This is a
  1211. dictionary mapping names to values. Any changes to the namespace made by the
  1212. examples (such as binding new variables) will be reflected in <a class="reference internal" href="#doctest.DocTest.globs" title="doctest.DocTest.globs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">globs</span></code></a>
  1213. after the test is run.</p>
  1214. </dd></dl>
  1215. <dl class="py attribute">
  1216. <dt class="sig sig-object py" id="doctest.DocTest.name">
  1217. <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#doctest.DocTest.name" title="Link to this definition">¶</a></dt>
  1218. <dd><p>A string name identifying the <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>. Typically, this is the name
  1219. of the object or file that the test was extracted from.</p>
  1220. </dd></dl>
  1221. <dl class="py attribute">
  1222. <dt class="sig sig-object py" id="doctest.DocTest.filename">
  1223. <span class="sig-name descname"><span class="pre">filename</span></span><a class="headerlink" href="#doctest.DocTest.filename" title="Link to this definition">¶</a></dt>
  1224. <dd><p>The name of the file that this <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> was extracted from; or
  1225. <code class="docutils literal notranslate"><span class="pre">None</span></code> if the filename is unknown, or if the <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> was not
  1226. extracted from a file.</p>
  1227. </dd></dl>
  1228. <dl class="py attribute">
  1229. <dt class="sig sig-object py" id="doctest.DocTest.lineno">
  1230. <span class="sig-name descname"><span class="pre">lineno</span></span><a class="headerlink" href="#doctest.DocTest.lineno" title="Link to this definition">¶</a></dt>
  1231. <dd><p>The line number within <a class="reference internal" href="#doctest.DocTest.filename" title="doctest.DocTest.filename"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filename</span></code></a> where this <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> begins, or
  1232. <code class="docutils literal notranslate"><span class="pre">None</span></code> if the line number is unavailable. This line number is zero-based
  1233. with respect to the beginning of the file.</p>
  1234. </dd></dl>
  1235. <dl class="py attribute">
  1236. <dt class="sig sig-object py" id="doctest.DocTest.docstring">
  1237. <span class="sig-name descname"><span class="pre">docstring</span></span><a class="headerlink" href="#doctest.DocTest.docstring" title="Link to this definition">¶</a></dt>
  1238. <dd><p>The string that the test was extracted from, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if the string is
  1239. unavailable, or if the test was not extracted from a string.</p>
  1240. </dd></dl>
  1241. </dd></dl>
  1242. </section>
  1243. <section id="example-objects">
  1244. <span id="doctest-example"></span><h3>Example Objects<a class="headerlink" href="#example-objects" title="Link to this heading">¶</a></h3>
  1245. <dl class="py class">
  1246. <dt class="sig sig-object py" id="doctest.Example">
  1247. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">Example</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">want</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exc_msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">lineno</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">indent</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">options</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.Example" title="Link to this definition">¶</a></dt>
  1248. <dd><p>A single interactive example, consisting of a Python statement and its expected
  1249. output. The constructor arguments are used to initialize the attributes of
  1250. the same names.</p>
  1251. <p><a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a> defines the following attributes. They are initialized by
  1252. the constructor, and should not be modified directly.</p>
  1253. <dl class="py attribute">
  1254. <dt class="sig sig-object py" id="doctest.Example.source">
  1255. <span class="sig-name descname"><span class="pre">source</span></span><a class="headerlink" href="#doctest.Example.source" title="Link to this definition">¶</a></dt>
  1256. <dd><p>A string containing the example’s source code. This source code consists of a
  1257. single Python statement, and always ends with a newline; the constructor adds
  1258. a newline when necessary.</p>
  1259. </dd></dl>
  1260. <dl class="py attribute">
  1261. <dt class="sig sig-object py" id="doctest.Example.want">
  1262. <span class="sig-name descname"><span class="pre">want</span></span><a class="headerlink" href="#doctest.Example.want" title="Link to this definition">¶</a></dt>
  1263. <dd><p>The expected output from running the example’s source code (either from
  1264. stdout, or a traceback in case of exception). <a class="reference internal" href="#doctest.Example.want" title="doctest.Example.want"><code class="xref py py-attr docutils literal notranslate"><span class="pre">want</span></code></a> ends with a
  1265. newline unless no output is expected, in which case it’s an empty string. The
  1266. constructor adds a newline when necessary.</p>
  1267. </dd></dl>
  1268. <dl class="py attribute">
  1269. <dt class="sig sig-object py" id="doctest.Example.exc_msg">
  1270. <span class="sig-name descname"><span class="pre">exc_msg</span></span><a class="headerlink" href="#doctest.Example.exc_msg" title="Link to this definition">¶</a></dt>
  1271. <dd><p>The exception message generated by the example, if the example is expected to
  1272. generate an exception; or <code class="docutils literal notranslate"><span class="pre">None</span></code> if it is not expected to generate an
  1273. exception. This exception message is compared against the return value of
  1274. <a class="reference internal" href="traceback.html#traceback.format_exception_only" title="traceback.format_exception_only"><code class="xref py py-func docutils literal notranslate"><span class="pre">traceback.format_exception_only()</span></code></a>. <a class="reference internal" href="#doctest.Example.exc_msg" title="doctest.Example.exc_msg"><code class="xref py py-attr docutils literal notranslate"><span class="pre">exc_msg</span></code></a> ends with a newline
  1275. unless it’s <code class="docutils literal notranslate"><span class="pre">None</span></code>. The constructor adds a newline if needed.</p>
  1276. </dd></dl>
  1277. <dl class="py attribute">
  1278. <dt class="sig sig-object py" id="doctest.Example.lineno">
  1279. <span class="sig-name descname"><span class="pre">lineno</span></span><a class="headerlink" href="#doctest.Example.lineno" title="Link to this definition">¶</a></dt>
  1280. <dd><p>The line number within the string containing this example where the example
  1281. begins. This line number is zero-based with respect to the beginning of the
  1282. containing string.</p>
  1283. </dd></dl>
  1284. <dl class="py attribute">
  1285. <dt class="sig sig-object py" id="doctest.Example.indent">
  1286. <span class="sig-name descname"><span class="pre">indent</span></span><a class="headerlink" href="#doctest.Example.indent" title="Link to this definition">¶</a></dt>
  1287. <dd><p>The example’s indentation in the containing string, i.e., the number of space
  1288. characters that precede the example’s first prompt.</p>
  1289. </dd></dl>
  1290. <dl class="py attribute">
  1291. <dt class="sig sig-object py" id="doctest.Example.options">
  1292. <span class="sig-name descname"><span class="pre">options</span></span><a class="headerlink" href="#doctest.Example.options" title="Link to this definition">¶</a></dt>
  1293. <dd><p>A dictionary mapping from option flags to <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code>, which is used
  1294. to override default options for this example. Any option flags not contained
  1295. in this dictionary are left at their default value (as specified by the
  1296. <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a>’s <a class="reference internal" href="#doctest-options"><span class="std std-ref">optionflags</span></a>).
  1297. By default, no options are set.</p>
  1298. </dd></dl>
  1299. </dd></dl>
  1300. </section>
  1301. <section id="doctestfinder-objects">
  1302. <span id="doctest-doctestfinder"></span><h3>DocTestFinder objects<a class="headerlink" href="#doctestfinder-objects" title="Link to this heading">¶</a></h3>
  1303. <dl class="py class">
  1304. <dt class="sig sig-object py" id="doctest.DocTestFinder">
  1305. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DocTestFinder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">DocTestParser()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">recurse</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exclude_empty</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestFinder" title="Link to this definition">¶</a></dt>
  1306. <dd><p>A processing class used to extract the <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>s that are relevant to
  1307. a given object, from its docstring and the docstrings of its contained objects.
  1308. <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>s can be extracted from modules, classes, functions,
  1309. methods, staticmethods, classmethods, and properties.</p>
  1310. <p>The optional argument <em>verbose</em> can be used to display the objects searched by
  1311. the finder. It defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code> (no output).</p>
  1312. <p>The optional argument <em>parser</em> specifies the <a class="reference internal" href="#doctest.DocTestParser" title="doctest.DocTestParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestParser</span></code></a> object (or a
  1313. drop-in replacement) that is used to extract doctests from docstrings.</p>
  1314. <p>If the optional argument <em>recurse</em> is false, then <a class="reference internal" href="#doctest.DocTestFinder.find" title="doctest.DocTestFinder.find"><code class="xref py py-meth docutils literal notranslate"><span class="pre">DocTestFinder.find()</span></code></a>
  1315. will only examine the given object, and not any contained objects.</p>
  1316. <p>If the optional argument <em>exclude_empty</em> is false, then
  1317. <a class="reference internal" href="#doctest.DocTestFinder.find" title="doctest.DocTestFinder.find"><code class="xref py py-meth docutils literal notranslate"><span class="pre">DocTestFinder.find()</span></code></a> will include tests for objects with empty docstrings.</p>
  1318. <p><a class="reference internal" href="#doctest.DocTestFinder" title="doctest.DocTestFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestFinder</span></code></a> defines the following method:</p>
  1319. <dl class="py method">
  1320. <dt class="sig sig-object py" id="doctest.DocTestFinder.find">
  1321. <span class="sig-name descname"><span class="pre">find</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">obj[,</span> <span class="pre">name][,</span> <span class="pre">module][,</span> <span class="pre">globs][,</span> <span class="pre">extraglobs]</span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestFinder.find" title="Link to this definition">¶</a></dt>
  1322. <dd><p>Return a list of the <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>s that are defined by <em>obj</em>’s
  1323. docstring, or by any of its contained objects’ docstrings.</p>
  1324. <p>The optional argument <em>name</em> specifies the object’s name; this name will be
  1325. used to construct names for the returned <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>s. If <em>name</em> is
  1326. not specified, then <code class="docutils literal notranslate"><span class="pre">obj.__name__</span></code> is used.</p>
  1327. <p>The optional parameter <em>module</em> is the module that contains the given object.
  1328. If the module is not specified or is <code class="docutils literal notranslate"><span class="pre">None</span></code>, then the test finder will attempt
  1329. to automatically determine the correct module. The object’s module is used:</p>
  1330. <ul class="simple">
  1331. <li><p>As a default namespace, if <em>globs</em> is not specified.</p></li>
  1332. <li><p>To prevent the DocTestFinder from extracting DocTests from objects that are
  1333. imported from other modules. (Contained objects with modules other than
  1334. <em>module</em> are ignored.)</p></li>
  1335. <li><p>To find the name of the file containing the object.</p></li>
  1336. <li><p>To help find the line number of the object within its file.</p></li>
  1337. </ul>
  1338. <p>If <em>module</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, no attempt to find the module will be made. This is
  1339. obscure, of use mostly in testing doctest itself: if <em>module</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, or
  1340. is <code class="docutils literal notranslate"><span class="pre">None</span></code> but cannot be found automatically, then all objects are considered
  1341. to belong to the (non-existent) module, so all contained objects will
  1342. (recursively) be searched for doctests.</p>
  1343. <p>The globals for each <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> is formed by combining <em>globs</em> and
  1344. <em>extraglobs</em> (bindings in <em>extraglobs</em> override bindings in <em>globs</em>). A new
  1345. shallow copy of the globals dictionary is created for each <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>.
  1346. If <em>globs</em> is not specified, then it defaults to the module’s <em>__dict__</em>, if
  1347. specified, or <code class="docutils literal notranslate"><span class="pre">{}</span></code> otherwise. If <em>extraglobs</em> is not specified, then it
  1348. defaults to <code class="docutils literal notranslate"><span class="pre">{}</span></code>.</p>
  1349. </dd></dl>
  1350. </dd></dl>
  1351. </section>
  1352. <section id="doctestparser-objects">
  1353. <span id="doctest-doctestparser"></span><h3>DocTestParser objects<a class="headerlink" href="#doctestparser-objects" title="Link to this heading">¶</a></h3>
  1354. <dl class="py class">
  1355. <dt class="sig sig-object py" id="doctest.DocTestParser">
  1356. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DocTestParser</span></span><a class="headerlink" href="#doctest.DocTestParser" title="Link to this definition">¶</a></dt>
  1357. <dd><p>A processing class used to extract interactive examples from a string, and use
  1358. them to create a <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object.</p>
  1359. <p><a class="reference internal" href="#doctest.DocTestParser" title="doctest.DocTestParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestParser</span></code></a> defines the following methods:</p>
  1360. <dl class="py method">
  1361. <dt class="sig sig-object py" id="doctest.DocTestParser.get_doctest">
  1362. <span class="sig-name descname"><span class="pre">get_doctest</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">lineno</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestParser.get_doctest" title="Link to this definition">¶</a></dt>
  1363. <dd><p>Extract all doctest examples from the given string, and collect them into a
  1364. <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object.</p>
  1365. <p><em>globs</em>, <em>name</em>, <em>filename</em>, and <em>lineno</em> are attributes for the new
  1366. <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object. See the documentation for <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> for more
  1367. information.</p>
  1368. </dd></dl>
  1369. <dl class="py method">
  1370. <dt class="sig sig-object py" id="doctest.DocTestParser.get_examples">
  1371. <span class="sig-name descname"><span class="pre">get_examples</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'&lt;string&gt;'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestParser.get_examples" title="Link to this definition">¶</a></dt>
  1372. <dd><p>Extract all doctest examples from the given string, and return them as a list
  1373. of <a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a> objects. Line numbers are 0-based. The optional argument
  1374. <em>name</em> is a name identifying this string, and is only used for error messages.</p>
  1375. </dd></dl>
  1376. <dl class="py method">
  1377. <dt class="sig sig-object py" id="doctest.DocTestParser.parse">
  1378. <span class="sig-name descname"><span class="pre">parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'&lt;string&gt;'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestParser.parse" title="Link to this definition">¶</a></dt>
  1379. <dd><p>Divide the given string into examples and intervening text, and return them as
  1380. a list of alternating <a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a>s and strings. Line numbers for the
  1381. <a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a>s are 0-based. The optional argument <em>name</em> is a name
  1382. identifying this string, and is only used for error messages.</p>
  1383. </dd></dl>
  1384. </dd></dl>
  1385. </section>
  1386. <section id="doctestrunner-objects">
  1387. <span id="doctest-doctestrunner"></span><h3>DocTestRunner objects<a class="headerlink" href="#doctestrunner-objects" title="Link to this heading">¶</a></h3>
  1388. <dl class="py class">
  1389. <dt class="sig sig-object py" id="doctest.DocTestRunner">
  1390. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DocTestRunner</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">checker</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optionflags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestRunner" title="Link to this definition">¶</a></dt>
  1391. <dd><p>A processing class used to execute and verify the interactive examples in a
  1392. <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>.</p>
  1393. <p>The comparison between expected outputs and actual outputs is done by an
  1394. <a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a>. This comparison may be customized with a number of
  1395. option flags; see section <a class="reference internal" href="#doctest-options"><span class="std std-ref">Option Flags</span></a> for more information. If the
  1396. option flags are insufficient, then the comparison may also be customized by
  1397. passing a subclass of <a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a> to the constructor.</p>
  1398. <p>The test runner’s display output can be controlled in two ways. First, an output
  1399. function can be passed to <a class="reference internal" href="#doctest.DocTestRunner.run" title="doctest.DocTestRunner.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a>; this function will be called
  1400. with strings that should be displayed. It defaults to <code class="docutils literal notranslate"><span class="pre">sys.stdout.write</span></code>. If
  1401. capturing the output is not sufficient, then the display output can be also
  1402. customized by subclassing DocTestRunner, and overriding the methods
  1403. <a class="reference internal" href="#doctest.DocTestRunner.report_start" title="doctest.DocTestRunner.report_start"><code class="xref py py-meth docutils literal notranslate"><span class="pre">report_start()</span></code></a>, <a class="reference internal" href="#doctest.DocTestRunner.report_success" title="doctest.DocTestRunner.report_success"><code class="xref py py-meth docutils literal notranslate"><span class="pre">report_success()</span></code></a>,
  1404. <a class="reference internal" href="#doctest.DocTestRunner.report_unexpected_exception" title="doctest.DocTestRunner.report_unexpected_exception"><code class="xref py py-meth docutils literal notranslate"><span class="pre">report_unexpected_exception()</span></code></a>, and <a class="reference internal" href="#doctest.DocTestRunner.report_failure" title="doctest.DocTestRunner.report_failure"><code class="xref py py-meth docutils literal notranslate"><span class="pre">report_failure()</span></code></a>.</p>
  1405. <p>The optional keyword argument <em>checker</em> specifies the <a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a>
  1406. object (or drop-in replacement) that should be used to compare the expected
  1407. outputs to the actual outputs of doctest examples.</p>
  1408. <p>The optional keyword argument <em>verbose</em> controls the <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a>’s
  1409. verbosity. If <em>verbose</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, then information is printed about each
  1410. example, as it is run. If <em>verbose</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, then only failures are
  1411. printed. If <em>verbose</em> is unspecified, or <code class="docutils literal notranslate"><span class="pre">None</span></code>, then verbose output is used
  1412. iff the command-line switch <code class="docutils literal notranslate"><span class="pre">-v</span></code> is used.</p>
  1413. <p>The optional keyword argument <em>optionflags</em> can be used to control how the test
  1414. runner compares expected output to actual output, and how it displays failures.
  1415. For more information, see section <a class="reference internal" href="#doctest-options"><span class="std std-ref">Option Flags</span></a>.</p>
  1416. <p><a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> defines the following methods:</p>
  1417. <dl class="py method">
  1418. <dt class="sig sig-object py" id="doctest.DocTestRunner.report_start">
  1419. <span class="sig-name descname"><span class="pre">report_start</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">out</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">example</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestRunner.report_start" title="Link to this definition">¶</a></dt>
  1420. <dd><p>Report that the test runner is about to process the given example. This method
  1421. is provided to allow subclasses of <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> to customize their
  1422. output; it should not be called directly.</p>
  1423. <p><em>example</em> is the example about to be processed. <em>test</em> is the test
  1424. <em>containing example</em>. <em>out</em> is the output function that was passed to
  1425. <a class="reference internal" href="#doctest.DocTestRunner.run" title="doctest.DocTestRunner.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">DocTestRunner.run()</span></code></a>.</p>
  1426. </dd></dl>
  1427. <dl class="py method">
  1428. <dt class="sig sig-object py" id="doctest.DocTestRunner.report_success">
  1429. <span class="sig-name descname"><span class="pre">report_success</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">out</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">example</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">got</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestRunner.report_success" title="Link to this definition">¶</a></dt>
  1430. <dd><p>Report that the given example ran successfully. This method is provided to
  1431. allow subclasses of <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> to customize their output; it
  1432. should not be called directly.</p>
  1433. <p><em>example</em> is the example about to be processed. <em>got</em> is the actual output
  1434. from the example. <em>test</em> is the test containing <em>example</em>. <em>out</em> is the
  1435. output function that was passed to <a class="reference internal" href="#doctest.DocTestRunner.run" title="doctest.DocTestRunner.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">DocTestRunner.run()</span></code></a>.</p>
  1436. </dd></dl>
  1437. <dl class="py method">
  1438. <dt class="sig sig-object py" id="doctest.DocTestRunner.report_failure">
  1439. <span class="sig-name descname"><span class="pre">report_failure</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">out</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">example</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">got</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestRunner.report_failure" title="Link to this definition">¶</a></dt>
  1440. <dd><p>Report that the given example failed. This method is provided to allow
  1441. subclasses of <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> to customize their output; it should not
  1442. be called directly.</p>
  1443. <p><em>example</em> is the example about to be processed. <em>got</em> is the actual output
  1444. from the example. <em>test</em> is the test containing <em>example</em>. <em>out</em> is the
  1445. output function that was passed to <a class="reference internal" href="#doctest.DocTestRunner.run" title="doctest.DocTestRunner.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">DocTestRunner.run()</span></code></a>.</p>
  1446. </dd></dl>
  1447. <dl class="py method">
  1448. <dt class="sig sig-object py" id="doctest.DocTestRunner.report_unexpected_exception">
  1449. <span class="sig-name descname"><span class="pre">report_unexpected_exception</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">out</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">example</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exc_info</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestRunner.report_unexpected_exception" title="Link to this definition">¶</a></dt>
  1450. <dd><p>Report that the given example raised an unexpected exception. This method is
  1451. provided to allow subclasses of <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> to customize their
  1452. output; it should not be called directly.</p>
  1453. <p><em>example</em> is the example about to be processed. <em>exc_info</em> is a tuple
  1454. containing information about the unexpected exception (as returned by
  1455. <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_info()</span></code></a>). <em>test</em> is the test containing <em>example</em>. <em>out</em> is the
  1456. output function that was passed to <a class="reference internal" href="#doctest.DocTestRunner.run" title="doctest.DocTestRunner.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">DocTestRunner.run()</span></code></a>.</p>
  1457. </dd></dl>
  1458. <dl class="py method">
  1459. <dt class="sig sig-object py" id="doctest.DocTestRunner.run">
  1460. <span class="sig-name descname"><span class="pre">run</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compileflags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">out</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">clear_globs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestRunner.run" title="Link to this definition">¶</a></dt>
  1461. <dd><p>Run the examples in <em>test</em> (a <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object), and display the
  1462. results using the writer function <em>out</em>.</p>
  1463. <p>The examples are run in the namespace <code class="docutils literal notranslate"><span class="pre">test.globs</span></code>. If <em>clear_globs</em> is
  1464. true (the default), then this namespace will be cleared after the test runs,
  1465. to help with garbage collection. If you would like to examine the namespace
  1466. after the test completes, then use <em>clear_globs=False</em>.</p>
  1467. <p><em>compileflags</em> gives the set of flags that should be used by the Python
  1468. compiler when running the examples. If not specified, then it will default to
  1469. the set of future-import flags that apply to <em>globs</em>.</p>
  1470. <p>The output of each example is checked using the <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a>’s
  1471. output checker, and the results are formatted by the
  1472. <code class="xref py py-meth docutils literal notranslate"><span class="pre">DocTestRunner.report_*()</span></code> methods.</p>
  1473. </dd></dl>
  1474. <dl class="py method">
  1475. <dt class="sig sig-object py" id="doctest.DocTestRunner.summarize">
  1476. <span class="sig-name descname"><span class="pre">summarize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestRunner.summarize" title="Link to this definition">¶</a></dt>
  1477. <dd><p>Print a summary of all the test cases that have been run by this DocTestRunner,
  1478. and return a <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> <code class="docutils literal notranslate"><span class="pre">TestResults(failed,</span> <span class="pre">attempted)</span></code>.</p>
  1479. <p>The optional <em>verbose</em> argument controls how detailed the summary is. If the
  1480. verbosity is not specified, then the <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a>’s verbosity is
  1481. used.</p>
  1482. </dd></dl>
  1483. </dd></dl>
  1484. </section>
  1485. <section id="outputchecker-objects">
  1486. <span id="doctest-outputchecker"></span><h3>OutputChecker objects<a class="headerlink" href="#outputchecker-objects" title="Link to this heading">¶</a></h3>
  1487. <dl class="py class">
  1488. <dt class="sig sig-object py" id="doctest.OutputChecker">
  1489. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">OutputChecker</span></span><a class="headerlink" href="#doctest.OutputChecker" title="Link to this definition">¶</a></dt>
  1490. <dd><p>A class used to check the whether the actual output from a doctest example
  1491. matches the expected output. <a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a> defines two methods:
  1492. <a class="reference internal" href="#doctest.OutputChecker.check_output" title="doctest.OutputChecker.check_output"><code class="xref py py-meth docutils literal notranslate"><span class="pre">check_output()</span></code></a>, which compares a given pair of outputs, and returns <code class="docutils literal notranslate"><span class="pre">True</span></code>
  1493. if they match; and <a class="reference internal" href="#doctest.OutputChecker.output_difference" title="doctest.OutputChecker.output_difference"><code class="xref py py-meth docutils literal notranslate"><span class="pre">output_difference()</span></code></a>, which returns a string describing
  1494. the differences between two outputs.</p>
  1495. <p><a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a> defines the following methods:</p>
  1496. <dl class="py method">
  1497. <dt class="sig sig-object py" id="doctest.OutputChecker.check_output">
  1498. <span class="sig-name descname"><span class="pre">check_output</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">want</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">got</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optionflags</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.OutputChecker.check_output" title="Link to this definition">¶</a></dt>
  1499. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> iff the actual output from an example (<em>got</em>) matches the
  1500. expected output (<em>want</em>). These strings are always considered to match if
  1501. they are identical; but depending on what option flags the test runner is
  1502. using, several non-exact match types are also possible. See section
  1503. <a class="reference internal" href="#doctest-options"><span class="std std-ref">Option Flags</span></a> for more information about option flags.</p>
  1504. </dd></dl>
  1505. <dl class="py method">
  1506. <dt class="sig sig-object py" id="doctest.OutputChecker.output_difference">
  1507. <span class="sig-name descname"><span class="pre">output_difference</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">example</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">got</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optionflags</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.OutputChecker.output_difference" title="Link to this definition">¶</a></dt>
  1508. <dd><p>Return a string describing the differences between the expected output for a
  1509. given example (<em>example</em>) and the actual output (<em>got</em>). <em>optionflags</em> is the
  1510. set of option flags used to compare <em>want</em> and <em>got</em>.</p>
  1511. </dd></dl>
  1512. </dd></dl>
  1513. </section>
  1514. </section>
  1515. <section id="debugging">
  1516. <span id="doctest-debugging"></span><h2>Debugging<a class="headerlink" href="#debugging" title="Link to this heading">¶</a></h2>
  1517. <p>Doctest provides several mechanisms for debugging doctest examples:</p>
  1518. <ul>
  1519. <li><p>Several functions convert doctests to executable Python programs, which can be
  1520. run under the Python debugger, <a class="reference internal" href="pdb.html#module-pdb" title="pdb: The Python debugger for interactive interpreters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pdb</span></code></a>.</p></li>
  1521. <li><p>The <a class="reference internal" href="#doctest.DebugRunner" title="doctest.DebugRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DebugRunner</span></code></a> class is a subclass of <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> that
  1522. raises an exception for the first failing example, containing information about
  1523. that example. This information can be used to perform post-mortem debugging on
  1524. the example.</p></li>
  1525. <li><p>The <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> cases generated by <a class="reference internal" href="#doctest.DocTestSuite" title="doctest.DocTestSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocTestSuite()</span></code></a> support the
  1526. <a class="reference internal" href="#doctest.debug" title="doctest.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">debug()</span></code></a> method defined by <a class="reference internal" href="unittest.html#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestCase</span></code></a>.</p></li>
  1527. <li><p>You can add a call to <a class="reference internal" href="pdb.html#pdb.set_trace" title="pdb.set_trace"><code class="xref py py-func docutils literal notranslate"><span class="pre">pdb.set_trace()</span></code></a> in a doctest example, and you’ll
  1528. drop into the Python debugger when that line is executed. Then you can inspect
  1529. current values of variables, and so on. For example, suppose <code class="file docutils literal notranslate"><span class="pre">a.py</span></code>
  1530. contains just this module docstring:</p>
  1531. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="sd">&quot;&quot;&quot;</span>
  1532. <span class="sd">&gt;&gt;&gt; def f(x):</span>
  1533. <span class="sd">... g(x*2)</span>
  1534. <span class="sd">&gt;&gt;&gt; def g(x):</span>
  1535. <span class="sd">... print(x+3)</span>
  1536. <span class="sd">... import pdb; pdb.set_trace()</span>
  1537. <span class="sd">&gt;&gt;&gt; f(3)</span>
  1538. <span class="sd">9</span>
  1539. <span class="sd">&quot;&quot;&quot;</span>
  1540. </pre></div>
  1541. </div>
  1542. <p>Then an interactive Python session may look like this:</p>
  1543. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">a</span><span class="o">,</span> <span class="nn">doctest</span>
  1544. <span class="gp">&gt;&gt;&gt; </span><span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
  1545. <span class="go">--Return--</span>
  1546. <span class="go">&gt; &lt;doctest a[1]&gt;(3)g()-&gt;None</span>
  1547. <span class="go">-&gt; import pdb; pdb.set_trace()</span>
  1548. <span class="go">(Pdb) list</span>
  1549. <span class="go"> 1 def g(x):</span>
  1550. <span class="go"> 2 print(x+3)</span>
  1551. <span class="go"> 3 -&gt; import pdb; pdb.set_trace()</span>
  1552. <span class="go">[EOF]</span>
  1553. <span class="go">(Pdb) p x</span>
  1554. <span class="go">6</span>
  1555. <span class="go">(Pdb) step</span>
  1556. <span class="go">--Return--</span>
  1557. <span class="go">&gt; &lt;doctest a[0]&gt;(2)f()-&gt;None</span>
  1558. <span class="go">-&gt; g(x*2)</span>
  1559. <span class="go">(Pdb) list</span>
  1560. <span class="go"> 1 def f(x):</span>
  1561. <span class="go"> 2 -&gt; g(x*2)</span>
  1562. <span class="go">[EOF]</span>
  1563. <span class="go">(Pdb) p x</span>
  1564. <span class="go">3</span>
  1565. <span class="go">(Pdb) step</span>
  1566. <span class="go">--Return--</span>
  1567. <span class="go">&gt; &lt;doctest a[2]&gt;(1)?()-&gt;None</span>
  1568. <span class="go">-&gt; f(3)</span>
  1569. <span class="go">(Pdb) cont</span>
  1570. <span class="go">(0, 3)</span>
  1571. <span class="gp">&gt;&gt;&gt;</span>
  1572. </pre></div>
  1573. </div>
  1574. </li>
  1575. </ul>
  1576. <p>Functions that convert doctests to Python code, and possibly run the synthesized
  1577. code under the debugger:</p>
  1578. <dl class="py function">
  1579. <dt class="sig sig-object py" id="doctest.script_from_examples">
  1580. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">script_from_examples</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.script_from_examples" title="Link to this definition">¶</a></dt>
  1581. <dd><p>Convert text with examples to a script.</p>
  1582. <p>Argument <em>s</em> is a string containing doctest examples. The string is converted
  1583. to a Python script, where doctest examples in <em>s</em> are converted to regular code,
  1584. and everything else is converted to Python comments. The generated script is
  1585. returned as a string. For example,</p>
  1586. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">doctest</span>
  1587. <span class="nb">print</span><span class="p">(</span><span class="n">doctest</span><span class="o">.</span><span class="n">script_from_examples</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;&quot;&quot;</span>
  1588. <span class="s2"> Set x and y to 1 and 2.</span>
  1589. <span class="s2"> &gt;&gt;&gt; x, y = 1, 2</span>
  1590. <span class="s2"> Print their sum:</span>
  1591. <span class="s2"> &gt;&gt;&gt; print(x+y)</span>
  1592. <span class="s2"> 3</span>
  1593. <span class="s2">&quot;&quot;&quot;</span><span class="p">))</span>
  1594. </pre></div>
  1595. </div>
  1596. <p>displays:</p>
  1597. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Set x and y to 1 and 2.</span>
  1598. <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span>
  1599. <span class="c1">#</span>
  1600. <span class="c1"># Print their sum:</span>
  1601. <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="p">)</span>
  1602. <span class="c1"># Expected:</span>
  1603. <span class="c1">## 3</span>
  1604. </pre></div>
  1605. </div>
  1606. <p>This function is used internally by other functions (see below), but can also be
  1607. useful when you want to transform an interactive Python session into a Python
  1608. script.</p>
  1609. </dd></dl>
  1610. <dl class="py function">
  1611. <dt class="sig sig-object py" id="doctest.testsource">
  1612. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">testsource</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.testsource" title="Link to this definition">¶</a></dt>
  1613. <dd><p>Convert the doctest for an object to a script.</p>
  1614. <p>Argument <em>module</em> is a module object, or dotted name of a module, containing the
  1615. object whose doctests are of interest. Argument <em>name</em> is the name (within the
  1616. module) of the object with the doctests of interest. The result is a string,
  1617. containing the object’s docstring converted to a Python script, as described for
  1618. <a class="reference internal" href="#doctest.script_from_examples" title="doctest.script_from_examples"><code class="xref py py-func docutils literal notranslate"><span class="pre">script_from_examples()</span></code></a> above. For example, if module <code class="file docutils literal notranslate"><span class="pre">a.py</span></code>
  1619. contains a top-level function <code class="xref py py-func docutils literal notranslate"><span class="pre">f()</span></code>, then</p>
  1620. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">a</span><span class="o">,</span> <span class="nn">doctest</span>
  1621. <span class="nb">print</span><span class="p">(</span><span class="n">doctest</span><span class="o">.</span><span class="n">testsource</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s2">&quot;a.f&quot;</span><span class="p">))</span>
  1622. </pre></div>
  1623. </div>
  1624. <p>prints a script version of function <code class="xref py py-func docutils literal notranslate"><span class="pre">f()</span></code>’s docstring, with doctests
  1625. converted to code, and the rest placed in comments.</p>
  1626. </dd></dl>
  1627. <dl class="py function">
  1628. <dt class="sig sig-object py" id="doctest.debug">
  1629. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">debug</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pm</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.debug" title="Link to this definition">¶</a></dt>
  1630. <dd><p>Debug the doctests for an object.</p>
  1631. <p>The <em>module</em> and <em>name</em> arguments are the same as for function
  1632. <a class="reference internal" href="#doctest.testsource" title="doctest.testsource"><code class="xref py py-func docutils literal notranslate"><span class="pre">testsource()</span></code></a> above. The synthesized Python script for the named object’s
  1633. docstring is written to a temporary file, and then that file is run under the
  1634. control of the Python debugger, <a class="reference internal" href="pdb.html#module-pdb" title="pdb: The Python debugger for interactive interpreters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pdb</span></code></a>.</p>
  1635. <p>A shallow copy of <code class="docutils literal notranslate"><span class="pre">module.__dict__</span></code> is used for both local and global
  1636. execution context.</p>
  1637. <p>Optional argument <em>pm</em> controls whether post-mortem debugging is used. If <em>pm</em>
  1638. has a true value, the script file is run directly, and the debugger gets
  1639. involved only if the script terminates via raising an unhandled exception. If
  1640. it does, then post-mortem debugging is invoked, via <a class="reference internal" href="pdb.html#pdb.post_mortem" title="pdb.post_mortem"><code class="xref py py-func docutils literal notranslate"><span class="pre">pdb.post_mortem()</span></code></a>,
  1641. passing the traceback object from the unhandled exception. If <em>pm</em> is not
  1642. specified, or is false, the script is run under the debugger from the start, via
  1643. passing an appropriate <a class="reference internal" href="functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> call to <a class="reference internal" href="pdb.html#pdb.run" title="pdb.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">pdb.run()</span></code></a>.</p>
  1644. </dd></dl>
  1645. <dl class="py function">
  1646. <dt class="sig sig-object py" id="doctest.debug_src">
  1647. <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">debug_src</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">src</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pm</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.debug_src" title="Link to this definition">¶</a></dt>
  1648. <dd><p>Debug the doctests in a string.</p>
  1649. <p>This is like function <a class="reference internal" href="#doctest.debug" title="doctest.debug"><code class="xref py py-func docutils literal notranslate"><span class="pre">debug()</span></code></a> above, except that a string containing
  1650. doctest examples is specified directly, via the <em>src</em> argument.</p>
  1651. <p>Optional argument <em>pm</em> has the same meaning as in function <a class="reference internal" href="#doctest.debug" title="doctest.debug"><code class="xref py py-func docutils literal notranslate"><span class="pre">debug()</span></code></a> above.</p>
  1652. <p>Optional argument <em>globs</em> gives a dictionary to use as both local and global
  1653. execution context. If not specified, or <code class="docutils literal notranslate"><span class="pre">None</span></code>, an empty dictionary is used.
  1654. If specified, a shallow copy of the dictionary is used.</p>
  1655. </dd></dl>
  1656. <p>The <a class="reference internal" href="#doctest.DebugRunner" title="doctest.DebugRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DebugRunner</span></code></a> class, and the special exceptions it may raise, are of
  1657. most interest to testing framework authors, and will only be sketched here. See
  1658. the source code, and especially <a class="reference internal" href="#doctest.DebugRunner" title="doctest.DebugRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DebugRunner</span></code></a>’s docstring (which is a
  1659. doctest!) for more details:</p>
  1660. <dl class="py class">
  1661. <dt class="sig sig-object py" id="doctest.DebugRunner">
  1662. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DebugRunner</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">checker</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optionflags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DebugRunner" title="Link to this definition">¶</a></dt>
  1663. <dd><p>A subclass of <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> that raises an exception as soon as a
  1664. failure is encountered. If an unexpected exception occurs, an
  1665. <a class="reference internal" href="#doctest.UnexpectedException" title="doctest.UnexpectedException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnexpectedException</span></code></a> exception is raised, containing the test, the
  1666. example, and the original exception. If the output doesn’t match, then a
  1667. <a class="reference internal" href="#doctest.DocTestFailure" title="doctest.DocTestFailure"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DocTestFailure</span></code></a> exception is raised, containing the test, the example, and
  1668. the actual output.</p>
  1669. <p>For information about the constructor parameters and methods, see the
  1670. documentation for <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> in section <a class="reference internal" href="#doctest-advanced-api"><span class="std std-ref">Advanced API</span></a>.</p>
  1671. </dd></dl>
  1672. <p>There are two exceptions that may be raised by <a class="reference internal" href="#doctest.DebugRunner" title="doctest.DebugRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DebugRunner</span></code></a> instances:</p>
  1673. <dl class="py exception">
  1674. <dt class="sig sig-object py" id="doctest.DocTestFailure">
  1675. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DocTestFailure</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">example</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">got</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestFailure" title="Link to this definition">¶</a></dt>
  1676. <dd><p>An exception raised by <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> to signal that a doctest example’s
  1677. actual output did not match its expected output. The constructor arguments are
  1678. used to initialize the attributes of the same names.</p>
  1679. </dd></dl>
  1680. <p><a class="reference internal" href="#doctest.DocTestFailure" title="doctest.DocTestFailure"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DocTestFailure</span></code></a> defines the following attributes:</p>
  1681. <dl class="py attribute">
  1682. <dt class="sig sig-object py" id="doctest.DocTestFailure.test">
  1683. <span class="sig-prename descclassname"><span class="pre">DocTestFailure.</span></span><span class="sig-name descname"><span class="pre">test</span></span><a class="headerlink" href="#doctest.DocTestFailure.test" title="Link to this definition">¶</a></dt>
  1684. <dd><p>The <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object that was being run when the example failed.</p>
  1685. </dd></dl>
  1686. <dl class="py attribute">
  1687. <dt class="sig sig-object py" id="doctest.DocTestFailure.example">
  1688. <span class="sig-prename descclassname"><span class="pre">DocTestFailure.</span></span><span class="sig-name descname"><span class="pre">example</span></span><a class="headerlink" href="#doctest.DocTestFailure.example" title="Link to this definition">¶</a></dt>
  1689. <dd><p>The <a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a> that failed.</p>
  1690. </dd></dl>
  1691. <dl class="py attribute">
  1692. <dt class="sig sig-object py" id="doctest.DocTestFailure.got">
  1693. <span class="sig-prename descclassname"><span class="pre">DocTestFailure.</span></span><span class="sig-name descname"><span class="pre">got</span></span><a class="headerlink" href="#doctest.DocTestFailure.got" title="Link to this definition">¶</a></dt>
  1694. <dd><p>The example’s actual output.</p>
  1695. </dd></dl>
  1696. <dl class="py exception">
  1697. <dt class="sig sig-object py" id="doctest.UnexpectedException">
  1698. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">UnexpectedException</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">example</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exc_info</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.UnexpectedException" title="Link to this definition">¶</a></dt>
  1699. <dd><p>An exception raised by <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> to signal that a doctest
  1700. example raised an unexpected exception. The constructor arguments are used
  1701. to initialize the attributes of the same names.</p>
  1702. </dd></dl>
  1703. <p><a class="reference internal" href="#doctest.UnexpectedException" title="doctest.UnexpectedException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnexpectedException</span></code></a> defines the following attributes:</p>
  1704. <dl class="py attribute">
  1705. <dt class="sig sig-object py" id="doctest.UnexpectedException.test">
  1706. <span class="sig-prename descclassname"><span class="pre">UnexpectedException.</span></span><span class="sig-name descname"><span class="pre">test</span></span><a class="headerlink" href="#doctest.UnexpectedException.test" title="Link to this definition">¶</a></dt>
  1707. <dd><p>The <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object that was being run when the example failed.</p>
  1708. </dd></dl>
  1709. <dl class="py attribute">
  1710. <dt class="sig sig-object py" id="doctest.UnexpectedException.example">
  1711. <span class="sig-prename descclassname"><span class="pre">UnexpectedException.</span></span><span class="sig-name descname"><span class="pre">example</span></span><a class="headerlink" href="#doctest.UnexpectedException.example" title="Link to this definition">¶</a></dt>
  1712. <dd><p>The <a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a> that failed.</p>
  1713. </dd></dl>
  1714. <dl class="py attribute">
  1715. <dt class="sig sig-object py" id="doctest.UnexpectedException.exc_info">
  1716. <span class="sig-prename descclassname"><span class="pre">UnexpectedException.</span></span><span class="sig-name descname"><span class="pre">exc_info</span></span><a class="headerlink" href="#doctest.UnexpectedException.exc_info" title="Link to this definition">¶</a></dt>
  1717. <dd><p>A tuple containing information about the unexpected exception, as returned by
  1718. <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_info()</span></code></a>.</p>
  1719. </dd></dl>
  1720. </section>
  1721. <section id="soapbox">
  1722. <span id="doctest-soapbox"></span><h2>Soapbox<a class="headerlink" href="#soapbox" title="Link to this heading">¶</a></h2>
  1723. <p>As mentioned in the introduction, <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> has grown to have three primary
  1724. uses:</p>
  1725. <ol class="arabic simple">
  1726. <li><p>Checking examples in docstrings.</p></li>
  1727. <li><p>Regression testing.</p></li>
  1728. <li><p>Executable documentation / literate testing.</p></li>
  1729. </ol>
  1730. <p>These uses have different requirements, and it is important to distinguish them.
  1731. In particular, filling your docstrings with obscure test cases makes for bad
  1732. documentation.</p>
  1733. <p>When writing a docstring, choose docstring examples with care. There’s an art to
  1734. this that needs to be learned—it may not be natural at first. Examples should
  1735. add genuine value to the documentation. A good example can often be worth many
  1736. words. If done with care, the examples will be invaluable for your users, and
  1737. will pay back the time it takes to collect them many times over as the years go
  1738. by and things change. I’m still amazed at how often one of my <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a>
  1739. examples stops working after a “harmless” change.</p>
  1740. <p>Doctest also makes an excellent tool for regression testing, especially if you
  1741. don’t skimp on explanatory text. By interleaving prose and examples, it becomes
  1742. much easier to keep track of what’s actually being tested, and why. When a test
  1743. fails, good prose can make it much easier to figure out what the problem is, and
  1744. how it should be fixed. It’s true that you could write extensive comments in
  1745. code-based testing, but few programmers do. Many have found that using doctest
  1746. approaches instead leads to much clearer tests. Perhaps this is simply because
  1747. doctest makes writing prose a little easier than writing code, while writing
  1748. comments in code is a little harder. I think it goes deeper than just that:
  1749. the natural attitude when writing a doctest-based test is that you want to
  1750. explain the fine points of your software, and illustrate them with examples.
  1751. This in turn naturally leads to test files that start with the simplest
  1752. features, and logically progress to complications and edge cases. A coherent
  1753. narrative is the result, instead of a collection of isolated functions that test
  1754. isolated bits of functionality seemingly at random. It’s a different attitude,
  1755. and produces different results, blurring the distinction between testing and
  1756. explaining.</p>
  1757. <p>Regression testing is best confined to dedicated objects or files. There are
  1758. several options for organizing tests:</p>
  1759. <ul class="simple">
  1760. <li><p>Write text files containing test cases as interactive examples, and test the
  1761. files using <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a> or <a class="reference internal" href="#doctest.DocFileSuite" title="doctest.DocFileSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocFileSuite()</span></code></a>. This is recommended,
  1762. although is easiest to do for new projects, designed from the start to use
  1763. doctest.</p></li>
  1764. <li><p>Define functions named <code class="docutils literal notranslate"><span class="pre">_regrtest_topic</span></code> that consist of single docstrings,
  1765. containing test cases for the named topics. These functions can be included in
  1766. the same file as the module, or separated out into a separate test file.</p></li>
  1767. <li><p>Define a <code class="docutils literal notranslate"><span class="pre">__test__</span></code> dictionary mapping from regression test topics to
  1768. docstrings containing test cases.</p></li>
  1769. </ul>
  1770. <p>When you have placed your tests in a module, the module can itself be the test
  1771. runner. When a test fails, you can arrange for your test runner to re-run only
  1772. the failing doctest while you debug the problem. Here is a minimal example of
  1773. such a test runner:</p>
  1774. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
  1775. <span class="kn">import</span> <span class="nn">doctest</span>
  1776. <span class="n">flags</span> <span class="o">=</span> <span class="n">doctest</span><span class="o">.</span><span class="n">REPORT_NDIFF</span><span class="o">|</span><span class="n">doctest</span><span class="o">.</span><span class="n">FAIL_FAST</span>
  1777. <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
  1778. <span class="n">name</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
  1779. <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="nb">globals</span><span class="p">():</span>
  1780. <span class="n">obj</span> <span class="o">=</span> <span class="nb">globals</span><span class="p">()[</span><span class="n">name</span><span class="p">]</span>
  1781. <span class="k">else</span><span class="p">:</span>
  1782. <span class="n">obj</span> <span class="o">=</span> <span class="n">__test__</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
  1783. <span class="n">doctest</span><span class="o">.</span><span class="n">run_docstring_examples</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="nb">globals</span><span class="p">(),</span> <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">,</span>
  1784. <span class="n">optionflags</span><span class="o">=</span><span class="n">flags</span><span class="p">)</span>
  1785. <span class="k">else</span><span class="p">:</span>
  1786. <span class="n">fail</span><span class="p">,</span> <span class="n">total</span> <span class="o">=</span> <span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">(</span><span class="n">optionflags</span><span class="o">=</span><span class="n">flags</span><span class="p">)</span>
  1787. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> failures out of </span><span class="si">{}</span><span class="s2"> tests&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">fail</span><span class="p">,</span> <span class="n">total</span><span class="p">))</span>
  1788. </pre></div>
  1789. </div>
  1790. <p class="rubric">Footnotes</p>
  1791. <aside class="footnote-list brackets">
  1792. <aside class="footnote brackets" id="id2" role="doc-footnote">
  1793. <span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id1">1</a><span class="fn-bracket">]</span></span>
  1794. <p>Examples containing both expected output and an exception are not supported.
  1795. Trying to guess where one ends and the other begins is too error-prone, and that
  1796. also makes for a confusing test.</p>
  1797. </aside>
  1798. </aside>
  1799. </section>
  1800. </section>
  1801. <div class="clearer"></div>
  1802. </div>
  1803. </div>
  1804. </div>
  1805. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1806. <div class="sphinxsidebarwrapper">
  1807. <div>
  1808. <h3><a href="../contents.html">Table of Contents</a></h3>
  1809. <ul>
  1810. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> — Test interactive Python examples</a><ul>
  1811. <li><a class="reference internal" href="#simple-usage-checking-examples-in-docstrings">Simple Usage: Checking Examples in Docstrings</a></li>
  1812. <li><a class="reference internal" href="#simple-usage-checking-examples-in-a-text-file">Simple Usage: Checking Examples in a Text File</a></li>
  1813. <li><a class="reference internal" href="#how-it-works">How It Works</a><ul>
  1814. <li><a class="reference internal" href="#which-docstrings-are-examined">Which Docstrings Are Examined?</a></li>
  1815. <li><a class="reference internal" href="#how-are-docstring-examples-recognized">How are Docstring Examples Recognized?</a></li>
  1816. <li><a class="reference internal" href="#what-s-the-execution-context">What’s the Execution Context?</a></li>
  1817. <li><a class="reference internal" href="#what-about-exceptions">What About Exceptions?</a></li>
  1818. <li><a class="reference internal" href="#option-flags">Option Flags</a></li>
  1819. <li><a class="reference internal" href="#directives">Directives</a></li>
  1820. <li><a class="reference internal" href="#warnings">Warnings</a></li>
  1821. </ul>
  1822. </li>
  1823. <li><a class="reference internal" href="#basic-api">Basic API</a></li>
  1824. <li><a class="reference internal" href="#unittest-api">Unittest API</a></li>
  1825. <li><a class="reference internal" href="#advanced-api">Advanced API</a><ul>
  1826. <li><a class="reference internal" href="#doctest-objects">DocTest Objects</a></li>
  1827. <li><a class="reference internal" href="#example-objects">Example Objects</a></li>
  1828. <li><a class="reference internal" href="#doctestfinder-objects">DocTestFinder objects</a></li>
  1829. <li><a class="reference internal" href="#doctestparser-objects">DocTestParser objects</a></li>
  1830. <li><a class="reference internal" href="#doctestrunner-objects">DocTestRunner objects</a></li>
  1831. <li><a class="reference internal" href="#outputchecker-objects">OutputChecker objects</a></li>
  1832. </ul>
  1833. </li>
  1834. <li><a class="reference internal" href="#debugging">Debugging</a></li>
  1835. <li><a class="reference internal" href="#soapbox">Soapbox</a></li>
  1836. </ul>
  1837. </li>
  1838. </ul>
  1839. </div>
  1840. <div>
  1841. <h4>Previous topic</h4>
  1842. <p class="topless"><a href="devmode.html"
  1843. title="previous chapter">Python Development Mode</a></p>
  1844. </div>
  1845. <div>
  1846. <h4>Next topic</h4>
  1847. <p class="topless"><a href="unittest.html"
  1848. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code> — Unit testing framework</a></p>
  1849. </div>
  1850. <div role="note" aria-label="source link">
  1851. <h3>This Page</h3>
  1852. <ul class="this-page-menu">
  1853. <li><a href="../bugs.html">Report a Bug</a></li>
  1854. <li>
  1855. <a href="https://github.com/python/cpython/blob/main/Doc/library/doctest.rst"
  1856. rel="nofollow">Show Source
  1857. </a>
  1858. </li>
  1859. </ul>
  1860. </div>
  1861. </div>
  1862. <div id="sidebarbutton" title="Collapse sidebar">
  1863. <span>«</span>
  1864. </div>
  1865. </div>
  1866. <div class="clearer"></div>
  1867. </div>
  1868. <div class="related" role="navigation" aria-label="related navigation">
  1869. <h3>Navigation</h3>
  1870. <ul>
  1871. <li class="right" style="margin-right: 10px">
  1872. <a href="../genindex.html" title="General Index"
  1873. >index</a></li>
  1874. <li class="right" >
  1875. <a href="../py-modindex.html" title="Python Module Index"
  1876. >modules</a> |</li>
  1877. <li class="right" >
  1878. <a href="unittest.html" title="unittest — Unit testing framework"
  1879. >next</a> |</li>
  1880. <li class="right" >
  1881. <a href="devmode.html" title="Python Development Mode"
  1882. >previous</a> |</li>
  1883. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1884. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1885. <li class="switchers">
  1886. <div class="language_switcher_placeholder"></div>
  1887. <div class="version_switcher_placeholder"></div>
  1888. </li>
  1889. <li>
  1890. </li>
  1891. <li id="cpython-language-and-version">
  1892. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1893. </li>
  1894. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1895. <li class="nav-item nav-item-2"><a href="development.html" >Development Tools</a> &#187;</li>
  1896. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> — Test interactive Python examples</a></li>
  1897. <li class="right">
  1898. <div class="inline-search" role="search">
  1899. <form class="inline-search" action="../search.html" method="get">
  1900. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1901. <input type="submit" value="Go" />
  1902. </form>
  1903. </div>
  1904. |
  1905. </li>
  1906. <li class="right">
  1907. <label class="theme-selector-label">
  1908. Theme
  1909. <select class="theme-selector" oninput="activateTheme(this.value)">
  1910. <option value="auto" selected>Auto</option>
  1911. <option value="light">Light</option>
  1912. <option value="dark">Dark</option>
  1913. </select>
  1914. </label> |</li>
  1915. </ul>
  1916. </div>
  1917. <div class="footer">
  1918. &copy;
  1919. <a href="../copyright.html">
  1920. Copyright
  1921. </a>
  1922. 2001-2024, Python Software Foundation.
  1923. <br />
  1924. This page is licensed under the Python Software Foundation License Version 2.
  1925. <br />
  1926. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1927. <br />
  1928. See <a href="/license.html">History and License</a> for more information.<br />
  1929. <br />
  1930. The Python Software Foundation is a non-profit corporation.
  1931. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1932. <br />
  1933. <br />
  1934. Last updated on Apr 09, 2024 (13:47 UTC).
  1935. <a href="/bugs.html">Found a bug</a>?
  1936. <br />
  1937. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1938. </div>
  1939. </body>
  1940. </html>
上海开阖软件有限公司 沪ICP备12045867号-1