parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="doctest — Test interactive Python examples" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/doctest.html" />10<meta property="og:site_name" content="Python documentation" />11<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..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_doctest_f34b68a9.png" />15<meta property="og:image:alt" 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..." />16<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..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>doctest — Test interactive Python examples — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="unittest — Unit testing framework" href="unittest.html" />43 <link rel="prev" title="Python Development Mode" href="devmode.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/doctest.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <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>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<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>108<li><a class="reference internal" href="#simple-usage-checking-examples-in-docstrings">Simple Usage: Checking Examples in Docstrings</a></li>109<li><a class="reference internal" href="#simple-usage-checking-examples-in-a-text-file">Simple Usage: Checking Examples in a Text File</a></li>110<li><a class="reference internal" href="#command-line-usage">Command-line Usage</a></li>111<li><a class="reference internal" href="#how-it-works">How It Works</a><ul>112<li><a class="reference internal" href="#which-docstrings-are-examined">Which Docstrings Are Examined?</a></li>113<li><a class="reference internal" href="#how-are-docstring-examples-recognized">How are Docstring Examples Recognized?</a></li>114<li><a class="reference internal" href="#what-s-the-execution-context">What’s the Execution Context?</a></li>115<li><a class="reference internal" href="#what-about-exceptions">What About Exceptions?</a></li>116<li><a class="reference internal" href="#option-flags">Option Flags</a></li>117<li><a class="reference internal" href="#directives">Directives</a></li>118<li><a class="reference internal" href="#warnings">Warnings</a></li>119</ul>120</li>121<li><a class="reference internal" href="#basic-api">Basic API</a></li>122<li><a class="reference internal" href="#unittest-api">Unittest API</a></li>123<li><a class="reference internal" href="#advanced-api">Advanced API</a><ul>124<li><a class="reference internal" href="#doctest-objects">DocTest Objects</a></li>125<li><a class="reference internal" href="#example-objects">Example Objects</a></li>126<li><a class="reference internal" href="#doctestfinder-objects">DocTestFinder objects</a></li>127<li><a class="reference internal" href="#doctestparser-objects">DocTestParser objects</a></li>128<li><a class="reference internal" href="#testresults-objects">TestResults objects</a></li>129<li><a class="reference internal" href="#doctestrunner-objects">DocTestRunner objects</a></li>130<li><a class="reference internal" href="#outputchecker-objects">OutputChecker objects</a></li>131</ul>132</li>133<li><a class="reference internal" href="#debugging">Debugging</a></li>134<li><a class="reference internal" href="#soapbox">Soapbox</a></li>135</ul>136</li>137</ul>138 139 </div>140 <div>141 <h4>Previous topic</h4>142 <p class="topless"><a href="devmode.html"143 title="previous chapter">Python Development Mode</a></p>144 </div>145 <div>146 <h4>Next topic</h4>147 <p class="topless"><a href="unittest.html"148 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code> — Unit testing framework</a></p>149 </div>150 <script>151 document.addEventListener('DOMContentLoaded', () => {152 const title = document.querySelector('meta[property="og:title"]').content;153 const elements = document.querySelectorAll('.improvepage');154 const pageurl = window.location.href.split('?')[0];155 elements.forEach(element => {156 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));157 url.searchParams.set('pagetitle', title);158 url.searchParams.set('pageurl', pageurl);159 url.searchParams.set('pagesource', "library/doctest.rst");160 element.href = url.toString();161 });162 });163 </script>164 <div role="note" aria-label="source link">165 <h3>This page</h3>166 <ul class="this-page-menu">167 <li><a href="../bugs.html">Report a bug</a></li>168 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>169 <li>170 <a href="https://github.com/python/cpython/blob/main/Doc/library/doctest.rst?plain=1"171 rel="nofollow">Show source172 </a>173 </li>174 175 </ul>176 </div>177 </nav>178 </div>179</div>180 181 182 <div class="related" role="navigation" aria-label="Related">183 <h3>Navigation</h3>184 <ul>185 <li class="right" style="margin-right: 10px">186 <a href="../genindex.html" title="General Index"187 accesskey="I">index</a></li>188 <li class="right" >189 <a href="../py-modindex.html" title="Python Module Index"190 >modules</a> |</li>191 <li class="right" >192 <a href="unittest.html" title="unittest — Unit testing framework"193 accesskey="N">next</a> |</li>194 <li class="right" >195 <a href="devmode.html" title="Python Development Mode"196 accesskey="P">previous</a> |</li>197 198 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>199 <li><a href="https://www.python.org/">Python</a> »</li>200 <li class="switchers">201 <div class="language_switcher_placeholder"></div>202 <div class="version_switcher_placeholder"></div>203 </li>204 <li>205 206 </li>207 <li id="cpython-language-and-version">208 <a href="../index.html">3.15.0a6 Documentation</a> »209 </li>210 211 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>212 <li class="nav-item nav-item-2"><a href="development.html" accesskey="U">Development Tools</a> »</li>213 <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>214 <li class="right">215 216 217 <div class="inline-search" role="search">218 <form class="inline-search" action="../search.html" method="get">219 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">220 <input type="submit" value="Go">221 </form>222 </div>223 |224 </li>225 <li class="right">226<label class="theme-selector-label">227 Theme228 <select class="theme-selector" oninput="activateTheme(this.value)">229 <option value="auto" selected>Auto</option>230 <option value="light">Light</option>231 <option value="dark">Dark</option>232 </select>233</label> |</li>234 235 </ul>236 </div> 237 238 <div class="document">239 <div class="documentwrapper">240 <div class="bodywrapper">241 <div class="body" role="main">242 243 <section id="module-doctest">244<span id="doctest-test-interactive-python-examples"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> — Test interactive Python examples<a class="headerlink" href="#module-doctest" title="Link to this heading">¶</a></h1>245<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/doctest.py">Lib/doctest.py</a></p>246<hr class="docutils" />247<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> module searches for pieces of text that look like interactive248Python sessions, and then executes those sessions to verify that they work249exactly as shown. There are several common ways to use doctest:</p>250<ul class="simple">251<li><p>To check that a module’s docstrings are up-to-date by verifying that all252interactive examples still work as documented.</p></li>253<li><p>To perform regression testing by verifying that interactive examples from a254test file or a test object work as expected.</p></li>255<li><p>To write tutorial documentation for a package, liberally illustrated with256input-output examples. Depending on whether the examples or the expository text257are emphasized, this has the flavor of “literate testing” or “executable258documentation”.</p></li>259</ul>260<p>Here’s a complete but small example module:</p>261<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>262<span class="sd">This is the "example" module.</span>263 264<span class="sd">The example module supplies one function, factorial(). For example,</span>265 266<span class="sd">>>> factorial(5)</span>267<span class="sd">120</span>268<span class="sd">"""</span>269 270<span class="k">def</span><span class="w"> </span><span class="nf">factorial</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>271<span class="w"> </span><span class="sd">"""Return the factorial of n, an exact integer >= 0.</span>272 273<span class="sd"> >>> [factorial(n) for n in range(6)]</span>274<span class="sd"> [1, 1, 2, 6, 24, 120]</span>275<span class="sd"> >>> factorial(30)</span>276<span class="sd"> 265252859812191058636308480000000</span>277<span class="sd"> >>> factorial(-1)</span>278<span class="sd"> Traceback (most recent call last):</span>279<span class="sd"> ...</span>280<span class="sd"> ValueError: n must be >= 0</span>281 282<span class="sd"> Factorials of floats are OK, but the float must be an exact integer:</span>283<span class="sd"> >>> factorial(30.1)</span>284<span class="sd"> Traceback (most recent call last):</span>285<span class="sd"> ...</span>286<span class="sd"> ValueError: n must be exact integer</span>287<span class="sd"> >>> factorial(30.0)</span>288<span class="sd"> 265252859812191058636308480000000</span>289 290<span class="sd"> It must also not be ridiculously large:</span>291<span class="sd"> >>> factorial(1e100)</span>292<span class="sd"> Traceback (most recent call last):</span>293<span class="sd"> ...</span>294<span class="sd"> OverflowError: n too large</span>295<span class="sd"> """</span>296 297 <span class="kn">import</span><span class="w"> </span><span class="nn">math</span>298 <span class="k">if</span> <span class="ow">not</span> <span class="n">n</span> <span class="o">>=</span> <span class="mi">0</span><span class="p">:</span>299 <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"n must be >= 0"</span><span class="p">)</span>300 <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>301 <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"n must be exact integer"</span><span class="p">)</span>302 <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>303 <span class="k">raise</span> <span class="ne">OverflowError</span><span class="p">(</span><span class="s2">"n too large"</span><span class="p">)</span>304 <span class="n">result</span> <span class="o">=</span> <span class="mi">1</span>305 <span class="n">factor</span> <span class="o">=</span> <span class="mi">2</span>306 <span class="k">while</span> <span class="n">factor</span> <span class="o"><=</span> <span class="n">n</span><span class="p">:</span>307 <span class="n">result</span> <span class="o">*=</span> <span class="n">factor</span>308 <span class="n">factor</span> <span class="o">+=</span> <span class="mi">1</span>309 <span class="k">return</span> <span class="n">result</span>310 311 312<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>313 <span class="kn">import</span><span class="w"> </span><span class="nn">doctest</span>314 <span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">()</span>315</pre></div>316</div>317<p>If you run <code class="file docutils literal notranslate"><span class="pre">example.py</span></code> directly from the command line, <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code>318works its magic:</p>319<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>example.py320<span class="gp">$</span>321</pre></div>322</div>323<p>There’s no output! That’s normal, and it means all the examples worked. Pass324<code class="docutils literal notranslate"><span class="pre">-v</span></code> to the script, and <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> prints a detailed log of what325it’s trying, and prints a summary at the end:</p>326<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>-v327<span class="go">Trying:</span>328<span class="go"> factorial(5)</span>329<span class="go">Expecting:</span>330<span class="go"> 120</span>331<span class="go">ok</span>332<span class="go">Trying:</span>333<span class="go"> [factorial(n) for n in range(6)]</span>334<span class="go">Expecting:</span>335<span class="go"> [1, 1, 2, 6, 24, 120]</span>336<span class="go">ok</span>337</pre></div>338</div>339<p>And so on, eventually ending with:</p>340<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Trying:341 factorial(1e100)342Expecting:343 Traceback (most recent call last):344 ...345 OverflowError: n too large346ok3472 items passed all tests:348 1 test in __main__349 6 tests in __main__.factorial3507 tests in 2 items.3517 passed.352Test passed.353$354</pre></div>355</div>356<p>That’s all you need to know to start making productive use of <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code>!357Jump in. The following sections provide full details. Note that there are many358examples of doctests in the standard Python test suite and libraries.359Especially useful examples can be found in the standard test file360<code class="file docutils literal notranslate"><span class="pre">Lib/test/test_doctest/test_doctest.py</span></code>.</p>361<div class="versionadded">362<p><span class="versionmodified added">Added in version 3.13: </span>Output is colorized by default and can be363<a class="reference internal" href="../using/cmdline.html#using-on-controlling-color"><span class="std std-ref">controlled using environment variables</span></a>.</p>364</div>365<section id="simple-usage-checking-examples-in-docstrings">366<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>367<p>The simplest way to start using doctest (but not necessarily the way you’ll368continue 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>369<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">"__main__"</span><span class="p">:</span>370 <span class="kn">import</span><span class="w"> </span><span class="nn">doctest</span>371 <span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">()</span>372</pre></div>373</div>374<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>375<p>Running the module as a script causes the examples in the docstrings to get376executed and verified:</p>377<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>378</pre></div>379</div>380<p>This won’t display anything unless an example fails, in which case the failing381example(s) and the cause(s) of the failure(s) are printed to stdout, and the382final 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 the383number of examples that failed.</p>384<p>Run it with the <code class="docutils literal notranslate"><span class="pre">-v</span></code> switch instead:</p>385<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>386</pre></div>387</div>388<p>and a detailed report of all examples tried is printed to standard output, along389with assorted summaries at the end.</p>390<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>, or391prohibit it by passing <code class="docutils literal notranslate"><span class="pre">verbose=False</span></code>. In either of those cases,392<a class="reference internal" href="sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> is not examined by <code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code> (so passing <code class="docutils literal notranslate"><span class="pre">-v</span></code> or not393has no effect).</p>394<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>, see section395<a class="reference internal" href="#doctest-cli"><span class="std std-ref">Command-line Usage</span></a>.</p>396<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>397</section>398<section id="simple-usage-checking-examples-in-a-text-file">399<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>400<p>Another simple application of doctest is testing interactive examples in a text401file. 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>402<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">doctest</span>403<span class="n">doctest</span><span class="o">.</span><span class="n">testfile</span><span class="p">(</span><span class="s2">"example.txt"</span><span class="p">)</span>404</pre></div>405</div>406<p>That short script executes and verifies any interactive Python examples407contained in the file <code class="file docutils literal notranslate"><span class="pre">example.txt</span></code>. The file content is treated as if it408were a single giant docstring; the file doesn’t need to contain a Python409program! For example, perhaps <code class="file docutils literal notranslate"><span class="pre">example.txt</span></code> contains this:</p>410<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>The ``example`` module411======================412 413Using ``factorial``414-------------------415 416This is an example text file in reStructuredText format. First import417``factorial`` from the ``example`` module:418 419 >>> from example import factorial420 421Now use it:422 423 >>> factorial(6)424 120425</pre></div>426</div>427<p>Running <code class="docutils literal notranslate"><span class="pre">doctest.testfile("example.txt")</span></code> then finds the error in this428documentation:</p>429<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">File</span> <span class="s2">"./example.txt"</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>430<span class="n">Failed</span> <span class="n">example</span><span class="p">:</span>431 <span class="n">factorial</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>432<span class="n">Expected</span><span class="p">:</span>433 <span class="mi">120</span>434<span class="n">Got</span><span class="p">:</span>435 <span class="mi">720</span>436</pre></div>437</div>438<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 an439example fails. If an example does fail, then the failing example(s) and the440cause(s) of the failure(s) are printed to stdout, using the same format as441<code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code>.</p>442<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.443See section <a class="reference internal" href="#doctest-basic-api"><span class="std std-ref">Basic API</span></a> for a description of the optional arguments444that can be used to tell it to look for files in other locations.</p>445<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 the446<code class="docutils literal notranslate"><span class="pre">-v</span></code> command-line switch or with the optional keyword argument447<em>verbose</em>.</p>448<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>, see section449<a class="reference internal" href="#doctest-cli"><span class="std std-ref">Command-line Usage</span></a>.</p>450<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>451</section>452<section id="command-line-usage">453<span id="doctest-cli"></span><h2>Command-line Usage<a class="headerlink" href="#command-line-usage" title="Link to this heading">¶</a></h2>454<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> module can be invoked as a script from the command line:</p>455<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>python<span class="w"> </span>-m<span class="w"> </span>doctest<span class="w"> </span><span class="o">[</span>-v<span class="o">]</span><span class="w"> </span><span class="o">[</span>-o<span class="w"> </span>OPTION<span class="o">]</span><span class="w"> </span><span class="o">[</span>-f<span class="o">]</span><span class="w"> </span>file<span class="w"> </span><span class="o">[</span>file<span class="w"> </span>...<span class="o">]</span>456</pre></div>457</div>458<dl class="std option">459<dt class="sig sig-object std" id="cmdoption-doctest-v">460<span id="cmdoption-doctest-verbose"></span><span class="sig-name descname"><span class="pre">-v</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--verbose</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-doctest-v" title="Link to this definition">¶</a></dt>461<dd><p>Detailed report of all examples tried is printed to standard output,462along with assorted summaries at the end:</p>463<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>464</pre></div>465</div>466<p>This will import <code class="file docutils literal notranslate"><span class="pre">example.py</span></code> as a standalone module and run467<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 the468file is part of a package and imports other submodules from that package.</p>469<p>If the file name does not end with <code class="file docutils literal notranslate"><span class="pre">.py</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> infers470that 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> instead:</p>471<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>472</pre></div>473</div>474</dd></dl>475 476<dl class="std option">477<dt class="sig sig-object std" id="cmdoption-doctest-o">478<span id="cmdoption-doctest-option"></span><span class="sig-name descname"><span class="pre">-o</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--option</span></span><span class="sig-prename descclassname"> <span class="pre"><option></span></span><a class="headerlink" href="#cmdoption-doctest-o" title="Link to this definition">¶</a></dt>479<dd><p>Option flags control various aspects of doctest’s behavior, see section480<a class="reference internal" href="#doctest-options"><span class="std std-ref">Option Flags</span></a>.</p>481<div class="versionadded">482<p><span class="versionmodified added">Added in version 3.4.</span></p>483</div>484</dd></dl>485 486<dl class="std option">487<dt class="sig sig-object std" id="cmdoption-doctest-f">488<span id="cmdoption-doctest-fail-fast"></span><span class="sig-name descname"><span class="pre">-f</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--fail-fast</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-doctest-f" title="Link to this definition">¶</a></dt>489<dd><p>This is shorthand for <code class="docutils literal notranslate"><span class="pre">-o</span> <span class="pre">FAIL_FAST</span></code>.</p>490<div class="versionadded">491<p><span class="versionmodified added">Added in version 3.4.</span></p>492</div>493</dd></dl>494 495</section>496<section id="how-it-works">497<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>498<p>This section examines in detail how doctest works: which docstrings it looks at,499how it finds interactive examples, what execution context it uses, how it500handles exceptions, and how option flags can be used to control its behavior.501This is the information that you need to know to write doctest examples; for502information about actually running doctest on these examples, see the following503sections.</p>504<section id="which-docstrings-are-examined">505<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>506<p>The module docstring, and all function, class and method docstrings are507searched. Objects imported into the module are not searched.</p>508<p id="module.__test__">In addition, there are cases when you want tests to be part of a module but not part509of the help text, which requires that the tests not be included in the docstring.510Doctest looks for a module-level variable called <code class="docutils literal notranslate"><span class="pre">__test__</span></code> and uses it to locate other511tests. If <code class="docutils literal notranslate"><span class="pre">M.__test__</span></code> exists, it must be a dict, and each512entry maps a (string) name to a function object, class object, or string.513Function and class object docstrings found from <code class="docutils literal notranslate"><span class="pre">M.__test__</span></code> are searched, and514strings are treated as if they were docstrings. In output, a key <code class="docutils literal notranslate"><span class="pre">K</span></code> in515<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>516<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>517<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">__test__</span> <span class="o">=</span> <span class="p">{</span>518 <span class="s1">'numbers'</span><span class="p">:</span> <span class="s2">"""</span>519<span class="s2">>>> factorial(6)</span>520<span class="s2">720</span>521 522<span class="s2">>>> [factorial(n) for n in range(6)]</span>523<span class="s2">[1, 1, 2, 6, 24, 120]</span>524<span class="s2">"""</span>525<span class="p">}</span>526</pre></div>527</div>528<p>The value of <code class="docutils literal notranslate"><span class="pre">example.__test__["numbers"]</span></code> will be treated as a529docstring and all the tests inside it will be run. It is530important to note that the value can be mapped to a function,531class object, or module; if so, <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code>532searches them recursively for docstrings, which are then scanned for tests.</p>533<p>Any classes found are recursively searched similarly, to test docstrings in534their contained methods and nested classes.</p>535<div class="admonition note">536<p class="admonition-title">Note</p>537<p><code class="docutils literal notranslate"><span class="pre">doctest</span></code> can only automatically discover classes and functions that are538defined at the module level or inside other classes.</p>539<p>Since nested classes and functions only exist when an outer function540is called, they cannot be discovered. Define them outside to make them visible.</p>541</div>542</section>543<section id="how-are-docstring-examples-recognized">544<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>545<p>In most cases a copy-and-paste of an interactive console session works fine,546but doctest isn’t trying to do an exact emulation of any specific Python shell.</p>547<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="c1"># comments are ignored</span>548<span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="mi">12</span>549<span class="gp">>>> </span><span class="n">x</span>550<span class="go">12</span>551<span class="gp">>>> </span><span class="k">if</span> <span class="n">x</span> <span class="o">==</span> <span class="mi">13</span><span class="p">:</span>552<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">"yes"</span><span class="p">)</span>553<span class="gp">... </span><span class="k">else</span><span class="p">:</span>554<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">"no"</span><span class="p">)</span>555<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">"NO"</span><span class="p">)</span>556<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">"NO!!!"</span><span class="p">)</span>557<span class="gp">...</span>558<span class="go">no</span>559<span class="go">NO</span>560<span class="go">NO!!!</span>561<span class="gp">>>></span>562</pre></div>563</div>564<p id="index-0">Any expected output must immediately follow the final <code class="docutils literal notranslate"><span class="pre">'>>></span> <span class="pre">'</span></code> or <code class="docutils literal notranslate"><span class="pre">'...</span> <span class="pre">'</span></code>565line containing the code, and the expected output (if any) extends to the next566<code class="docutils literal notranslate"><span class="pre">'>>></span> <span class="pre">'</span></code> or all-whitespace line.</p>567<p>The fine print:</p>568<ul>569<li><p>Expected output cannot contain an all-whitespace line, since such a line is570taken to signal the end of expected output. If expected output does contain a571blank line, put <code class="docutils literal notranslate"><span class="pre"><BLANKLINE></span></code> in your doctest example each place a blank line572is expected.</p></li>573<li><p>All hard tab characters are expanded to spaces, using 8-column tab stops.574Tabs in output generated by the tested code are not modified. Because any575hard tabs in the sample output <em>are</em> expanded, this means that if the code576output includes hard tabs, the only way the doctest can pass is if the577<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>578is in effect.579Alternatively, the test can be rewritten to capture the output and compare it580to an expected value as part of the test. This handling of tabs in the581source was arrived at through trial and error, and has proven to be the least582error prone way of handling them. It is possible to use a different583algorithm 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>584<li><p>Output to stdout is captured, but not output to stderr (exception tracebacks585are captured via a different means).</p></li>586<li><p>If you continue a line via backslashing in an interactive session, or for any587other reason use a backslash, you should use a raw docstring, which will588preserve your backslashes exactly as you type them:</p>589<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span><span class="w"> </span><span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>590<span class="gp">... </span><span class="w"> </span><span class="sa">r</span><span class="sd">'''Backslashes in a raw docstring: m\n'''</span>591<span class="gp">...</span>592<span class="gp">>>> </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>593<span class="go">Backslashes in a raw docstring: m\n</span>594</pre></div>595</div>596<p>Otherwise, the backslash will be interpreted as part of the string. For example,597the <code class="docutils literal notranslate"><span class="pre">\n</span></code> above would be interpreted as a newline character. Alternatively, you598can double each backslash in the doctest version (and not use a raw string):</p>599<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span><span class="w"> </span><span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>600<span class="gp">... </span><span class="w"> </span><span class="sd">'''Backslashes in a raw docstring: m\\n'''</span>601<span class="gp">...</span>602<span class="gp">>>> </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>603<span class="go">Backslashes in a raw docstring: m\n</span>604</pre></div>605</div>606</li>607<li><p>The starting column doesn’t matter:</p>608<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">assert</span> <span class="s2">"Easy!"</span>609<span class="go"> >>> import math</span>610<span class="go"> >>> math.floor(1.9)</span>611<span class="go"> 1</span>612</pre></div>613</div>614<p>and as many leading whitespace characters are stripped from the expected output615as appeared in the initial <code class="docutils literal notranslate"><span class="pre">'>>></span> <span class="pre">'</span></code> line that started the example.</p>616</li>617</ul>618</section>619<section id="what-s-the-execution-context">620<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>621<p>By default, each time <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> finds a docstring to test, it uses a622<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 the623module’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 behind624crumbs that accidentally allow another test to work. This means examples can625freely 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 earlier626in the docstring being run. Examples cannot see names defined in other627docstrings.</p>628<p>You can force use of your own dict as the execution context by passing629<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>630</section>631<section id="what-about-exceptions">632<span id="doctest-exceptions"></span><h3>What About Exceptions?<a class="headerlink" href="#what-about-exceptions" title="Link to this heading">¶</a></h3>633<p>No problem, provided that the traceback is the only output produced by the634example: 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 details635that are likely to change rapidly (for example, exact file paths and line636numbers), this is one case where doctest works hard to be flexible in what it637accepts.</p>638<p>Simple example:</p>639<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </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>640<span class="gt">Traceback (most recent call last):</span>641 File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>642<span class="gr">ValueError</span>: <span class="n">list.remove(x): x not in list</span>643</pre></div>644</div>645<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>646<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>647<p>The expected output for an exception must start with a traceback header, which648may be either of the following two lines, indented the same as the first line of649the example:</p>650<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>651<span class="n">Traceback</span> <span class="p">(</span><span class="n">innermost</span> <span class="n">last</span><span class="p">):</span>652</pre></div>653</div>654<p>The traceback header is followed by an optional traceback stack, whose contents655are ignored by doctest. The traceback stack is typically omitted, or copied656verbatim from an interactive session.</p>657<p>The traceback stack is followed by the most interesting part: the line(s)658containing the exception type and detail. This is usually the last line of a659traceback, but can extend across multiple lines if the exception has a660multi-line detail:</p>661<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'multi</span><span class="se">\n</span><span class="s1"> line</span><span class="se">\n</span><span class="s1">detail'</span><span class="p">)</span>662<span class="gt">Traceback (most recent call last):</span>663 File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>664<span class="gr">ValueError</span>: <span class="n">multi</span>665<span class="x"> line</span>666<span class="x">detail</span>667</pre></div>668</div>669<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 the670exception’s type and detail, and the rest are ignored.</p>671<p>Best practice is to omit the traceback stack, unless it adds significant672documentation value to the example. So the last example is probably better as:</p>673<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'multi</span><span class="se">\n</span><span class="s1"> line</span><span class="se">\n</span><span class="s1">detail'</span><span class="p">)</span>674<span class="gt">Traceback (most recent call last):</span>675<span class="w"> </span><span class="o">...</span>676<span class="gr">ValueError</span>: <span class="n">multi</span>677<span class="x"> line</span>678<span class="x">detail</span>679</pre></div>680</div>681<p>Note that tracebacks are treated very specially. In particular, in the682rewritten example, the use of <code class="docutils literal notranslate"><span class="pre">...</span></code> is independent of doctest’s683<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, or684could just as well be three (or three hundred) commas or digits, or an indented685transcript of a Monty Python skit.</p>686<p>Some details you should read once, but won’t need to remember:</p>687<ul class="simple">688<li><p>Doctest can’t guess whether your expected output came from an exception689traceback or from ordinary printing. So, e.g., an example that expects690<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 actually691raised or if the example merely prints that traceback text. In practice,692ordinary output rarely begins with a traceback header line, so this doesn’t693create real problems.</p></li>694<li><p>Each line of the traceback stack (if present) must be indented further than695the first line of the example, <em>or</em> start with a non-alphanumeric character.696The first line following the traceback header indented the same and starting697with an alphanumeric is taken to be the start of the exception detail. Of698course this does the right thing for genuine tracebacks.</p></li>699<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,700everything following the leftmost colon and any module information in the701exception name is ignored.</p></li>702<li><p>The interactive shell omits the traceback header line for some703<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 to704distinguish exceptions from non-exceptions. So in the rare case where you need705to test a <code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code> that omits the traceback header, you will need to706manually add the traceback header line to your test example.</p></li>707</ul>708<ul id="index-1">709<li><p>For some exceptions, Python displays the position of the error using <code class="docutils literal notranslate"><span class="pre">^</span></code>710markers and tildes:</p>711<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mi">1</span> <span class="o">+</span> <span class="kc">None</span>712 File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>713<span class="w"> </span><span class="mi">1</span> <span class="o">+</span> <span class="kc">None</span>714<span class="w"> </span><span class="pm">~~^~~~~~</span>715<span class="gr">TypeError</span>: <span class="n">unsupported operand type(s) for +: 'int' and 'NoneType'</span>716</pre></div>717</div>718<p>Since the lines showing the position of the error come before the exception type719and detail, they are not checked by doctest. For example, the following test720would pass, even though it puts the <code class="docutils literal notranslate"><span class="pre">^</span></code> marker in the wrong location:</p>721<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mi">1</span> <span class="o">+</span> <span class="kc">None</span>722 File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>723<span class="w"> </span><span class="mi">1</span> <span class="o">+</span> <span class="kc">None</span>724<span class="w"> </span><span class="pm">^~~~~~~~</span>725<span class="gr">TypeError</span>: <span class="n">unsupported operand type(s) for +: 'int' and 'NoneType'</span>726</pre></div>727</div>728</li>729</ul>730</section>731<section id="option-flags">732<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>733<p>A number of option flags control various aspects of doctest’s behavior.734Symbolic names for the flags are supplied as module constants, which can be735<a class="reference internal" href="../reference/expressions.html#bitwise"><span class="std std-ref">bitwise ORed</span></a> together and passed to various functions.736The names can also be used in <a class="reference internal" href="#doctest-directives"><span class="std std-ref">doctest directives</span></a>,737and may be passed to the doctest command line interface via the <code class="docutils literal notranslate"><span class="pre">-o</span></code> option.</p>738<p>The first group of options define test semantics, controlling aspects of how739doctest decides whether actual output matches an example’s expected output:</p>740<dl class="py data">741<dt class="sig sig-object py" id="doctest.DONT_ACCEPT_TRUE_FOR_1">742<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>743<dd><p>By default, if an expected output block contains just <code class="docutils literal notranslate"><span class="pre">1</span></code>, an actual output744block 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, and745similarly 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> is746specified, neither substitution is allowed. The default behavior caters to that747Python changed the return type of many functions from integer to boolean;748doctests expecting “little integer” output still work in these cases. This749option will probably go away, but not for several years.</p>750</dd></dl>751 752<dl class="py data" id="index-2">753<dt class="sig sig-object py" id="doctest.DONT_ACCEPT_BLANKLINE">754<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>755<dd><p>By default, if an expected output block contains a line containing only the756string <code class="docutils literal notranslate"><span class="pre"><BLANKLINE></span></code>, then that line will match a blank line in the actual757output. Because a genuinely blank line delimits the expected output, this is758the only way to communicate that a blank line is expected. When759<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>760</dd></dl>761 762<dl class="py data">763<dt class="sig sig-object py" id="doctest.NORMALIZE_WHITESPACE">764<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>765<dd><p>When specified, all sequences of whitespace (blanks and newlines) are treated as766equal. Any sequence of whitespace within the expected output will match any767sequence of whitespace within the actual output. By default, whitespace must768match 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 of769expected output is very long, and you want to wrap it across multiple lines in770your source.</p>771</dd></dl>772 773<dl class="py data" id="index-3">774<dt class="sig sig-object py" id="doctest.ELLIPSIS">775<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>776<dd><p>When specified, an ellipsis marker (<code class="docutils literal notranslate"><span class="pre">...</span></code>) in the expected output can match777any substring in the actual output. This includes substrings that span line778boundaries, and empty substrings, so it’s best to keep usage of this simple.779Complicated uses can lead to the same kinds of “oops, it matched too much!”780surprises that <code class="docutils literal notranslate"><span class="pre">.*</span></code> is prone to in regular expressions.</p>781</dd></dl>782 783<dl class="py data">784<dt class="sig sig-object py" id="doctest.IGNORE_EXCEPTION_DETAIL">785<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>786<dd><p>When specified, doctests expecting exceptions pass so long as an exception787of the expected type is raised, even if the details788(message and fully qualified exception name) don’t match.</p>789<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 actual790exception raised is <code class="docutils literal notranslate"><span class="pre">ValueError:</span> <span class="pre">3*14</span></code>, but will fail if, say, a791<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.792It will also ignore any fully qualified name included before the793exception class, which can vary between implementations and versions794of Python and the code/libraries in use.795Hence, all three of these variations will work with the flag specified:</p>796<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">'message'</span><span class="p">)</span>797<span class="gt">Traceback (most recent call last):</span>798<span class="gr">Exception</span>: <span class="n">message</span>799 800<span class="gp">>>> </span><span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">'message'</span><span class="p">)</span>801<span class="gt">Traceback (most recent call last):</span>802<span class="gr">builtins.Exception</span>: <span class="n">message</span>803 804<span class="gp">>>> </span><span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">'message'</span><span class="p">)</span>805<span class="gt">Traceback (most recent call last):</span>806<span class="gr">__main__.Exception</span>: <span class="n">message</span>807</pre></div>808</div>809<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 the810details of the exception message, but such a test may still fail based811on whether the module name is present or matches exactly.</p>812<div class="versionchanged">813<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 relating814to the module containing the exception under test.</p>815</div>816</dd></dl>817 818<dl class="py data">819<dt class="sig sig-object py" id="doctest.SKIP">820<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>821<dd><p>When specified, do not run the example at all. This can be useful in contexts822where doctest examples serve as both documentation and test cases, and an823example should be included for documentation purposes, but should not be824checked. E.g., the example’s output might be random; or the example might825depend on resources which would be unavailable to the test driver.</p>826<p>The SKIP flag can also be used for temporarily “commenting out” examples.</p>827</dd></dl>828 829<dl class="py data">830<dt class="sig sig-object py" id="doctest.COMPARISON_FLAGS">831<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>832<dd><p>A bitmask or’ing together all the comparison flags above.</p>833</dd></dl>834 835<p>The second group of options controls how test failures are reported:</p>836<dl class="py data">837<dt class="sig sig-object py" id="doctest.REPORT_UDIFF">838<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>839<dd><p>When specified, failures that involve multi-line expected and actual outputs are840displayed using a unified diff.</p>841</dd></dl>842 843<dl class="py data">844<dt class="sig sig-object py" id="doctest.REPORT_CDIFF">845<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>846<dd><p>When specified, failures that involve multi-line expected and actual outputs847will be displayed using a context diff.</p>848</dd></dl>849 850<dl class="py data">851<dt class="sig sig-object py" id="doctest.REPORT_NDIFF">852<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>853<dd><p>When specified, differences are computed by <code class="docutils literal notranslate"><span class="pre">difflib.Differ</span></code>, using the same854algorithm as the popular <code class="file docutils literal notranslate"><span class="pre">ndiff.py</span></code> utility. This is the only method that855marks differences within lines as well as across lines. For example, if a line856of expected output contains digit <code class="docutils literal notranslate"><span class="pre">1</span></code> where actual output contains letter857<code class="docutils literal notranslate"><span class="pre">l</span></code>, a line is inserted with a caret marking the mismatching column positions.</p>858</dd></dl>859 860<dl class="py data">861<dt class="sig sig-object py" id="doctest.REPORT_ONLY_FIRST_FAILURE">862<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>863<dd><p>When specified, display the first failing example in each doctest, but suppress864output for all remaining examples. This will prevent doctest from reporting865correct examples that break because of earlier failures; but it might also hide866incorrect examples that fail independently of the first failure. When867<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 are868still run, and still count towards the total number of failures reported; only869the output is suppressed.</p>870</dd></dl>871 872<dl class="py data">873<dt class="sig sig-object py" id="doctest.FAIL_FAST">874<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>875<dd><p>When specified, exit after the first failing example and don’t attempt to run876the remaining examples. Thus, the number of failures reported will be at most8771. This flag may be useful during debugging, since examples after the first878failure won’t even produce debugging output.</p>879</dd></dl>880 881<dl class="py data">882<dt class="sig sig-object py" id="doctest.REPORTING_FLAGS">883<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>884<dd><p>A bitmask or’ing together all the reporting flags above.</p>885</dd></dl>886 887<p>There is also a way to register new option flag names, though this isn’t888useful unless you intend to extend <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> internals via subclassing:</p>889<dl class="py function">890<dt class="sig sig-object py" id="doctest.register_optionflag">891<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>892<dd><p>Create a new option flag with a given name, and return the new flag’s integer893value. <code class="xref py py-func docutils literal notranslate"><span class="pre">register_optionflag()</span></code> can be used when subclassing894<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 are895supported by your subclasses. <code class="xref py py-func docutils literal notranslate"><span class="pre">register_optionflag()</span></code> should always be896called using the following idiom:</p>897<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">'MY_FLAG'</span><span class="p">)</span>898</pre></div>899</div>900</dd></dl>901 902</section>903<section id="directives">904<span id="doctest-directives"></span><span id="index-4"></span><h3>Directives<a class="headerlink" href="#directives" title="Link to this heading">¶</a></h3>905<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 are906special Python comments following an example’s source code:</p>907<pre class="highlight">908<strong id="grammar-token-doctest-directive">directive</strong>: <span class="sx">"#"</span> <span class="sx">"doctest:"</span> <a class="reference internal" href="#grammar-token-doctest-directive_options"><code class="xref docutils literal notranslate"><span class="pre">directive_options</span></code></a>909<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> (<span class="sx">","</span> <a class="reference internal" href="#grammar-token-doctest-directive_option"><code class="xref docutils literal notranslate"><span class="pre">directive_option</span></code></a>)*910<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>911<strong id="grammar-token-doctest-on_or_off">on_or_off</strong>: <span class="sx">"+"</span> | <span class="sx">"-"</span>912<strong id="grammar-token-doctest-directive_option_name">directive_option_name</strong>: <span class="sx">"DONT_ACCEPT_BLANKLINE"</span> | <span class="sx">"NORMALIZE_WHITESPACE"</span> | ...913</pre>914<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 option915name. The directive option name can be any of the option flag names explained916above.</p>917<p>An example’s doctest directives modify doctest’s behavior for that single918example. 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>919<p>For example, this test passes:</p>920<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </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>921<span class="go">[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,</span>922<span class="go">10, 11, 12, 13, 14, 15, 16, 17, 18, 19]</span>923</pre></div>924</div>925<p>Without the directive it would fail, both because the actual output doesn’t have926two blanks before the single-digit list elements, and because the actual output927is on a single line. This test also passes, and also requires a directive to do928so:</p>929<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </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>930<span class="go">[0, 1, ..., 18, 19]</span>931</pre></div>932</div>933<p>Multiple directives can be used on a single physical line, separated by934commas:</p>935<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </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>936<span class="go">[0, 1, ..., 18, 19]</span>937</pre></div>938</div>939<p>If multiple directive comments are used for a single example, then they are940combined:</p>941<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </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>942<span class="gp">... </span> <span class="c1"># doctest: +NORMALIZE_WHITESPACE</span>943<span class="go">[0, 1, ..., 18, 19]</span>944</pre></div>945</div>946<p>As the previous example shows, you can add <code class="docutils literal notranslate"><span class="pre">...</span></code> lines to your example947containing only directives. This can be useful when an example is too long for948a directive to comfortably fit on the same line:</p>949<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </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>950<span class="gp">... </span><span class="c1"># doctest: +ELLIPSIS</span>951<span class="go">[0, ..., 4, 10, ..., 19, 30, ..., 39]</span>952</pre></div>953</div>954<p>Note that since all options are disabled by default, and directives apply only955to the example they appear in, enabling options (via <code class="docutils literal notranslate"><span class="pre">+</span></code> in a directive) is956usually the only meaningful choice. However, option flags can also be passed to957functions that run doctests, establishing different defaults. In such cases,958disabling an option via <code class="docutils literal notranslate"><span class="pre">-</span></code> in a directive can be useful.</p>959</section>960<section id="warnings">961<span id="doctest-warnings"></span><h3>Warnings<a class="headerlink" href="#warnings" title="Link to this heading">¶</a></h3>962<p><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> is serious about requiring exact matches in expected output. If963even a single character doesn’t match, the test fails. This will probably964surprise you a few times, as you learn exactly what Python does and doesn’t965guarantee about output. For example, when printing a set, Python doesn’t966guarantee that the element is printed in any particular order, so a test like</p>967<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">foo</span><span class="p">()</span>968<span class="go">{"spam", "eggs"}</span>969</pre></div>970</div>971<p>is vulnerable! One workaround is to do</p>972<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">foo</span><span class="p">()</span> <span class="o">==</span> <span class="p">{</span><span class="s2">"spam"</span><span class="p">,</span> <span class="s2">"eggs"</span><span class="p">}</span>973<span class="go">True</span>974</pre></div>975</div>976<p>instead. Another is to do</p>977<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </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>978<span class="gp">>>> </span><span class="n">d</span>979<span class="go">['eggs', 'spam']</span>980</pre></div>981</div>982<p>There are others, but you get the idea.</p>983<p>Another bad idea is to print things that embed an object address, like</p>984<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </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>985<span class="go">7948648</span>986<span class="gp">>>> </span><span class="k">class</span><span class="w"> </span><span class="nc">C</span><span class="p">:</span> <span class="k">pass</span>987<span class="gp">>>> </span><span class="n">C</span><span class="p">()</span> <span class="c1"># the default repr() for instances embeds an address</span>988<span class="go"><C object at 0x00AC18F0></span>989</pre></div>990</div>991<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>992<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">C</span><span class="p">()</span> <span class="c1"># doctest: +ELLIPSIS</span>993<span class="go"><C object at 0x...></span>994</pre></div>995</div>996<p>Floating-point numbers are also subject to small output variations across997platforms, because Python defers to the platform C library for some998floating-point calculations, and C libraries vary widely in quality here.</p>999<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mi">1000</span><span class="o">**</span><span class="mf">0.1</span> <span class="c1"># risky</span>1000<span class="go">1.9952623149688797</span>1001<span class="gp">>>> </span><span class="nb">round</span><span class="p">(</span><span class="mi">1000</span><span class="o">**</span><span class="mf">0.1</span><span class="p">,</span> <span class="mi">9</span><span class="p">)</span> <span class="c1"># safer</span>1002<span class="go">1.995262315</span>1003<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">'</span><span class="si">{</span><span class="mi">1000</span><span class="o">**</span><span class="mf">0.1</span><span class="si">:</span><span class="s1">.4f</span><span class="si">}</span><span class="s1">'</span><span class="p">)</span> <span class="c1"># much safer</span>1004<span class="go">1.9953</span>1005</pre></div>1006</div>1007<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 often1008contrive doctest examples to produce numbers of that form:</p>1009<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mf">3.</span><span class="o">/</span><span class="mi">4</span> <span class="c1"># utterly safe</span>1010<span class="go">0.75</span>1011</pre></div>1012</div>1013<p>Simple fractions are also easier for people to understand, and that makes for1014better documentation.</p>1015</section>1016</section>1017<section id="basic-api">1018<span id="doctest-basic-api"></span><h2>Basic API<a class="headerlink" href="#basic-api" title="Link to this heading">¶</a></h2>1019<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 to1020doctest that should be sufficient for most basic uses. For a less formal1021introduction 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>1022and <a class="reference internal" href="#doctest-simple-testfile"><span class="std std-ref">Simple Usage: Checking Examples in a Text File</span></a>.</p>1023<dl class="py function">1024<dt class="sig sig-object py" id="doctest.testfile">1025<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>1026<dd><p>All arguments except <em>filename</em> are optional, and should be specified in keyword1027form.</p>1028<p>Test examples in the file named <em>filename</em>. Return <code class="docutils literal notranslate"><span class="pre">(failure_count,</span>1029<span class="pre">test_count)</span></code>.</p>1030<p>Optional argument <em>module_relative</em> specifies how the filename should be1031interpreted:</p>1032<ul class="simple">1033<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 an1034OS-independent module-relative path. By default, this path is relative to the1035calling module’s directory; but if the <em>package</em> argument is specified, then it1036is relative to that package. To ensure OS-independence, <em>filename</em> should use1037<code class="docutils literal notranslate"><span class="pre">/</span></code> characters to separate path segments, and may not be an absolute path1038(i.e., it may not begin with <code class="docutils literal notranslate"><span class="pre">/</span></code>).</p></li>1039<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-specific1040path. The path may be absolute or relative; relative paths are resolved with1041respect to the current working directory.</p></li>1042</ul>1043<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>,1044<code class="docutils literal notranslate"><span class="pre">os.path.basename(filename)</span></code> is used.</p>1045<p>Optional argument <em>package</em> is a Python package or the name of a Python package1046whose directory should be used as the base directory for a module-relative1047filename. If no package is specified, then the calling module’s directory is1048used as the base directory for module-relative filenames. It is an error to1049specify <em>package</em> if <em>module_relative</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>1050<p>Optional argument <em>globs</em> gives a dict to be used as the globals when executing1051examples. A new shallow copy of this dict is created for the doctest, so its1052examples start with a clean slate. By default, or if <code class="docutils literal notranslate"><span class="pre">None</span></code>, a new empty dict1053is used.</p>1054<p>Optional argument <em>extraglobs</em> gives a dict merged into the globals used to1055execute 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> and1056<em>extraglobs</em> have a common key, the associated value in <em>extraglobs</em> appears in1057the combined dict. By default, or if <code class="docutils literal notranslate"><span class="pre">None</span></code>, no extra globals are used. This1058is an advanced feature that allows parameterization of doctests. For example, a1059doctest can be written for a base class, using a generic name for the class,1060then reused to test any number of subclasses by passing an <em>extraglobs</em> dict1061mapping the generic name to the subclass to be tested.</p>1062<p>Optional argument <em>verbose</em> prints lots of stuff if true, and prints only1063failures 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>1064is in <a class="reference internal" href="sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a>.</p>1065<p>Optional argument <em>report</em> prints a summary at the end when true, else prints1066nothing at the end. In verbose mode, the summary is detailed, else the summary1067is very brief (in fact, empty if all tests passed).</p>1068<p>Optional argument <em>optionflags</em> (default value <code class="docutils literal notranslate"><span class="pre">0</span></code>) takes the1069<a class="reference internal" href="../reference/expressions.html#bitwise"><span class="std std-ref">bitwise OR</span></a> of option flags.1070See section <a class="reference internal" href="#doctest-options"><span class="std std-ref">Option Flags</span></a>.</p>1071<p>Optional argument <em>raise_on_error</em> defaults to false. If true, an exception is1072raised upon the first failure or unexpected exception in an example. This1073allows failures to be post-mortem debugged. Default behavior is to continue1074running examples.</p>1075<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) that1076should be used to extract tests from the files. It defaults to a normal parser1077(i.e., <code class="docutils literal notranslate"><span class="pre">DocTestParser()</span></code>).</p>1078<p>Optional argument <em>encoding</em> specifies an encoding that should be used to1079convert the file to unicode.</p>1080</dd></dl>1081 1082<dl class="py function">1083<dt class="sig sig-object py" id="doctest.testmod">1084<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>1085<dd><p>All arguments are optional, and all except for <em>m</em> should be specified in1086keyword form.</p>1087<p>Test examples in docstrings in functions and classes reachable from module <em>m</em>1088(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 with1089<code class="docutils literal notranslate"><span class="pre">m.__doc__</span></code>.</p>1090<p>Also test examples reachable from dict <code class="docutils literal notranslate"><span class="pre">m.__test__</span></code>, if it exists.1091<code class="docutils literal notranslate"><span class="pre">m.__test__</span></code> maps names (strings) to functions, classes and1092strings; function and class docstrings are searched for examples; strings are1093searched directly, as if they were docstrings.</p>1094<p>Only docstrings attached to objects belonging to module <em>m</em> are searched.</p>1095<p>Return <code class="docutils literal notranslate"><span class="pre">(failure_count,</span> <span class="pre">test_count)</span></code>.</p>1096<p>Optional argument <em>name</em> gives the name of the module; by default, or if1097<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>1098<p>Optional argument <em>exclude_empty</em> defaults to false. If true, objects for which1099no doctests are found are excluded from consideration. The default is a backward1100compatibility hack, so that code still using1101<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> in1102conjunction with <code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code> continues to get output for objects with no1103tests. 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>1104constructor defaults to true.</p>1105<p>Optional arguments <em>extraglobs</em>, <em>verbose</em>, <em>report</em>, <em>optionflags</em>,1106<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>1107above, except that <em>globs</em> defaults to <code class="docutils literal notranslate"><span class="pre">m.__dict__</span></code>.</p>1108</dd></dl>1109 1110<dl class="py function">1111<dt class="sig sig-object py" id="doctest.run_docstring_examples">1112<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>1113<dd><p>Test examples associated with object <em>f</em>; for example, <em>f</em> may be a string,1114a module, a function, or a class object.</p>1115<p>A shallow copy of dictionary argument <em>globs</em> is used for the execution context.</p>1116<p>Optional argument <em>name</em> is used in failure messages, and defaults to1117<code class="docutils literal notranslate"><span class="pre">"NoName"</span></code>.</p>1118<p>If optional argument <em>verbose</em> is true, output is generated even if there are no1119failures. By default, output is generated only in case of an example failure.</p>1120<p>Optional argument <em>compileflags</em> gives the set of flags that should be used by1121the Python compiler when running the examples. By default, or if <code class="docutils literal notranslate"><span class="pre">None</span></code>,1122flags are deduced corresponding to the set of future features found in <em>globs</em>.</p>1123<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>1124</dd></dl>1125 1126</section>1127<section id="unittest-api">1128<span id="doctest-unittest-api"></span><h2>Unittest API<a class="headerlink" href="#unittest-api" title="Link to this heading">¶</a></h2>1129<p>As your collection of doctest’ed modules grows, you’ll want a way to run all1130their doctests systematically. <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> provides two functions that can1131be 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 files1132containing doctests. To integrate with <code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code> test discovery, include1133a <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>1134<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">unittest</span>1135<span class="kn">import</span><span class="w"> </span><span class="nn">doctest</span>1136<span class="kn">import</span><span class="w"> </span><span class="nn">my_module_with_doctests</span>1137 1138<span class="k">def</span><span class="w"> </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>1139 <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>1140 <span class="k">return</span> <span class="n">tests</span>1141</pre></div>1142</div>1143<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> instances1144from text files and modules with doctests:</p>1145<dl class="py function">1146<dt class="sig sig-object py" id="doctest.DocFileSuite">1147<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>1148<dd><p>Convert doctest tests from one or more text files to a1149<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>1150<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 unittest1151framework and runs the interactive examples in each file.1152Each file is run as a separate unit test, and each example in a file1153is run as a <a class="reference internal" href="unittest.html#subtests"><span class="std std-ref">subtest</span></a>.1154If any example in a file fails, then the synthesized unit test fails.1155The traceback for failure or error contains the name of the file1156containing the test and a (sometimes approximate) line number.1157If all the examples in a file are skipped, then the synthesized unit1158test is also marked as skipped.</p>1159<p>Pass one or more paths (as strings) to text files to be examined.</p>1160<p>Options may be provided as keyword arguments:</p>1161<p>Optional argument <em>module_relative</em> specifies how the filenames in <em>paths</em>1162should be interpreted:</p>1163<ul class="simple">1164<li><p>If <em>module_relative</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default), then each filename in1165<em>paths</em> specifies an OS-independent module-relative path. By default, this1166path is relative to the calling module’s directory; but if the <em>package</em>1167argument is specified, then it is relative to that package. To ensure1168OS-independence, each filename should use <code class="docutils literal notranslate"><span class="pre">/</span></code> characters to separate path1169segments, and may not be an absolute path (i.e., it may not begin with1170<code class="docutils literal notranslate"><span class="pre">/</span></code>).</p></li>1171<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> specifies1172an OS-specific path. The path may be absolute or relative; relative paths1173are resolved with respect to the current working directory.</p></li>1174</ul>1175<p>Optional argument <em>package</em> is a Python package or the name of a Python1176package whose directory should be used as the base directory for1177module-relative filenames in <em>paths</em>. If no package is specified, then the1178calling module’s directory is used as the base directory for module-relative1179filenames. It is an error to specify <em>package</em> if <em>module_relative</em> is1180<code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>1181<p>Optional argument <em>setUp</em> specifies a set-up function for the test suite.1182This is called before running the tests in each file. The <em>setUp</em> function1183will 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 <em>setUp</em> function can access the1184test globals as the <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> attribute of the test passed.</p>1185<p>Optional argument <em>tearDown</em> specifies a tear-down function for the test1186suite. This is called after running the tests in each file. The <em>tearDown</em>1187function 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 <em>tearDown</em> function can1188access the test globals as the <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> attribute of the test1189passed.</p>1190<p>Optional argument <em>globs</em> is a dictionary containing the initial global1191variables for the tests. A new copy of this dictionary is created for each1192test. By default, <em>globs</em> is a new empty dictionary.</p>1193<p>Optional argument <em>optionflags</em> specifies the default doctest options for the1194tests, created by or-ing together individual option flags. See section1195<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> below1196for a better way to set reporting options.</p>1197<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)1198that should be used to extract tests from the files. It defaults to a normal1199parser (i.e., <code class="docutils literal notranslate"><span class="pre">DocTestParser()</span></code>).</p>1200<p>Optional argument <em>encoding</em> specifies an encoding that should be used to