mirror of
				https://github.com/bunny-lab-io/Borealis.git
				synced 2025-10-28 07:41:58 -06:00 
			
		
		
		
	
		
			
				
	
	
		
			2155 lines
		
	
	
		
			212 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			2155 lines
		
	
	
		
			212 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
| <!DOCTYPE html>
 | ||
| 
 | ||
| <html lang="en" data-content_root="../">
 | ||
|   <head>
 | ||
|     <meta charset="utf-8" />
 | ||
|     <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
 | ||
| <meta property="og:title" content="doctest — Test interactive Python examples" />
 | ||
| <meta property="og:type" content="website" />
 | ||
| <meta property="og:url" content="https://docs.python.org/3/library/doctest.html" />
 | ||
| <meta property="og:site_name" content="Python documentation" />
 | ||
| <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..." />
 | ||
| <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
 | ||
| <meta property="og:image:alt" content="Python documentation" />
 | ||
| <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..." />
 | ||
| <meta property="og:image:width" content="200">
 | ||
| <meta property="og:image:height" content="200">
 | ||
| <meta name="theme-color" content="#3776ab">
 | ||
| 
 | ||
|     <title>doctest — Test interactive Python examples — Python 3.13.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
 | ||
|     
 | ||
|     <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
 | ||
|     <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=23252803" />
 | ||
|     <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />
 | ||
|     
 | ||
|     <script src="../_static/documentation_options.js?v=5d57ca2d"></script>
 | ||
|     <script src="../_static/doctools.js?v=9bcbadda"></script>
 | ||
|     <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
 | ||
|     
 | ||
|     <script src="../_static/sidebar.js"></script>
 | ||
|     
 | ||
|     <link rel="search" type="application/opensearchdescription+xml"
 | ||
|           title="Search within Python 3.13.3 documentation"
 | ||
|           href="../_static/opensearch.xml"/>
 | ||
|     <link rel="author" title="About these documents" href="../about.html" />
 | ||
|     <link rel="index" title="Index" href="../genindex.html" />
 | ||
|     <link rel="search" title="Search" href="../search.html" />
 | ||
|     <link rel="copyright" title="Copyright" href="../copyright.html" />
 | ||
|     <link rel="next" title="unittest — Unit testing framework" href="unittest.html" />
 | ||
|     <link rel="prev" title="Python Development Mode" href="devmode.html" />
 | ||
|     
 | ||
|     <link rel="canonical" href="https://docs.python.org/3/library/doctest.html">
 | ||
|     
 | ||
|       
 | ||
|     
 | ||
| 
 | ||
|     
 | ||
|     <style>
 | ||
|       @media only screen {
 | ||
|         table.full-width-table {
 | ||
|             width: 100%;
 | ||
|         }
 | ||
|       }
 | ||
|     </style>
 | ||
| <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
 | ||
|     <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
 | ||
|             <script type="text/javascript" src="../_static/copybutton.js"></script>
 | ||
|             <script type="text/javascript" src="../_static/menu.js"></script>
 | ||
|             <script type="text/javascript" src="../_static/search-focus.js"></script>
 | ||
|             <script type="text/javascript" src="../_static/themetoggle.js"></script> 
 | ||
|             <script type="text/javascript" src="../_static/rtd_switcher.js"></script>
 | ||
|             <meta name="readthedocs-addons-api-version" content="1">
 | ||
| 
 | ||
|   </head>
 | ||
| <body>
 | ||
| <div class="mobile-nav">
 | ||
|     <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
 | ||
|            aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
 | ||
|     <nav class="nav-content" role="navigation">
 | ||
|         <label for="menuToggler" class="toggler__label">
 | ||
|             <span></span>
 | ||
|         </label>
 | ||
|         <span class="nav-items-wrapper">
 | ||
|             <a href="https://www.python.org/" class="nav-logo">
 | ||
|                 <img src="../_static/py.svg" alt="Python logo"/>
 | ||
|             </a>
 | ||
|             <span class="version_switcher_placeholder"></span>
 | ||
|             <form role="search" class="search" action="../search.html" method="get">
 | ||
|                 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
 | ||
|                     <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>
 | ||
|                 </svg>
 | ||
|                 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
 | ||
|                 <input type="submit" value="Go"/>
 | ||
|             </form>
 | ||
|         </span>
 | ||
|     </nav>
 | ||
|     <div class="menu-wrapper">
 | ||
|         <nav class="menu" role="navigation" aria-label="main navigation">
 | ||
|             <div class="language_switcher_placeholder"></div>
 | ||
|             
 | ||
| <label class="theme-selector-label">
 | ||
|     Theme
 | ||
|     <select class="theme-selector" oninput="activateTheme(this.value)">
 | ||
|         <option value="auto" selected>Auto</option>
 | ||
|         <option value="light">Light</option>
 | ||
|         <option value="dark">Dark</option>
 | ||
|     </select>
 | ||
| </label>
 | ||
|   <div>
 | ||
|     <h3><a href="../contents.html">Table of Contents</a></h3>
 | ||
|     <ul>
 | ||
| <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>
 | ||
| <li><a class="reference internal" href="#simple-usage-checking-examples-in-docstrings">Simple Usage: Checking Examples in Docstrings</a></li>
 | ||
| <li><a class="reference internal" href="#simple-usage-checking-examples-in-a-text-file">Simple Usage: Checking Examples in a Text File</a></li>
 | ||
| <li><a class="reference internal" href="#command-line-usage">Command-line Usage</a></li>
 | ||
| <li><a class="reference internal" href="#how-it-works">How It Works</a><ul>
 | ||
| <li><a class="reference internal" href="#which-docstrings-are-examined">Which Docstrings Are Examined?</a></li>
 | ||
| <li><a class="reference internal" href="#how-are-docstring-examples-recognized">How are Docstring Examples Recognized?</a></li>
 | ||
| <li><a class="reference internal" href="#what-s-the-execution-context">What’s the Execution Context?</a></li>
 | ||
| <li><a class="reference internal" href="#what-about-exceptions">What About Exceptions?</a></li>
 | ||
| <li><a class="reference internal" href="#option-flags">Option Flags</a></li>
 | ||
| <li><a class="reference internal" href="#directives">Directives</a></li>
 | ||
| <li><a class="reference internal" href="#warnings">Warnings</a></li>
 | ||
| </ul>
 | ||
| </li>
 | ||
| <li><a class="reference internal" href="#basic-api">Basic API</a></li>
 | ||
| <li><a class="reference internal" href="#unittest-api">Unittest API</a></li>
 | ||
| <li><a class="reference internal" href="#advanced-api">Advanced API</a><ul>
 | ||
| <li><a class="reference internal" href="#doctest-objects">DocTest Objects</a></li>
 | ||
| <li><a class="reference internal" href="#example-objects">Example Objects</a></li>
 | ||
| <li><a class="reference internal" href="#doctestfinder-objects">DocTestFinder objects</a></li>
 | ||
| <li><a class="reference internal" href="#doctestparser-objects">DocTestParser objects</a></li>
 | ||
| <li><a class="reference internal" href="#testresults-objects">TestResults objects</a></li>
 | ||
| <li><a class="reference internal" href="#doctestrunner-objects">DocTestRunner objects</a></li>
 | ||
| <li><a class="reference internal" href="#outputchecker-objects">OutputChecker objects</a></li>
 | ||
| </ul>
 | ||
| </li>
 | ||
| <li><a class="reference internal" href="#debugging">Debugging</a></li>
 | ||
| <li><a class="reference internal" href="#soapbox">Soapbox</a></li>
 | ||
| </ul>
 | ||
| </li>
 | ||
| </ul>
 | ||
| 
 | ||
|   </div>
 | ||
|   <div>
 | ||
|     <h4>Previous topic</h4>
 | ||
|     <p class="topless"><a href="devmode.html"
 | ||
|                           title="previous chapter">Python Development Mode</a></p>
 | ||
|   </div>
 | ||
|   <div>
 | ||
|     <h4>Next topic</h4>
 | ||
|     <p class="topless"><a href="unittest.html"
 | ||
|                           title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code> — Unit testing framework</a></p>
 | ||
|   </div>
 | ||
|   <div role="note" aria-label="source link">
 | ||
|     <h3>This Page</h3>
 | ||
|     <ul class="this-page-menu">
 | ||
|       <li><a href="../bugs.html">Report a Bug</a></li>
 | ||
|       <li>
 | ||
|         <a href="https://github.com/python/cpython/blob/main/Doc/library/doctest.rst"
 | ||
|             rel="nofollow">Show Source
 | ||
|         </a>
 | ||
|       </li>
 | ||
|     </ul>
 | ||
|   </div>
 | ||
|         </nav>
 | ||
|     </div>
 | ||
| </div>
 | ||
| 
 | ||
|   
 | ||
|     <div class="related" role="navigation" aria-label="Related">
 | ||
|       <h3>Navigation</h3>
 | ||
|       <ul>
 | ||
|         <li class="right" style="margin-right: 10px">
 | ||
|           <a href="../genindex.html" title="General Index"
 | ||
|              accesskey="I">index</a></li>
 | ||
|         <li class="right" >
 | ||
|           <a href="../py-modindex.html" title="Python Module Index"
 | ||
|              >modules</a> |</li>
 | ||
|         <li class="right" >
 | ||
|           <a href="unittest.html" title="unittest — Unit testing framework"
 | ||
|              accesskey="N">next</a> |</li>
 | ||
|         <li class="right" >
 | ||
|           <a href="devmode.html" title="Python Development Mode"
 | ||
|              accesskey="P">previous</a> |</li>
 | ||
| 
 | ||
|           <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
 | ||
|           <li><a href="https://www.python.org/">Python</a> »</li>
 | ||
|           <li class="switchers">
 | ||
|             <div class="language_switcher_placeholder"></div>
 | ||
|             <div class="version_switcher_placeholder"></div>
 | ||
|           </li>
 | ||
|           <li>
 | ||
|               
 | ||
|           </li>
 | ||
|     <li id="cpython-language-and-version">
 | ||
|       <a href="../index.html">3.13.3 Documentation</a> »
 | ||
|     </li>
 | ||
| 
 | ||
|           <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
 | ||
|           <li class="nav-item nav-item-2"><a href="development.html" accesskey="U">Development Tools</a> »</li>
 | ||
|         <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>
 | ||
|                 <li class="right">
 | ||
|                     
 | ||
| 
 | ||
|     <div class="inline-search" role="search">
 | ||
|         <form class="inline-search" action="../search.html" method="get">
 | ||
|           <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
 | ||
|           <input type="submit" value="Go" />
 | ||
|         </form>
 | ||
|     </div>
 | ||
|                      |
 | ||
|                 </li>
 | ||
|             <li class="right">
 | ||
| <label class="theme-selector-label">
 | ||
|     Theme
 | ||
|     <select class="theme-selector" oninput="activateTheme(this.value)">
 | ||
|         <option value="auto" selected>Auto</option>
 | ||
|         <option value="light">Light</option>
 | ||
|         <option value="dark">Dark</option>
 | ||
|     </select>
 | ||
| </label> |</li>
 | ||
|             
 | ||
|       </ul>
 | ||
|     </div>    
 | ||
| 
 | ||
|     <div class="document">
 | ||
|       <div class="documentwrapper">
 | ||
|         <div class="bodywrapper">
 | ||
|           <div class="body" role="main">
 | ||
|             
 | ||
|   <section id="module-doctest">
 | ||
| <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>
 | ||
| <p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/3.13/Lib/doctest.py">Lib/doctest.py</a></p>
 | ||
| <hr class="docutils" />
 | ||
| <p>The <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> module searches for pieces of text that look like interactive
 | ||
| Python sessions, and then executes those sessions to verify that they work
 | ||
| exactly as shown.  There are several common ways to use doctest:</p>
 | ||
| <ul class="simple">
 | ||
| <li><p>To check that a module’s docstrings are up-to-date by verifying that all
 | ||
| interactive examples still work as documented.</p></li>
 | ||
| <li><p>To perform regression testing by verifying that interactive examples from a
 | ||
| test file or a test object work as expected.</p></li>
 | ||
| <li><p>To write tutorial documentation for a package, liberally illustrated with
 | ||
| input-output examples.  Depending on whether the examples or the expository text
 | ||
| are emphasized, this has the flavor of “literate testing” or “executable
 | ||
| documentation”.</p></li>
 | ||
| </ul>
 | ||
| <p>Here’s a complete but small example module:</p>
 | ||
| <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
 | ||
| <span class="sd">This is the "example" module.</span>
 | ||
| 
 | ||
| <span class="sd">The example module supplies one function, factorial().  For example,</span>
 | ||
| 
 | ||
| <span class="sd">>>> factorial(5)</span>
 | ||
| <span class="sd">120</span>
 | ||
| <span class="sd">"""</span>
 | ||
| 
 | ||
| <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>
 | ||
| <span class="w">    </span><span class="sd">"""Return the factorial of n, an exact integer >= 0.</span>
 | ||
| 
 | ||
| <span class="sd">    >>> [factorial(n) for n in range(6)]</span>
 | ||
| <span class="sd">    [1, 1, 2, 6, 24, 120]</span>
 | ||
| <span class="sd">    >>> factorial(30)</span>
 | ||
| <span class="sd">    265252859812191058636308480000000</span>
 | ||
| <span class="sd">    >>> factorial(-1)</span>
 | ||
| <span class="sd">    Traceback (most recent call last):</span>
 | ||
| <span class="sd">        ...</span>
 | ||
| <span class="sd">    ValueError: n must be >= 0</span>
 | ||
| 
 | ||
| <span class="sd">    Factorials of floats are OK, but the float must be an exact integer:</span>
 | ||
| <span class="sd">    >>> factorial(30.1)</span>
 | ||
| <span class="sd">    Traceback (most recent call last):</span>
 | ||
| <span class="sd">        ...</span>
 | ||
| <span class="sd">    ValueError: n must be exact integer</span>
 | ||
| <span class="sd">    >>> factorial(30.0)</span>
 | ||
| <span class="sd">    265252859812191058636308480000000</span>
 | ||
| 
 | ||
| <span class="sd">    It must also not be ridiculously large:</span>
 | ||
| <span class="sd">    >>> factorial(1e100)</span>
 | ||
| <span class="sd">    Traceback (most recent call last):</span>
 | ||
| <span class="sd">        ...</span>
 | ||
| <span class="sd">    OverflowError: n too large</span>
 | ||
| <span class="sd">    """</span>
 | ||
| 
 | ||
|     <span class="kn">import</span><span class="w"> </span><span class="nn">math</span>
 | ||
|     <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>
 | ||
|         <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>
 | ||
|     <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>
 | ||
|         <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>
 | ||
|     <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>
 | ||
|         <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>
 | ||
|     <span class="n">result</span> <span class="o">=</span> <span class="mi">1</span>
 | ||
|     <span class="n">factor</span> <span class="o">=</span> <span class="mi">2</span>
 | ||
|     <span class="k">while</span> <span class="n">factor</span> <span class="o"><=</span> <span class="n">n</span><span class="p">:</span>
 | ||
|         <span class="n">result</span> <span class="o">*=</span> <span class="n">factor</span>
 | ||
|         <span class="n">factor</span> <span class="o">+=</span> <span class="mi">1</span>
 | ||
|     <span class="k">return</span> <span class="n">result</span>
 | ||
| 
 | ||
| 
 | ||
| <span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
 | ||
|     <span class="kn">import</span><span class="w"> </span><span class="nn">doctest</span>
 | ||
|     <span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">()</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>If you run <code class="file docutils literal notranslate"><span class="pre">example.py</span></code> directly from the command line, <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a>
 | ||
| works its magic:</p>
 | ||
| <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="gp">$</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>There’s no output!  That’s normal, and it means all the examples worked.  Pass
 | ||
| <code class="docutils literal notranslate"><span class="pre">-v</span></code> to the script, and <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> prints a detailed log of what
 | ||
| it’s trying, and prints a summary at the end:</p>
 | ||
| <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>example.py<span class="w"> </span>-v
 | ||
| <span class="go">Trying:</span>
 | ||
| <span class="go">    factorial(5)</span>
 | ||
| <span class="go">Expecting:</span>
 | ||
| <span class="go">    120</span>
 | ||
| <span class="go">ok</span>
 | ||
| <span class="go">Trying:</span>
 | ||
| <span class="go">    [factorial(n) for n in range(6)]</span>
 | ||
| <span class="go">Expecting:</span>
 | ||
| <span class="go">    [1, 1, 2, 6, 24, 120]</span>
 | ||
| <span class="go">ok</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>And so on, eventually ending with:</p>
 | ||
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Trying:
 | ||
|     factorial(1e100)
 | ||
| Expecting:
 | ||
|     Traceback (most recent call last):
 | ||
|         ...
 | ||
|     OverflowError: n too large
 | ||
| ok
 | ||
| 2 items passed all tests:
 | ||
|    1 test in __main__
 | ||
|    6 tests in __main__.factorial
 | ||
| 7 tests in 2 items.
 | ||
| 7 passed.
 | ||
| Test passed.
 | ||
| $
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>That’s all you need to know to start making productive use of <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a>!
 | ||
| Jump in.  The following sections provide full details.  Note that there are many
 | ||
| examples of doctests in the standard Python test suite and libraries.
 | ||
| Especially useful examples can be found in the standard test file
 | ||
| <code class="file docutils literal notranslate"><span class="pre">Lib/test/test_doctest/test_doctest.py</span></code>.</p>
 | ||
| <section id="simple-usage-checking-examples-in-docstrings">
 | ||
| <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>
 | ||
| <p>The simplest way to start using doctest (but not necessarily the way you’ll
 | ||
| continue to do it) is to end each module <code class="xref py py-mod docutils literal notranslate"><span class="pre">M</span></code> with:</p>
 | ||
| <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>
 | ||
|     <span class="kn">import</span><span class="w"> </span><span class="nn">doctest</span>
 | ||
|     <span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">()</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <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>
 | ||
| <p>Running the module as a script causes the examples in the docstrings to get
 | ||
| executed and verified:</p>
 | ||
| <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>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>This won’t display anything unless an example fails, in which case the failing
 | ||
| example(s) and the cause(s) of the failure(s) are printed to stdout, and the
 | ||
| final line of output is <code class="docutils literal notranslate"><span class="pre">***Test</span> <span class="pre">Failed***</span> <span class="pre">N</span> <span class="pre">failures.</span></code>, where <em>N</em> is the
 | ||
| number of examples that failed.</p>
 | ||
| <p>Run it with the <code class="docutils literal notranslate"><span class="pre">-v</span></code> switch instead:</p>
 | ||
| <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>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>and a detailed report of all examples tried is printed to standard output, along
 | ||
| with assorted summaries at the end.</p>
 | ||
| <p>You can force verbose mode by passing <code class="docutils literal notranslate"><span class="pre">verbose=True</span></code> to <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>, or
 | ||
| prohibit it by passing <code class="docutils literal notranslate"><span class="pre">verbose=False</span></code>.  In either of those cases,
 | ||
| <code class="docutils literal notranslate"><span class="pre">sys.argv</span></code> is not examined by <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a> (so passing <code class="docutils literal notranslate"><span class="pre">-v</span></code> or not
 | ||
| has no effect).</p>
 | ||
| <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 section
 | ||
| <a class="reference internal" href="#doctest-cli"><span class="std std-ref">Command-line Usage</span></a>.</p>
 | ||
| <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>
 | ||
| </section>
 | ||
| <section id="simple-usage-checking-examples-in-a-text-file">
 | ||
| <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>
 | ||
| <p>Another simple application of doctest is testing interactive examples in a text
 | ||
| file.  This can be done with the <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a> function:</p>
 | ||
| <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>
 | ||
| <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>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>That short script executes and verifies any interactive Python examples
 | ||
| contained in the file <code class="file docutils literal notranslate"><span class="pre">example.txt</span></code>.  The file content is treated as if it
 | ||
| were a single giant docstring; the file doesn’t need to contain a Python
 | ||
| program!   For example, perhaps <code class="file docutils literal notranslate"><span class="pre">example.txt</span></code> contains this:</p>
 | ||
| <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>The ``example`` module
 | ||
| ======================
 | ||
| 
 | ||
| Using ``factorial``
 | ||
| -------------------
 | ||
| 
 | ||
| This is an example text file in reStructuredText format.  First import
 | ||
| ``factorial`` from the ``example`` module:
 | ||
| 
 | ||
|     >>> from example import factorial
 | ||
| 
 | ||
| Now use it:
 | ||
| 
 | ||
|     >>> factorial(6)
 | ||
|     120
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>Running <code class="docutils literal notranslate"><span class="pre">doctest.testfile("example.txt")</span></code> then finds the error in this
 | ||
| documentation:</p>
 | ||
| <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>
 | ||
| <span class="n">Failed</span> <span class="n">example</span><span class="p">:</span>
 | ||
|     <span class="n">factorial</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
 | ||
| <span class="n">Expected</span><span class="p">:</span>
 | ||
|     <span class="mi">120</span>
 | ||
| <span class="n">Got</span><span class="p">:</span>
 | ||
|     <span class="mi">720</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>As with <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>, <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a> won’t display anything unless an
 | ||
| example fails.  If an example does fail, then the failing example(s) and the
 | ||
| cause(s) of the failure(s) are printed to stdout, using the same format as
 | ||
| <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>.</p>
 | ||
| <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.
 | ||
| See section <a class="reference internal" href="#doctest-basic-api"><span class="std std-ref">Basic API</span></a> for a description of the optional arguments
 | ||
| that can be used to tell it to look for files in other locations.</p>
 | ||
| <p>Like <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>, <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a>’s verbosity can be set with the
 | ||
| <code class="docutils literal notranslate"><span class="pre">-v</span></code> command-line switch or with the optional keyword argument
 | ||
| <em>verbose</em>.</p>
 | ||
| <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 section
 | ||
| <a class="reference internal" href="#doctest-cli"><span class="std std-ref">Command-line Usage</span></a>.</p>
 | ||
| <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>
 | ||
| </section>
 | ||
| <section id="command-line-usage">
 | ||
| <span id="doctest-cli"></span><h2>Command-line Usage<a class="headerlink" href="#command-line-usage" title="Link to this heading">¶</a></h2>
 | ||
| <p>The <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> module can be invoked as a script from the command line:</p>
 | ||
| <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>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <dl class="std option">
 | ||
| <dt class="sig sig-object std" id="cmdoption-doctest-v">
 | ||
| <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>
 | ||
| <dd><p>Detailed report of all examples tried is printed to standard output,
 | ||
| along with assorted summaries at the end:</p>
 | ||
| <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>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>This will import <code class="file docutils literal notranslate"><span class="pre">example.py</span></code> as a standalone module and run
 | ||
| <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a> on it. Note that this may not work correctly if the
 | ||
| file is part of a package and imports other submodules from that package.</p>
 | ||
| <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> infers
 | ||
| that 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>
 | ||
| <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>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="std option">
 | ||
| <dt class="sig sig-object std" id="cmdoption-doctest-o">
 | ||
| <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>
 | ||
| <dd><p>Option flags control various aspects of doctest’s behavior, see section
 | ||
| <a class="reference internal" href="#doctest-options"><span class="std std-ref">Option Flags</span></a>.</p>
 | ||
| <div class="versionadded">
 | ||
| <p><span class="versionmodified added">Added in version 3.4.</span></p>
 | ||
| </div>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="std option">
 | ||
| <dt class="sig sig-object std" id="cmdoption-doctest-f">
 | ||
| <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>
 | ||
| <dd><p>This is shorthand for <code class="docutils literal notranslate"><span class="pre">-o</span> <span class="pre">FAIL_FAST</span></code>.</p>
 | ||
| <div class="versionadded">
 | ||
| <p><span class="versionmodified added">Added in version 3.4.</span></p>
 | ||
| </div>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </section>
 | ||
| <section id="how-it-works">
 | ||
| <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>
 | ||
| <p>This section examines in detail how doctest works: which docstrings it looks at,
 | ||
| how it finds interactive examples, what execution context it uses, how it
 | ||
| handles exceptions, and how option flags can be used to control its behavior.
 | ||
| This is the information that you need to know to write doctest examples; for
 | ||
| information about actually running doctest on these examples, see the following
 | ||
| sections.</p>
 | ||
| <section id="which-docstrings-are-examined">
 | ||
| <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>
 | ||
| <p>The module docstring, and all function, class and method docstrings are
 | ||
| searched.  Objects imported into the module are not searched.</p>
 | ||
| <p>In addition, there are cases when you want tests to be part of a module but not part
 | ||
| of the help text, which requires that the tests not be included in the docstring.
 | ||
| Doctest looks for a module-level variable called <code class="docutils literal notranslate"><span class="pre">__test__</span></code> and uses it to locate other
 | ||
| tests. If <code class="docutils literal notranslate"><span class="pre">M.__test__</span></code> exists, it must be a dict, and each
 | ||
| entry maps a (string) name to a function object, class object, or string.
 | ||
| Function and class object docstrings found from <code class="docutils literal notranslate"><span class="pre">M.__test__</span></code> are searched, and
 | ||
| strings are treated as if they were docstrings.  In output, a key <code class="docutils literal notranslate"><span class="pre">K</span></code> in
 | ||
| <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>
 | ||
| <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>
 | ||
| <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">__test__</span> <span class="o">=</span> <span class="p">{</span>
 | ||
|     <span class="s1">'numbers'</span><span class="p">:</span> <span class="s2">"""</span>
 | ||
| <span class="s2">>>> factorial(6)</span>
 | ||
| <span class="s2">720</span>
 | ||
| 
 | ||
| <span class="s2">>>> [factorial(n) for n in range(6)]</span>
 | ||
| <span class="s2">[1, 1, 2, 6, 24, 120]</span>
 | ||
| <span class="s2">"""</span>
 | ||
| <span class="p">}</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>The value of <code class="docutils literal notranslate"><span class="pre">example.__test__["numbers"]</span></code> will be treated as a
 | ||
| docstring and all the tests inside it will be run. It is
 | ||
| important to note that the value can be mapped to a function,
 | ||
| class object, or module; if so, <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code>
 | ||
| searches them recursively for docstrings, which are then scanned for tests.</p>
 | ||
| <p>Any classes found are recursively searched similarly, to test docstrings in
 | ||
| their contained methods and nested classes.</p>
 | ||
| </section>
 | ||
| <section id="how-are-docstring-examples-recognized">
 | ||
| <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>
 | ||
| <p>In most cases a copy-and-paste of an interactive console session works fine,
 | ||
| but doctest isn’t trying to do an exact emulation of any specific Python shell.</p>
 | ||
| <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="c1"># comments are ignored</span>
 | ||
| <span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="mi">12</span>
 | ||
| <span class="gp">>>> </span><span class="n">x</span>
 | ||
| <span class="go">12</span>
 | ||
| <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>
 | ||
| <span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="s2">"yes"</span><span class="p">)</span>
 | ||
| <span class="gp">... </span><span class="k">else</span><span class="p">:</span>
 | ||
| <span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="s2">"no"</span><span class="p">)</span>
 | ||
| <span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="s2">"NO"</span><span class="p">)</span>
 | ||
| <span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="s2">"NO!!!"</span><span class="p">)</span>
 | ||
| <span class="gp">...</span>
 | ||
| <span class="go">no</span>
 | ||
| <span class="go">NO</span>
 | ||
| <span class="go">NO!!!</span>
 | ||
| <span class="gp">>>></span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <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>
 | ||
| line containing the code, and the expected output (if any) extends to the next
 | ||
| <code class="docutils literal notranslate"><span class="pre">'>>></span> <span class="pre">'</span></code> or all-whitespace line.</p>
 | ||
| <p>The fine print:</p>
 | ||
| <ul>
 | ||
| <li><p>Expected output cannot contain an all-whitespace line, since such a line is
 | ||
| taken to signal the end of expected output.  If expected output does contain a
 | ||
| blank line, put <code class="docutils literal notranslate"><span class="pre"><BLANKLINE></span></code> in your doctest example each place a blank line
 | ||
| is expected.</p></li>
 | ||
| <li><p>All hard tab characters are expanded to spaces, using 8-column tab stops.
 | ||
| Tabs in output generated by the tested code are not modified.  Because any
 | ||
| hard tabs in the sample output <em>are</em> expanded, this means that if the code
 | ||
| output includes hard tabs, the only way the doctest can pass is if the
 | ||
| <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>
 | ||
| is in effect.
 | ||
| Alternatively, the test can be rewritten to capture the output and compare it
 | ||
| to an expected value as part of the test.  This handling of tabs in the
 | ||
| source was arrived at through trial and error, and has proven to be the least
 | ||
| error prone way of handling them.  It is possible to use a different
 | ||
| algorithm for handling tabs by writing a custom <a class="reference internal" href="#doctest.DocTestParser" title="doctest.DocTestParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestParser</span></code></a> class.</p></li>
 | ||
| <li><p>Output to stdout is captured, but not output to stderr (exception tracebacks
 | ||
| are captured via a different means).</p></li>
 | ||
| <li><p>If you continue a line via backslashing in an interactive session, or for any
 | ||
| other reason use a backslash, you should use a raw docstring, which will
 | ||
| preserve your backslashes exactly as you type them:</p>
 | ||
| <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>
 | ||
| <span class="gp">... </span><span class="w">    </span><span class="sa">r</span><span class="sd">'''Backslashes in a raw docstring: m\n'''</span>
 | ||
| <span class="gp">...</span>
 | ||
| <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>
 | ||
| <span class="go">Backslashes in a raw docstring: m\n</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>Otherwise, the backslash will be interpreted as part of the string. For example,
 | ||
| the <code class="docutils literal notranslate"><span class="pre">\n</span></code> above would be interpreted as a newline character.  Alternatively, you
 | ||
| can double each backslash in the doctest version (and not use a raw string):</p>
 | ||
| <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>
 | ||
| <span class="gp">... </span><span class="w">    </span><span class="sd">'''Backslashes in a raw docstring: m\\n'''</span>
 | ||
| <span class="gp">...</span>
 | ||
| <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>
 | ||
| <span class="go">Backslashes in a raw docstring: m\n</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| </li>
 | ||
| <li><p>The starting column doesn’t matter:</p>
 | ||
| <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>
 | ||
| <span class="go">      >>> import math</span>
 | ||
| <span class="go">          >>> math.floor(1.9)</span>
 | ||
| <span class="go">          1</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>and as many leading whitespace characters are stripped from the expected output
 | ||
| as appeared in the initial <code class="docutils literal notranslate"><span class="pre">'>>></span> <span class="pre">'</span></code> line that started the example.</p>
 | ||
| </li>
 | ||
| </ul>
 | ||
| </section>
 | ||
| <section id="what-s-the-execution-context">
 | ||
| <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>
 | ||
| <p>By default, each time <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> finds a docstring to test, it uses a
 | ||
| <em>shallow copy</em> of <code class="xref py py-mod docutils literal notranslate"><span class="pre">M</span></code>’s globals, so that running tests doesn’t change the
 | ||
| module’s real globals, and so that one test in <code class="xref py py-mod docutils literal notranslate"><span class="pre">M</span></code> can’t leave behind
 | ||
| crumbs that accidentally allow another test to work.  This means examples can
 | ||
| freely use any names defined at top-level in <code class="xref py py-mod docutils literal notranslate"><span class="pre">M</span></code>, and names defined earlier
 | ||
| in the docstring being run. Examples cannot see names defined in other
 | ||
| docstrings.</p>
 | ||
| <p>You can force use of your own dict as the execution context by passing
 | ||
| <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>
 | ||
| </section>
 | ||
| <section id="what-about-exceptions">
 | ||
| <span id="doctest-exceptions"></span><h3>What About Exceptions?<a class="headerlink" href="#what-about-exceptions" title="Link to this heading">¶</a></h3>
 | ||
| <p>No problem, provided that the traceback is the only output produced by the
 | ||
| example:  just paste in the traceback. <a class="footnote-reference brackets" href="#id2" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> Since tracebacks contain details
 | ||
| that are likely to change rapidly (for example, exact file paths and line
 | ||
| numbers), this is one case where doctest works hard to be flexible in what it
 | ||
| accepts.</p>
 | ||
| <p>Simple example:</p>
 | ||
| <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>
 | ||
| <span class="gt">Traceback (most recent call last):</span>
 | ||
|   File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
 | ||
| <span class="gr">ValueError</span>: <span class="n">list.remove(x): x not in list</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <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>
 | ||
| <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>
 | ||
| <p>The expected output for an exception must start with a traceback header, which
 | ||
| may be either of the following two lines, indented the same as the first line of
 | ||
| the example:</p>
 | ||
| <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>
 | ||
| <span class="n">Traceback</span> <span class="p">(</span><span class="n">innermost</span> <span class="n">last</span><span class="p">):</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>The traceback header is followed by an optional traceback stack, whose contents
 | ||
| are ignored by doctest.  The traceback stack is typically omitted, or copied
 | ||
| verbatim from an interactive session.</p>
 | ||
| <p>The traceback stack is followed by the most interesting part: the line(s)
 | ||
| containing the exception type and detail.  This is usually the last line of a
 | ||
| traceback, but can extend across multiple lines if the exception has a
 | ||
| multi-line detail:</p>
 | ||
| <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>
 | ||
| <span class="gt">Traceback (most recent call last):</span>
 | ||
|   File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
 | ||
| <span class="gr">ValueError</span>: <span class="n">multi</span>
 | ||
| <span class="x">    line</span>
 | ||
| <span class="x">detail</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>The last three lines (starting with <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>) are compared against the
 | ||
| exception’s type and detail, and the rest are ignored.</p>
 | ||
| <p>Best practice is to omit the traceback stack, unless it adds significant
 | ||
| documentation value to the example.  So the last example is probably better as:</p>
 | ||
| <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>
 | ||
| <span class="gt">Traceback (most recent call last):</span>
 | ||
| <span class="w">    </span><span class="o">...</span>
 | ||
| <span class="gr">ValueError</span>: <span class="n">multi</span>
 | ||
| <span class="x">    line</span>
 | ||
| <span class="x">detail</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>Note that tracebacks are treated very specially.  In particular, in the
 | ||
| rewritten example, the use of <code class="docutils literal notranslate"><span class="pre">...</span></code> is independent of doctest’s
 | ||
| <a class="reference internal" href="#doctest.ELLIPSIS" title="doctest.ELLIPSIS"><code class="xref py py-const docutils literal notranslate"><span class="pre">ELLIPSIS</span></code></a> option.  The ellipsis in that example could be left out, or
 | ||
| could just as well be three (or three hundred) commas or digits, or an indented
 | ||
| transcript of a Monty Python skit.</p>
 | ||
| <p>Some details you should read once, but won’t need to remember:</p>
 | ||
| <ul class="simple">
 | ||
| <li><p>Doctest can’t guess whether your expected output came from an exception
 | ||
| traceback or from ordinary printing.  So, e.g., an example that expects
 | ||
| <code class="docutils literal notranslate"><span class="pre">ValueError:</span> <span class="pre">42</span> <span class="pre">is</span> <span class="pre">prime</span></code> will pass whether <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is actually
 | ||
| raised or if the example merely prints that traceback text.  In practice,
 | ||
| ordinary output rarely begins with a traceback header line, so this doesn’t
 | ||
| create real problems.</p></li>
 | ||
| <li><p>Each line of the traceback stack (if present) must be indented further than
 | ||
| the first line of the example, <em>or</em> start with a non-alphanumeric character.
 | ||
| The first line following the traceback header indented the same and starting
 | ||
| with an alphanumeric is taken to be the start of the exception detail.  Of
 | ||
| course this does the right thing for genuine tracebacks.</p></li>
 | ||
| <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,
 | ||
| everything following the leftmost colon and any module information in the
 | ||
| exception name is ignored.</p></li>
 | ||
| <li><p>The interactive shell omits the traceback header line for some
 | ||
| <a class="reference internal" href="exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a>s.  But doctest uses the traceback header line to
 | ||
| distinguish exceptions from non-exceptions.  So in the rare case where you need
 | ||
| to test a <a class="reference internal" href="exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> that omits the traceback header, you will need to
 | ||
| manually add the traceback header line to your test example.</p></li>
 | ||
| </ul>
 | ||
| <ul id="index-1">
 | ||
| <li><p>For some exceptions, Python displays the position of the error using <code class="docutils literal notranslate"><span class="pre">^</span></code>
 | ||
| markers and tildes:</p>
 | ||
| <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>
 | ||
|   File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
 | ||
| <span class="w">    </span><span class="mi">1</span> <span class="o">+</span> <span class="kc">None</span>
 | ||
| <span class="w">    </span><span class="pm">~~^~~~~~</span>
 | ||
| <span class="gr">TypeError</span>: <span class="n">unsupported operand type(s) for +: 'int' and 'NoneType'</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>Since the lines showing the position of the error come before the exception type
 | ||
| and detail, they are not checked by doctest.  For example, the following test
 | ||
| would pass, even though it puts the <code class="docutils literal notranslate"><span class="pre">^</span></code> marker in the wrong location:</p>
 | ||
| <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>
 | ||
|   File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
 | ||
| <span class="w">    </span><span class="mi">1</span> <span class="o">+</span> <span class="kc">None</span>
 | ||
| <span class="w">    </span><span class="pm">^~~~~~~~</span>
 | ||
| <span class="gr">TypeError</span>: <span class="n">unsupported operand type(s) for +: 'int' and 'NoneType'</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| </li>
 | ||
| </ul>
 | ||
| </section>
 | ||
| <section id="option-flags">
 | ||
| <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>
 | ||
| <p>A number of option flags control various aspects of doctest’s behavior.
 | ||
| Symbolic names for the flags are supplied as module constants, which can be
 | ||
| <a class="reference internal" href="../reference/expressions.html#bitwise"><span class="std std-ref">bitwise ORed</span></a> together and passed to various functions.
 | ||
| The names can also be used in <a class="reference internal" href="#doctest-directives"><span class="std std-ref">doctest directives</span></a>,
 | ||
| and may be passed to the doctest command line interface via the <code class="docutils literal notranslate"><span class="pre">-o</span></code> option.</p>
 | ||
| <p>The first group of options define test semantics, controlling aspects of how
 | ||
| doctest decides whether actual output matches an example’s expected output:</p>
 | ||
| <dl class="py data">
 | ||
| <dt class="sig sig-object py" id="doctest.DONT_ACCEPT_TRUE_FOR_1">
 | ||
| <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>
 | ||
| <dd><p>By default, if an expected output block contains just <code class="docutils literal notranslate"><span class="pre">1</span></code>, an actual output
 | ||
| block containing just <code class="docutils literal notranslate"><span class="pre">1</span></code> or just <code class="docutils literal notranslate"><span class="pre">True</span></code> is considered to be a match, and
 | ||
| similarly for <code class="docutils literal notranslate"><span class="pre">0</span></code> versus <code class="docutils literal notranslate"><span class="pre">False</span></code>.  When <a class="reference internal" href="#doctest.DONT_ACCEPT_TRUE_FOR_1" title="doctest.DONT_ACCEPT_TRUE_FOR_1"><code class="xref py py-const docutils literal notranslate"><span class="pre">DONT_ACCEPT_TRUE_FOR_1</span></code></a> is
 | ||
| specified, neither substitution is allowed.  The default behavior caters to that
 | ||
| Python changed the return type of many functions from integer to boolean;
 | ||
| doctests expecting “little integer” output still work in these cases.  This
 | ||
| option will probably go away, but not for several years.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py data" id="index-2">
 | ||
| <dt class="sig sig-object py" id="doctest.DONT_ACCEPT_BLANKLINE">
 | ||
| <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>
 | ||
| <dd><p>By default, if an expected output block contains a line containing only the
 | ||
| string <code class="docutils literal notranslate"><span class="pre"><BLANKLINE></span></code>, then that line will match a blank line in the actual
 | ||
| output.  Because a genuinely blank line delimits the expected output, this is
 | ||
| the only way to communicate that a blank line is expected.  When
 | ||
| <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>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py data">
 | ||
| <dt class="sig sig-object py" id="doctest.NORMALIZE_WHITESPACE">
 | ||
| <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>
 | ||
| <dd><p>When specified, all sequences of whitespace (blanks and newlines) are treated as
 | ||
| equal.  Any sequence of whitespace within the expected output will match any
 | ||
| sequence of whitespace within the actual output. By default, whitespace must
 | ||
| match exactly. <a class="reference internal" href="#doctest.NORMALIZE_WHITESPACE" title="doctest.NORMALIZE_WHITESPACE"><code class="xref py py-const docutils literal notranslate"><span class="pre">NORMALIZE_WHITESPACE</span></code></a> is especially useful when a line of
 | ||
| expected output is very long, and you want to wrap it across multiple lines in
 | ||
| your source.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py data" id="index-3">
 | ||
| <dt class="sig sig-object py" id="doctest.ELLIPSIS">
 | ||
| <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>
 | ||
| <dd><p>When specified, an ellipsis marker (<code class="docutils literal notranslate"><span class="pre">...</span></code>) in the expected output can match
 | ||
| any substring in the actual output.  This includes substrings that span line
 | ||
| boundaries, and empty substrings, so it’s best to keep usage of this simple.
 | ||
| Complicated uses can lead to the same kinds of “oops, it matched too much!”
 | ||
| surprises that <code class="docutils literal notranslate"><span class="pre">.*</span></code> is prone to in regular expressions.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py data">
 | ||
| <dt class="sig sig-object py" id="doctest.IGNORE_EXCEPTION_DETAIL">
 | ||
| <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>
 | ||
| <dd><p>When specified, doctests expecting exceptions pass so long as an exception
 | ||
| of the expected type is raised, even if the details
 | ||
| (message and fully qualified exception name) don’t match.</p>
 | ||
| <p>For example, an example expecting <code class="docutils literal notranslate"><span class="pre">ValueError:</span> <span class="pre">42</span></code> will pass if the actual
 | ||
| exception raised is <code class="docutils literal notranslate"><span class="pre">ValueError:</span> <span class="pre">3*14</span></code>, but will fail if, say, a
 | ||
| <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.
 | ||
| It will also ignore any fully qualified name included before the
 | ||
| exception class, which can vary between implementations and versions
 | ||
| of Python and the code/libraries in use.
 | ||
| Hence, all three of these variations will work with the flag specified:</p>
 | ||
| <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>
 | ||
| <span class="gt">Traceback (most recent call last):</span>
 | ||
| <span class="gr">Exception</span>: <span class="n">message</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>
 | ||
| <span class="gt">Traceback (most recent call last):</span>
 | ||
| <span class="gr">builtins.Exception</span>: <span class="n">message</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>
 | ||
| <span class="gt">Traceback (most recent call last):</span>
 | ||
| <span class="gr">__main__.Exception</span>: <span class="n">message</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>Note that <a class="reference internal" href="#doctest.ELLIPSIS" title="doctest.ELLIPSIS"><code class="xref py py-const docutils literal notranslate"><span class="pre">ELLIPSIS</span></code></a> can also be used to ignore the
 | ||
| details of the exception message, but such a test may still fail based
 | ||
| on whether the module name is present or matches exactly.</p>
 | ||
| <div class="versionchanged">
 | ||
| <p><span class="versionmodified changed">Changed in version 3.2: </span><a class="reference internal" href="#doctest.IGNORE_EXCEPTION_DETAIL" title="doctest.IGNORE_EXCEPTION_DETAIL"><code class="xref py py-const docutils literal notranslate"><span class="pre">IGNORE_EXCEPTION_DETAIL</span></code></a> now also ignores any information relating
 | ||
| to the module containing the exception under test.</p>
 | ||
| </div>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py data">
 | ||
| <dt class="sig sig-object py" id="doctest.SKIP">
 | ||
| <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>
 | ||
| <dd><p>When specified, do not run the example at all.  This can be useful in contexts
 | ||
| where doctest examples serve as both documentation and test cases, and an
 | ||
| example should be included for documentation purposes, but should not be
 | ||
| checked.  E.g., the example’s output might be random; or the example might
 | ||
| depend on resources which would be unavailable to the test driver.</p>
 | ||
| <p>The SKIP flag can also be used for temporarily “commenting out” examples.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py data">
 | ||
| <dt class="sig sig-object py" id="doctest.COMPARISON_FLAGS">
 | ||
| <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>
 | ||
| <dd><p>A bitmask or’ing together all the comparison flags above.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <p>The second group of options controls how test failures are reported:</p>
 | ||
| <dl class="py data">
 | ||
| <dt class="sig sig-object py" id="doctest.REPORT_UDIFF">
 | ||
| <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>
 | ||
| <dd><p>When specified, failures that involve multi-line expected and actual outputs are
 | ||
| displayed using a unified diff.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py data">
 | ||
| <dt class="sig sig-object py" id="doctest.REPORT_CDIFF">
 | ||
| <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>
 | ||
| <dd><p>When specified, failures that involve multi-line expected and actual outputs
 | ||
| will be displayed using a context diff.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py data">
 | ||
| <dt class="sig sig-object py" id="doctest.REPORT_NDIFF">
 | ||
| <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>
 | ||
| <dd><p>When specified, differences are computed by <code class="docutils literal notranslate"><span class="pre">difflib.Differ</span></code>, using the same
 | ||
| algorithm as the popular <code class="file docutils literal notranslate"><span class="pre">ndiff.py</span></code> utility. This is the only method that
 | ||
| marks differences within lines as well as across lines.  For example, if a line
 | ||
| of expected output contains digit <code class="docutils literal notranslate"><span class="pre">1</span></code> where actual output contains letter
 | ||
| <code class="docutils literal notranslate"><span class="pre">l</span></code>, a line is inserted with a caret marking the mismatching column positions.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py data">
 | ||
| <dt class="sig sig-object py" id="doctest.REPORT_ONLY_FIRST_FAILURE">
 | ||
| <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>
 | ||
| <dd><p>When specified, display the first failing example in each doctest, but suppress
 | ||
| output for all remaining examples.  This will prevent doctest from reporting
 | ||
| correct examples that break because of earlier failures; but it might also hide
 | ||
| incorrect examples that fail independently of the first failure.  When
 | ||
| <a class="reference internal" href="#doctest.REPORT_ONLY_FIRST_FAILURE" title="doctest.REPORT_ONLY_FIRST_FAILURE"><code class="xref py py-const docutils literal notranslate"><span class="pre">REPORT_ONLY_FIRST_FAILURE</span></code></a> is specified, the remaining examples are
 | ||
| still run, and still count towards the total number of failures reported; only
 | ||
| the output is suppressed.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py data">
 | ||
| <dt class="sig sig-object py" id="doctest.FAIL_FAST">
 | ||
| <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>
 | ||
| <dd><p>When specified, exit after the first failing example and don’t attempt to run
 | ||
| the remaining examples. Thus, the number of failures reported will be at most
 | ||
| 1.  This flag may be useful during debugging, since examples after the first
 | ||
| failure won’t even produce debugging output.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py data">
 | ||
| <dt class="sig sig-object py" id="doctest.REPORTING_FLAGS">
 | ||
| <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>
 | ||
| <dd><p>A bitmask or’ing together all the reporting flags above.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <p>There is also a way to register new option flag names, though this isn’t
 | ||
| useful unless you intend to extend <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> internals via subclassing:</p>
 | ||
| <dl class="py function">
 | ||
| <dt class="sig sig-object py" id="doctest.register_optionflag">
 | ||
| <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>
 | ||
| <dd><p>Create a new option flag with a given name, and return the new flag’s integer
 | ||
| value.  <a class="reference internal" href="#doctest.register_optionflag" title="doctest.register_optionflag"><code class="xref py py-func docutils literal notranslate"><span class="pre">register_optionflag()</span></code></a> can be used when subclassing
 | ||
| <a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a> or <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> to create new options that are
 | ||
| supported by your subclasses.  <a class="reference internal" href="#doctest.register_optionflag" title="doctest.register_optionflag"><code class="xref py py-func docutils literal notranslate"><span class="pre">register_optionflag()</span></code></a> should always be
 | ||
| called using the following idiom:</p>
 | ||
| <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>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </section>
 | ||
| <section id="directives">
 | ||
| <span id="doctest-directives"></span><span id="index-4"></span><h3>Directives<a class="headerlink" href="#directives" title="Link to this heading">¶</a></h3>
 | ||
| <p>Doctest directives may be used to modify the <a class="reference internal" href="#doctest-options"><span class="std std-ref">option flags</span></a> for an individual example.  Doctest directives are
 | ||
| special Python comments following an example’s source code:</p>
 | ||
| <pre>
 | ||
| <strong id="grammar-token-doctest-directive">directive</strong>             ::= "#" "doctest:" <a class="reference internal" href="#grammar-token-doctest-directive_options"><code class="xref docutils literal notranslate"><span class="pre">directive_options</span></code></a>
 | ||
| <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> ("," <a class="reference internal" href="#grammar-token-doctest-directive_option"><code class="xref docutils literal notranslate"><span class="pre">directive_option</span></code></a>)*
 | ||
| <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>
 | ||
| <strong id="grammar-token-doctest-on_or_off">on_or_off</strong>             ::= "+" | "-"
 | ||
| <strong id="grammar-token-doctest-directive_option_name">directive_option_name</strong> ::= "DONT_ACCEPT_BLANKLINE" | "NORMALIZE_WHITESPACE" | ...
 | ||
| </pre>
 | ||
| <p>Whitespace is not allowed between the <code class="docutils literal notranslate"><span class="pre">+</span></code> or <code class="docutils literal notranslate"><span class="pre">-</span></code> and the directive option
 | ||
| name.  The directive option name can be any of the option flag names explained
 | ||
| above.</p>
 | ||
| <p>An example’s doctest directives modify doctest’s behavior for that single
 | ||
| example.  Use <code class="docutils literal notranslate"><span class="pre">+</span></code> to enable the named behavior, or <code class="docutils literal notranslate"><span class="pre">-</span></code> to disable it.</p>
 | ||
| <p>For example, this test passes:</p>
 | ||
| <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>
 | ||
| <span class="go">[0,   1,  2,  3,  4,  5,  6,  7,  8,  9,</span>
 | ||
| <span class="go">10,  11, 12, 13, 14, 15, 16, 17, 18, 19]</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>Without the directive it would fail, both because the actual output doesn’t have
 | ||
| two blanks before the single-digit list elements, and because the actual output
 | ||
| is on a single line.  This test also passes, and also requires a directive to do
 | ||
| so:</p>
 | ||
| <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>
 | ||
| <span class="go">[0, 1, ..., 18, 19]</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>Multiple directives can be used on a single physical line, separated by
 | ||
| commas:</p>
 | ||
| <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>
 | ||
| <span class="go">[0,    1, ...,   18,    19]</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>If multiple directive comments are used for a single example, then they are
 | ||
| combined:</p>
 | ||
| <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>
 | ||
| <span class="gp">... </span>                        <span class="c1"># doctest: +NORMALIZE_WHITESPACE</span>
 | ||
| <span class="go">[0,    1, ...,   18,    19]</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>As the previous example shows, you can add <code class="docutils literal notranslate"><span class="pre">...</span></code> lines to your example
 | ||
| containing only directives.  This can be useful when an example is too long for
 | ||
| a directive to comfortably fit on the same line:</p>
 | ||
| <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>
 | ||
| <span class="gp">... </span><span class="c1"># doctest: +ELLIPSIS</span>
 | ||
| <span class="go">[0, ..., 4, 10, ..., 19, 30, ..., 39]</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>Note that since all options are disabled by default, and directives apply only
 | ||
| to the example they appear in, enabling options (via <code class="docutils literal notranslate"><span class="pre">+</span></code> in a directive) is
 | ||
| usually the only meaningful choice.  However, option flags can also be passed to
 | ||
| functions that run doctests, establishing different defaults.  In such cases,
 | ||
| disabling an option via <code class="docutils literal notranslate"><span class="pre">-</span></code> in a directive can be useful.</p>
 | ||
| </section>
 | ||
| <section id="warnings">
 | ||
| <span id="doctest-warnings"></span><h3>Warnings<a class="headerlink" href="#warnings" title="Link to this heading">¶</a></h3>
 | ||
| <p><a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> is serious about requiring exact matches in expected output.  If
 | ||
| even a single character doesn’t match, the test fails.  This will probably
 | ||
| surprise you a few times, as you learn exactly what Python does and doesn’t
 | ||
| guarantee about output.  For example, when printing a set, Python doesn’t
 | ||
| guarantee that the element is printed in any particular order, so a test like</p>
 | ||
| <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="go">{"spam", "eggs"}</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>is vulnerable!  One workaround is to do</p>
 | ||
| <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>
 | ||
| <span class="go">True</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>instead.  Another is to do</p>
 | ||
| <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>
 | ||
| <span class="gp">>>> </span><span class="n">d</span>
 | ||
| <span class="go">['eggs', 'spam']</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>There are others, but you get the idea.</p>
 | ||
| <p>Another bad idea is to print things that embed an object address, like</p>
 | ||
| <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>
 | ||
| <span class="go">7948648</span>
 | ||
| <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>
 | ||
| <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>
 | ||
| <span class="go"><C object at 0x00AC18F0></span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <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>
 | ||
| <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>
 | ||
| <span class="go"><C object at 0x...></span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>Floating-point numbers are also subject to small output variations across
 | ||
| platforms, because Python defers to the platform C library for float formatting,
 | ||
| and C libraries vary widely in quality here.</p>
 | ||
| <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mf">1.</span><span class="o">/</span><span class="mi">7</span>  <span class="c1"># risky</span>
 | ||
| <span class="go">0.14285714285714285</span>
 | ||
| <span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="mi">7</span><span class="p">)</span> <span class="c1"># safer</span>
 | ||
| <span class="go">0.142857142857</span>
 | ||
| <span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="mi">7</span><span class="p">,</span> <span class="mi">6</span><span class="p">))</span> <span class="c1"># much safer</span>
 | ||
| <span class="go">0.142857</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>Numbers of the form <code class="docutils literal notranslate"><span class="pre">I/2.**J</span></code> are safe across all platforms, and I often
 | ||
| contrive doctest examples to produce numbers of that form:</p>
 | ||
| <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>
 | ||
| <span class="go">0.75</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>Simple fractions are also easier for people to understand, and that makes for
 | ||
| better documentation.</p>
 | ||
| </section>
 | ||
| </section>
 | ||
| <section id="basic-api">
 | ||
| <span id="doctest-basic-api"></span><h2>Basic API<a class="headerlink" href="#basic-api" title="Link to this heading">¶</a></h2>
 | ||
| <p>The functions <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a> and <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a> provide a simple interface to
 | ||
| doctest that should be sufficient for most basic uses.  For a less formal
 | ||
| introduction to these two functions, see sections <a class="reference internal" href="#doctest-simple-testmod"><span class="std std-ref">Simple Usage: Checking Examples in Docstrings</span></a>
 | ||
| and <a class="reference internal" href="#doctest-simple-testfile"><span class="std std-ref">Simple Usage: Checking Examples in a Text File</span></a>.</p>
 | ||
| <dl class="py function">
 | ||
| <dt class="sig sig-object py" id="doctest.testfile">
 | ||
| <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>
 | ||
| <dd><p>All arguments except <em>filename</em> are optional, and should be specified in keyword
 | ||
| form.</p>
 | ||
| <p>Test examples in the file named <em>filename</em>.  Return <code class="docutils literal notranslate"><span class="pre">(failure_count,</span>
 | ||
| <span class="pre">test_count)</span></code>.</p>
 | ||
| <p>Optional argument <em>module_relative</em> specifies how the filename should be
 | ||
| interpreted:</p>
 | ||
| <ul class="simple">
 | ||
| <li><p>If <em>module_relative</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default), then <em>filename</em> specifies an
 | ||
| OS-independent module-relative path.  By default, this path is relative to the
 | ||
| calling module’s directory; but if the <em>package</em> argument is specified, then it
 | ||
| is relative to that package.  To ensure OS-independence, <em>filename</em> should use
 | ||
| <code class="docutils literal notranslate"><span class="pre">/</span></code> characters to separate path segments, and may not be an absolute path
 | ||
| (i.e., it may not begin with <code class="docutils literal notranslate"><span class="pre">/</span></code>).</p></li>
 | ||
| <li><p>If <em>module_relative</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, then <em>filename</em> specifies an OS-specific
 | ||
| path.  The path may be absolute or relative; relative paths are resolved with
 | ||
| respect to the current working directory.</p></li>
 | ||
| </ul>
 | ||
| <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>,
 | ||
| <code class="docutils literal notranslate"><span class="pre">os.path.basename(filename)</span></code> is used.</p>
 | ||
| <p>Optional argument <em>package</em> is a Python package or the name of a Python package
 | ||
| whose directory should be used as the base directory for a module-relative
 | ||
| filename.  If no package is specified, then the calling module’s directory is
 | ||
| used as the base directory for module-relative filenames.  It is an error to
 | ||
| specify <em>package</em> if <em>module_relative</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
 | ||
| <p>Optional argument <em>globs</em> gives a dict to be used as the globals when executing
 | ||
| examples.  A new shallow copy of this dict is created for the doctest, so its
 | ||
| examples start with a clean slate. By default, or if <code class="docutils literal notranslate"><span class="pre">None</span></code>, a new empty dict
 | ||
| is used.</p>
 | ||
| <p>Optional argument <em>extraglobs</em> gives a dict merged into the globals used to
 | ||
| execute examples.  This works like <a class="reference internal" href="stdtypes.html#dict.update" title="dict.update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.update()</span></code></a>:  if <em>globs</em> and
 | ||
| <em>extraglobs</em> have a common key, the associated value in <em>extraglobs</em> appears in
 | ||
| the combined dict.  By default, or if <code class="docutils literal notranslate"><span class="pre">None</span></code>, no extra globals are used.  This
 | ||
| is an advanced feature that allows parameterization of doctests.  For example, a
 | ||
| doctest can be written for a base class, using a generic name for the class,
 | ||
| then reused to test any number of subclasses by passing an <em>extraglobs</em> dict
 | ||
| mapping the generic name to the subclass to be tested.</p>
 | ||
| <p>Optional argument <em>verbose</em> prints lots of stuff if true, and prints only
 | ||
| failures if false; by default, or if <code class="docutils literal notranslate"><span class="pre">None</span></code>, it’s true if and only if <code class="docutils literal notranslate"><span class="pre">'-v'</span></code>
 | ||
| is in <code class="docutils literal notranslate"><span class="pre">sys.argv</span></code>.</p>
 | ||
| <p>Optional argument <em>report</em> prints a summary at the end when true, else prints
 | ||
| nothing at the end.  In verbose mode, the summary is detailed, else the summary
 | ||
| is very brief (in fact, empty if all tests passed).</p>
 | ||
| <p>Optional argument <em>optionflags</em> (default value 0) takes the
 | ||
| <a class="reference internal" href="../reference/expressions.html#bitwise"><span class="std std-ref">bitwise OR</span></a> of option flags.
 | ||
| See section <a class="reference internal" href="#doctest-options"><span class="std std-ref">Option Flags</span></a>.</p>
 | ||
| <p>Optional argument <em>raise_on_error</em> defaults to false.  If true, an exception is
 | ||
| raised upon the first failure or unexpected exception in an example.  This
 | ||
| allows failures to be post-mortem debugged. Default behavior is to continue
 | ||
| running examples.</p>
 | ||
| <p>Optional argument <em>parser</em> specifies a <a class="reference internal" href="#doctest.DocTestParser" title="doctest.DocTestParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestParser</span></code></a> (or subclass) that
 | ||
| should be used to extract tests from the files.  It defaults to a normal parser
 | ||
| (i.e., <code class="docutils literal notranslate"><span class="pre">DocTestParser()</span></code>).</p>
 | ||
| <p>Optional argument <em>encoding</em> specifies an encoding that should be used to
 | ||
| convert the file to unicode.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py function">
 | ||
| <dt class="sig sig-object py" id="doctest.testmod">
 | ||
| <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>
 | ||
| <dd><p>All arguments are optional, and all except for <em>m</em> should be specified in
 | ||
| keyword form.</p>
 | ||
| <p>Test examples in docstrings in functions and classes reachable from module <em>m</em>
 | ||
| (or module <a class="reference internal" href="__main__.html#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> if <em>m</em> is not supplied or is <code class="docutils literal notranslate"><span class="pre">None</span></code>), starting with
 | ||
| <code class="docutils literal notranslate"><span class="pre">m.__doc__</span></code>.</p>
 | ||
| <p>Also test examples reachable from dict <code class="docutils literal notranslate"><span class="pre">m.__test__</span></code>, if it exists.
 | ||
| <code class="docutils literal notranslate"><span class="pre">m.__test__</span></code> maps names (strings) to functions, classes and
 | ||
| strings; function and class docstrings are searched for examples; strings are
 | ||
| searched directly, as if they were docstrings.</p>
 | ||
| <p>Only docstrings attached to objects belonging to module <em>m</em> are searched.</p>
 | ||
| <p>Return <code class="docutils literal notranslate"><span class="pre">(failure_count,</span> <span class="pre">test_count)</span></code>.</p>
 | ||
| <p>Optional argument <em>name</em> gives the name of the module; by default, or if
 | ||
| <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>
 | ||
| <p>Optional argument <em>exclude_empty</em> defaults to false.  If true, objects for which
 | ||
| no doctests are found are excluded from consideration. The default is a backward
 | ||
| compatibility hack, so that code still using
 | ||
| <a class="reference internal" href="#doctest.DocTestRunner.summarize" title="doctest.DocTestRunner.summarize"><code class="xref py py-meth docutils literal notranslate"><span class="pre">doctest.master.summarize</span></code></a> in
 | ||
| conjunction with <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a> continues to get output for objects with no
 | ||
| tests. The <em>exclude_empty</em> argument to the newer <a class="reference internal" href="#doctest.DocTestFinder" title="doctest.DocTestFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestFinder</span></code></a>
 | ||
| constructor defaults to true.</p>
 | ||
| <p>Optional arguments <em>extraglobs</em>, <em>verbose</em>, <em>report</em>, <em>optionflags</em>,
 | ||
| <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>
 | ||
| above, except that <em>globs</em> defaults to <code class="docutils literal notranslate"><span class="pre">m.__dict__</span></code>.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py function">
 | ||
| <dt class="sig sig-object py" id="doctest.run_docstring_examples">
 | ||
| <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>
 | ||
| <dd><p>Test examples associated with object <em>f</em>; for example, <em>f</em> may be a string,
 | ||
| a module, a function, or a class object.</p>
 | ||
| <p>A shallow copy of dictionary argument <em>globs</em> is used for the execution context.</p>
 | ||
| <p>Optional argument <em>name</em> is used in failure messages, and defaults to
 | ||
| <code class="docutils literal notranslate"><span class="pre">"NoName"</span></code>.</p>
 | ||
| <p>If optional argument <em>verbose</em> is true, output is generated even if there are no
 | ||
| failures.  By default, output is generated only in case of an example failure.</p>
 | ||
| <p>Optional argument <em>compileflags</em> gives the set of flags that should be used by
 | ||
| the Python compiler when running the examples.  By default, or if <code class="docutils literal notranslate"><span class="pre">None</span></code>,
 | ||
| flags are deduced corresponding to the set of future features found in <em>globs</em>.</p>
 | ||
| <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>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </section>
 | ||
| <section id="unittest-api">
 | ||
| <span id="doctest-unittest-api"></span><h2>Unittest API<a class="headerlink" href="#unittest-api" title="Link to this heading">¶</a></h2>
 | ||
| <p>As your collection of doctest’ed modules grows, you’ll want a way to run all
 | ||
| their doctests systematically.  <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> provides two functions that can
 | ||
| be used to create <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> test suites from modules and text files
 | ||
| containing doctests.  To integrate with <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> test discovery, include
 | ||
| a <a class="reference internal" href="unittest.html#load-tests-protocol"><span class="std std-ref">load_tests</span></a> function in your test module:</p>
 | ||
| <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>
 | ||
| <span class="kn">import</span><span class="w"> </span><span class="nn">doctest</span>
 | ||
| <span class="kn">import</span><span class="w"> </span><span class="nn">my_module_with_doctests</span>
 | ||
| 
 | ||
| <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>
 | ||
|     <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>
 | ||
|     <span class="k">return</span> <span class="n">tests</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>There are two main functions for creating <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a> instances
 | ||
| from text files and modules with doctests:</p>
 | ||
| <dl class="py function">
 | ||
| <dt class="sig sig-object py" id="doctest.DocFileSuite">
 | ||
| <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>
 | ||
| <dd><p>Convert doctest tests from one or more text files to a
 | ||
| <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a>.</p>
 | ||
| <p>The returned <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a> is to be run by the unittest framework
 | ||
| and runs the interactive examples in each file.  If an example in any file
 | ||
| fails, then the synthesized unit test fails, and a <a class="reference internal" href="#doctest.failureException" title="doctest.failureException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">failureException</span></code></a>
 | ||
| exception is raised showing the name of the file containing the test and a
 | ||
| (sometimes approximate) line number.  If all the examples in a file are
 | ||
| skipped, then the synthesized unit test is also marked as skipped.</p>
 | ||
| <p>Pass one or more paths (as strings) to text files to be examined.</p>
 | ||
| <p>Options may be provided as keyword arguments:</p>
 | ||
| <p>Optional argument <em>module_relative</em> specifies how the filenames in <em>paths</em>
 | ||
| should be interpreted:</p>
 | ||
| <ul class="simple">
 | ||
| <li><p>If <em>module_relative</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default), then each filename in
 | ||
| <em>paths</em> specifies an OS-independent module-relative path.  By default, this
 | ||
| path is relative to the calling module’s directory; but if the <em>package</em>
 | ||
| argument is specified, then it is relative to that package.  To ensure
 | ||
| OS-independence, each filename should use <code class="docutils literal notranslate"><span class="pre">/</span></code> characters to separate path
 | ||
| segments, and may not be an absolute path (i.e., it may not begin with
 | ||
| <code class="docutils literal notranslate"><span class="pre">/</span></code>).</p></li>
 | ||
| <li><p>If <em>module_relative</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, then each filename in <em>paths</em> specifies
 | ||
| an OS-specific path.  The path may be absolute or relative; relative paths
 | ||
| are resolved with respect to the current working directory.</p></li>
 | ||
| </ul>
 | ||
| <p>Optional argument <em>package</em> is a Python package or the name of a Python
 | ||
| package whose directory should be used as the base directory for
 | ||
| module-relative filenames in <em>paths</em>.  If no package is specified, then the
 | ||
| calling module’s directory is used as the base directory for module-relative
 | ||
| filenames.  It is an error to specify <em>package</em> if <em>module_relative</em> is
 | ||
| <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
 | ||
| <p>Optional argument <em>setUp</em> specifies a set-up function for the test suite.
 | ||
| This is called before running the tests in each file.  The <em>setUp</em> function
 | ||
| will be passed a <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object.  The setUp function can access the
 | ||
| test globals as the <em>globs</em> attribute of the test passed.</p>
 | ||
| <p>Optional argument <em>tearDown</em> specifies a tear-down function for the test
 | ||
| suite.  This is called after running the tests in each file.  The <em>tearDown</em>
 | ||
| function will be passed a <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object.  The setUp function can
 | ||
| access the test globals as the <em>globs</em> attribute of the test passed.</p>
 | ||
| <p>Optional argument <em>globs</em> is a dictionary containing the initial global
 | ||
| variables for the tests.  A new copy of this dictionary is created for each
 | ||
| test.  By default, <em>globs</em> is a new empty dictionary.</p>
 | ||
| <p>Optional argument <em>optionflags</em> specifies the default doctest options for the
 | ||
| tests, created by or-ing together individual option flags.  See section
 | ||
| <a class="reference internal" href="#doctest-options"><span class="std std-ref">Option Flags</span></a>. See function <a class="reference internal" href="#doctest.set_unittest_reportflags" title="doctest.set_unittest_reportflags"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_unittest_reportflags()</span></code></a> below
 | ||
| for a better way to set reporting options.</p>
 | ||
| <p>Optional argument <em>parser</em> specifies a <a class="reference internal" href="#doctest.DocTestParser" title="doctest.DocTestParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestParser</span></code></a> (or subclass)
 | ||
| that should be used to extract tests from the files.  It defaults to a normal
 | ||
| parser (i.e., <code class="docutils literal notranslate"><span class="pre">DocTestParser()</span></code>).</p>
 | ||
| <p>Optional argument <em>encoding</em> specifies an encoding that should be used to
 | ||
| convert the file to unicode.</p>
 | ||
| <p>The global <code class="docutils literal notranslate"><span class="pre">__file__</span></code> is added to the globals provided to doctests loaded
 | ||
| from a text file using <a class="reference internal" href="#doctest.DocFileSuite" title="doctest.DocFileSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocFileSuite()</span></code></a>.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py function">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestSuite">
 | ||
| <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DocTestSuite</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">extraglobs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">test_finder</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">setUp</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tearDown</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optionflags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">checker</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestSuite" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Convert doctest tests for a module to a <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a>.</p>
 | ||
| <p>The returned <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a> is to be run by the unittest framework
 | ||
| and runs each doctest in the module.  If any of the doctests fail, then the
 | ||
| synthesized unit test fails, and a <a class="reference internal" href="#doctest.failureException" title="doctest.failureException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">failureException</span></code></a> exception is raised
 | ||
| showing the name of the file containing the test and a (sometimes approximate)
 | ||
| line number.  If all the examples in a docstring are skipped, then the
 | ||
| synthesized unit test is also marked as skipped.</p>
 | ||
| <p>Optional argument <em>module</em> provides the module to be tested.  It can be a module
 | ||
| object or a (possibly dotted) module name.  If not specified, the module calling
 | ||
| this function is used.</p>
 | ||
| <p>Optional argument <em>globs</em> is a dictionary containing the initial global
 | ||
| variables for the tests.  A new copy of this dictionary is created for each
 | ||
| test.  By default, <em>globs</em> is a new empty dictionary.</p>
 | ||
| <p>Optional argument <em>extraglobs</em> specifies an extra set of global variables, which
 | ||
| is merged into <em>globs</em>.  By default, no extra globals are used.</p>
 | ||
| <p>Optional argument <em>test_finder</em> is the <a class="reference internal" href="#doctest.DocTestFinder" title="doctest.DocTestFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestFinder</span></code></a> object (or a
 | ||
| drop-in replacement) that is used to extract doctests from the module.</p>
 | ||
| <p>Optional arguments <em>setUp</em>, <em>tearDown</em>, and <em>optionflags</em> are the same as for
 | ||
| function <a class="reference internal" href="#doctest.DocFileSuite" title="doctest.DocFileSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocFileSuite()</span></code></a> above.</p>
 | ||
| <p>This function uses the same search technique as <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>.</p>
 | ||
| <div class="versionchanged">
 | ||
| <p><span class="versionmodified changed">Changed in version 3.5: </span><a class="reference internal" href="#doctest.DocTestSuite" title="doctest.DocTestSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocTestSuite()</span></code></a> returns an empty <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a> if <em>module</em>
 | ||
| contains no docstrings instead of raising <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
 | ||
| </div>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py exception">
 | ||
| <dt class="sig sig-object py" id="doctest.failureException">
 | ||
| <em class="property"><span class="k"><span class="pre">exception</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">failureException</span></span><a class="headerlink" href="#doctest.failureException" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>When doctests which have been converted to unit tests by <a class="reference internal" href="#doctest.DocFileSuite" title="doctest.DocFileSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocFileSuite()</span></code></a>
 | ||
| or <a class="reference internal" href="#doctest.DocTestSuite" title="doctest.DocTestSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocTestSuite()</span></code></a> fail, this exception is raised showing the name of
 | ||
| the file containing the test and a (sometimes approximate) line number.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <p>Under the covers, <a class="reference internal" href="#doctest.DocTestSuite" title="doctest.DocTestSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocTestSuite()</span></code></a> creates a <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a> out
 | ||
| of <code class="xref py py-class docutils literal notranslate"><span class="pre">doctest.DocTestCase</span></code> instances, and <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestCase</span></code> is a
 | ||
| subclass of <a class="reference internal" href="unittest.html#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestCase</span></code></a>. <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestCase</span></code> isn’t documented
 | ||
| here (it’s an internal detail), but studying its code can answer questions about
 | ||
| the exact details of <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> integration.</p>
 | ||
| <p>Similarly, <a class="reference internal" href="#doctest.DocFileSuite" title="doctest.DocFileSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocFileSuite()</span></code></a> creates a <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a> out of
 | ||
| <code class="xref py py-class docutils literal notranslate"><span class="pre">doctest.DocFileCase</span></code> instances, and <code class="xref py py-class docutils literal notranslate"><span class="pre">DocFileCase</span></code> is a subclass
 | ||
| of <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestCase</span></code>.</p>
 | ||
| <p>So both ways of creating a <a class="reference internal" href="unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</span></code></a> run instances of
 | ||
| <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestCase</span></code>.  This is important for a subtle reason: when you run
 | ||
| <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> functions yourself, you can control the <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> options in
 | ||
| use directly, by passing option flags to <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> functions.  However, if
 | ||
| you’re writing a <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> framework, <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> ultimately controls
 | ||
| when and how tests get run.  The framework author typically wants to control
 | ||
| <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> reporting options (perhaps, e.g., specified by command line
 | ||
| options), but there’s no way to pass options through <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> to
 | ||
| <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> test runners.</p>
 | ||
| <p>For this reason, <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> also supports a notion of <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a>
 | ||
| reporting flags specific to <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> support, via this function:</p>
 | ||
| <dl class="py function">
 | ||
| <dt class="sig sig-object py" id="doctest.set_unittest_reportflags">
 | ||
| <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">set_unittest_reportflags</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">flags</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.set_unittest_reportflags" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Set the <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> reporting flags to use.</p>
 | ||
| <p>Argument <em>flags</em> takes the <a class="reference internal" href="../reference/expressions.html#bitwise"><span class="std std-ref">bitwise OR</span></a> of option flags.  See
 | ||
| section <a class="reference internal" href="#doctest-options"><span class="std std-ref">Option Flags</span></a>.  Only “reporting flags” can be used.</p>
 | ||
| <p>This is a module-global setting, and affects all future doctests run by module
 | ||
| <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a>:  the <code class="xref py py-meth docutils literal notranslate"><span class="pre">runTest()</span></code> method of <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestCase</span></code> looks at
 | ||
| the option flags specified for the test case when the <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestCase</span></code>
 | ||
| instance was constructed.  If no reporting flags were specified (which is the
 | ||
| typical and expected case), <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code>’s <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> reporting flags are
 | ||
| <a class="reference internal" href="../reference/expressions.html#bitwise"><span class="std std-ref">bitwise ORed</span></a> into the option flags, and the option flags
 | ||
| so augmented are passed to the <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> instance created to
 | ||
| run the doctest.  If any reporting flags were specified when the
 | ||
| <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestCase</span></code> instance was constructed, <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code>’s
 | ||
| <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> reporting flags are ignored.</p>
 | ||
| <p>The value of the <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> reporting flags in effect before the function
 | ||
| was called is returned by the function.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </section>
 | ||
| <section id="advanced-api">
 | ||
| <span id="doctest-advanced-api"></span><h2>Advanced API<a class="headerlink" href="#advanced-api" title="Link to this heading">¶</a></h2>
 | ||
| <p>The basic API is a simple wrapper that’s intended to make doctest easy to use.
 | ||
| It is fairly flexible, and should meet most users’ needs; however, if you
 | ||
| require more fine-grained control over testing, or wish to extend doctest’s
 | ||
| capabilities, then you should use the advanced API.</p>
 | ||
| <p>The advanced API revolves around two container classes, which are used to store
 | ||
| the interactive examples extracted from doctest cases:</p>
 | ||
| <ul class="simple">
 | ||
| <li><p><a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a>: A single Python <a class="reference internal" href="../glossary.html#term-statement"><span class="xref std std-term">statement</span></a>, paired with its expected
 | ||
| output.</p></li>
 | ||
| <li><p><a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>: A collection of <a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a>s, typically extracted
 | ||
| from a single docstring or text file.</p></li>
 | ||
| </ul>
 | ||
| <p>Additional processing classes are defined to find, parse, and run, and check
 | ||
| doctest examples:</p>
 | ||
| <ul class="simple">
 | ||
| <li><p><a class="reference internal" href="#doctest.DocTestFinder" title="doctest.DocTestFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestFinder</span></code></a>: Finds all docstrings in a given module, and uses a
 | ||
| <a class="reference internal" href="#doctest.DocTestParser" title="doctest.DocTestParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestParser</span></code></a> to create a <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> from every docstring that
 | ||
| contains interactive examples.</p></li>
 | ||
| <li><p><a class="reference internal" href="#doctest.DocTestParser" title="doctest.DocTestParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestParser</span></code></a>: Creates a <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object from a string (such
 | ||
| as an object’s docstring).</p></li>
 | ||
| <li><p><a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a>: Executes the examples in a <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>, and uses
 | ||
| an <a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a> to verify their output.</p></li>
 | ||
| <li><p><a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a>: Compares the actual output from a doctest example with
 | ||
| the expected output, and decides whether they match.</p></li>
 | ||
| </ul>
 | ||
| <p>The relationships among these processing classes are summarized in the following
 | ||
| diagram:</p>
 | ||
| <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span>                            <span class="nb">list</span> <span class="n">of</span><span class="p">:</span>
 | ||
| <span class="o">+------+</span>                   <span class="o">+---------+</span>
 | ||
| <span class="o">|</span><span class="n">module</span><span class="o">|</span> <span class="o">--</span><span class="n">DocTestFinder</span><span class="o">-></span> <span class="o">|</span> <span class="n">DocTest</span> <span class="o">|</span> <span class="o">--</span><span class="n">DocTestRunner</span><span class="o">-></span> <span class="n">results</span>
 | ||
| <span class="o">+------+</span>    <span class="o">|</span>        <span class="o">^</span>     <span class="o">+---------+</span>     <span class="o">|</span>       <span class="o">^</span>    <span class="p">(</span><span class="n">printed</span><span class="p">)</span>
 | ||
|             <span class="o">|</span>        <span class="o">|</span>     <span class="o">|</span> <span class="n">Example</span> <span class="o">|</span>     <span class="o">|</span>       <span class="o">|</span>
 | ||
|             <span class="n">v</span>        <span class="o">|</span>     <span class="o">|</span>   <span class="o">...</span>   <span class="o">|</span>     <span class="n">v</span>       <span class="o">|</span>
 | ||
|            <span class="n">DocTestParser</span>   <span class="o">|</span> <span class="n">Example</span> <span class="o">|</span>   <span class="n">OutputChecker</span>
 | ||
|                            <span class="o">+---------+</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <section id="doctest-objects">
 | ||
| <span id="doctest-doctest"></span><h3>DocTest Objects<a class="headerlink" href="#doctest-objects" title="Link to this heading">¶</a></h3>
 | ||
| <dl class="py class">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTest">
 | ||
| <em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DocTest</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">examples</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">lineno</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">docstring</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTest" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>A collection of doctest examples that should be run in a single namespace.  The
 | ||
| constructor arguments are used to initialize the attributes of the same names.</p>
 | ||
| <p><a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> defines the following attributes.  They are initialized by
 | ||
| the constructor, and should not be modified directly.</p>
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTest.examples">
 | ||
| <span class="sig-name descname"><span class="pre">examples</span></span><a class="headerlink" href="#doctest.DocTest.examples" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>A list of <a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a> objects encoding the individual interactive Python
 | ||
| examples that should be run by this test.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTest.globs">
 | ||
| <span class="sig-name descname"><span class="pre">globs</span></span><a class="headerlink" href="#doctest.DocTest.globs" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>The namespace (aka globals) that the examples should be run in. This is a
 | ||
| dictionary mapping names to values.  Any changes to the namespace made by the
 | ||
| examples (such as binding new variables) will be reflected in <a class="reference internal" href="#doctest.DocTest.globs" title="doctest.DocTest.globs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">globs</span></code></a>
 | ||
| after the test is run.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTest.name">
 | ||
| <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#doctest.DocTest.name" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>A string name identifying the <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>.  Typically, this is the name
 | ||
| of the object or file that the test was extracted from.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTest.filename">
 | ||
| <span class="sig-name descname"><span class="pre">filename</span></span><a class="headerlink" href="#doctest.DocTest.filename" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>The name of the file that this <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> was extracted from; or
 | ||
| <code class="docutils literal notranslate"><span class="pre">None</span></code> if the filename is unknown, or if the <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> was not
 | ||
| extracted from a file.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTest.lineno">
 | ||
| <span class="sig-name descname"><span class="pre">lineno</span></span><a class="headerlink" href="#doctest.DocTest.lineno" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>The line number within <a class="reference internal" href="#doctest.DocTest.filename" title="doctest.DocTest.filename"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filename</span></code></a> where this <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> begins, or
 | ||
| <code class="docutils literal notranslate"><span class="pre">None</span></code> if the line number is unavailable.  This line number is zero-based
 | ||
| with respect to the beginning of the file.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTest.docstring">
 | ||
| <span class="sig-name descname"><span class="pre">docstring</span></span><a class="headerlink" href="#doctest.DocTest.docstring" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>The string that the test was extracted from, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if the string is
 | ||
| unavailable, or if the test was not extracted from a string.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </section>
 | ||
| <section id="example-objects">
 | ||
| <span id="doctest-example"></span><h3>Example Objects<a class="headerlink" href="#example-objects" title="Link to this heading">¶</a></h3>
 | ||
| <dl class="py class">
 | ||
| <dt class="sig sig-object py" id="doctest.Example">
 | ||
| <em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">Example</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">want</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exc_msg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">lineno</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">indent</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">options</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.Example" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>A single interactive example, consisting of a Python statement and its expected
 | ||
| output.  The constructor arguments are used to initialize the attributes of
 | ||
| the same names.</p>
 | ||
| <p><a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a> defines the following attributes.  They are initialized by
 | ||
| the constructor, and should not be modified directly.</p>
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.Example.source">
 | ||
| <span class="sig-name descname"><span class="pre">source</span></span><a class="headerlink" href="#doctest.Example.source" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>A string containing the example’s source code.  This source code consists of a
 | ||
| single Python statement, and always ends with a newline; the constructor adds
 | ||
| a newline when necessary.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.Example.want">
 | ||
| <span class="sig-name descname"><span class="pre">want</span></span><a class="headerlink" href="#doctest.Example.want" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>The expected output from running the example’s source code (either from
 | ||
| stdout, or a traceback in case of exception).  <a class="reference internal" href="#doctest.Example.want" title="doctest.Example.want"><code class="xref py py-attr docutils literal notranslate"><span class="pre">want</span></code></a> ends with a
 | ||
| newline unless no output is expected, in which case it’s an empty string.  The
 | ||
| constructor adds a newline when necessary.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.Example.exc_msg">
 | ||
| <span class="sig-name descname"><span class="pre">exc_msg</span></span><a class="headerlink" href="#doctest.Example.exc_msg" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>The exception message generated by the example, if the example is expected to
 | ||
| generate an exception; or <code class="docutils literal notranslate"><span class="pre">None</span></code> if it is not expected to generate an
 | ||
| exception.  This exception message is compared against the return value of
 | ||
| <a class="reference internal" href="traceback.html#traceback.format_exception_only" title="traceback.format_exception_only"><code class="xref py py-func docutils literal notranslate"><span class="pre">traceback.format_exception_only()</span></code></a>.  <a class="reference internal" href="#doctest.Example.exc_msg" title="doctest.Example.exc_msg"><code class="xref py py-attr docutils literal notranslate"><span class="pre">exc_msg</span></code></a> ends with a newline
 | ||
| unless it’s <code class="docutils literal notranslate"><span class="pre">None</span></code>.  The constructor adds a newline if needed.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.Example.lineno">
 | ||
| <span class="sig-name descname"><span class="pre">lineno</span></span><a class="headerlink" href="#doctest.Example.lineno" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>The line number within the string containing this example where the example
 | ||
| begins.  This line number is zero-based with respect to the beginning of the
 | ||
| containing string.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.Example.indent">
 | ||
| <span class="sig-name descname"><span class="pre">indent</span></span><a class="headerlink" href="#doctest.Example.indent" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>The example’s indentation in the containing string, i.e., the number of space
 | ||
| characters that precede the example’s first prompt.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.Example.options">
 | ||
| <span class="sig-name descname"><span class="pre">options</span></span><a class="headerlink" href="#doctest.Example.options" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>A dictionary mapping from option flags to <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code>, which is used
 | ||
| to override default options for this example.  Any option flags not contained
 | ||
| in this dictionary are left at their default value (as specified by the
 | ||
| <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a>’s <a class="reference internal" href="#doctest-options"><span class="std std-ref">optionflags</span></a>).
 | ||
| By default, no options are set.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </section>
 | ||
| <section id="doctestfinder-objects">
 | ||
| <span id="doctest-doctestfinder"></span><h3>DocTestFinder objects<a class="headerlink" href="#doctestfinder-objects" title="Link to this heading">¶</a></h3>
 | ||
| <dl class="py class">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestFinder">
 | ||
| <em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DocTestFinder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parser</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">DocTestParser()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">recurse</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exclude_empty</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestFinder" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>A processing class used to extract the <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>s that are relevant to
 | ||
| a given object, from its docstring and the docstrings of its contained objects.
 | ||
| <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>s can be extracted from modules, classes, functions,
 | ||
| methods, staticmethods, classmethods, and properties.</p>
 | ||
| <p>The optional argument <em>verbose</em> can be used to display the objects searched by
 | ||
| the finder.  It defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code> (no output).</p>
 | ||
| <p>The optional argument <em>parser</em> specifies the <a class="reference internal" href="#doctest.DocTestParser" title="doctest.DocTestParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestParser</span></code></a> object (or a
 | ||
| drop-in replacement) that is used to extract doctests from docstrings.</p>
 | ||
| <p>If the optional argument <em>recurse</em> is false, then <a class="reference internal" href="#doctest.DocTestFinder.find" title="doctest.DocTestFinder.find"><code class="xref py py-meth docutils literal notranslate"><span class="pre">DocTestFinder.find()</span></code></a>
 | ||
| will only examine the given object, and not any contained objects.</p>
 | ||
| <p>If the optional argument <em>exclude_empty</em> is false, then
 | ||
| <a class="reference internal" href="#doctest.DocTestFinder.find" title="doctest.DocTestFinder.find"><code class="xref py py-meth docutils literal notranslate"><span class="pre">DocTestFinder.find()</span></code></a> will include tests for objects with empty docstrings.</p>
 | ||
| <p><a class="reference internal" href="#doctest.DocTestFinder" title="doctest.DocTestFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestFinder</span></code></a> defines the following method:</p>
 | ||
| <dl class="py method">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestFinder.find">
 | ||
| <span class="sig-name descname"><span class="pre">find</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">obj[,</span> <span class="pre">name][,</span> <span class="pre">module][,</span> <span class="pre">globs][,</span> <span class="pre">extraglobs]</span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestFinder.find" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Return a list of the <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>s that are defined by <em>obj</em>’s
 | ||
| docstring, or by any of its contained objects’ docstrings.</p>
 | ||
| <p>The optional argument <em>name</em> specifies the object’s name; this name will be
 | ||
| used to construct names for the returned <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>s.  If <em>name</em> is
 | ||
| not specified, then <code class="docutils literal notranslate"><span class="pre">obj.__name__</span></code> is used.</p>
 | ||
| <p>The optional parameter <em>module</em> is the module that contains the given object.
 | ||
| If the module is not specified or is <code class="docutils literal notranslate"><span class="pre">None</span></code>, then the test finder will attempt
 | ||
| to automatically determine the correct module.  The object’s module is used:</p>
 | ||
| <ul class="simple">
 | ||
| <li><p>As a default namespace, if <em>globs</em> is not specified.</p></li>
 | ||
| <li><p>To prevent the DocTestFinder from extracting DocTests from objects that are
 | ||
| imported from other modules.  (Contained objects with modules other than
 | ||
| <em>module</em> are ignored.)</p></li>
 | ||
| <li><p>To find the name of the file containing the object.</p></li>
 | ||
| <li><p>To help find the line number of the object within its file.</p></li>
 | ||
| </ul>
 | ||
| <p>If <em>module</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, no attempt to find the module will be made.  This is
 | ||
| obscure, of use mostly in testing doctest itself: if <em>module</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, or
 | ||
| is <code class="docutils literal notranslate"><span class="pre">None</span></code> but cannot be found automatically, then all objects are considered
 | ||
| to belong to the (non-existent) module, so all contained objects will
 | ||
| (recursively) be searched for doctests.</p>
 | ||
| <p>The globals for each <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> is formed by combining <em>globs</em> and
 | ||
| <em>extraglobs</em> (bindings in <em>extraglobs</em> override bindings in <em>globs</em>).  A new
 | ||
| shallow copy of the globals dictionary is created for each <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>.
 | ||
| If <em>globs</em> is not specified, then it defaults to the module’s <em>__dict__</em>, if
 | ||
| specified, or <code class="docutils literal notranslate"><span class="pre">{}</span></code> otherwise.  If <em>extraglobs</em> is not specified, then it
 | ||
| defaults to <code class="docutils literal notranslate"><span class="pre">{}</span></code>.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </section>
 | ||
| <section id="doctestparser-objects">
 | ||
| <span id="doctest-doctestparser"></span><h3>DocTestParser objects<a class="headerlink" href="#doctestparser-objects" title="Link to this heading">¶</a></h3>
 | ||
| <dl class="py class">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestParser">
 | ||
| <em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DocTestParser</span></span><a class="headerlink" href="#doctest.DocTestParser" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>A processing class used to extract interactive examples from a string, and use
 | ||
| them to create a <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object.</p>
 | ||
| <p><a class="reference internal" href="#doctest.DocTestParser" title="doctest.DocTestParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestParser</span></code></a> defines the following methods:</p>
 | ||
| <dl class="py method">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestParser.get_doctest">
 | ||
| <span class="sig-name descname"><span class="pre">get_doctest</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">lineno</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestParser.get_doctest" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Extract all doctest examples from the given string, and collect them into a
 | ||
| <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object.</p>
 | ||
| <p><em>globs</em>, <em>name</em>, <em>filename</em>, and <em>lineno</em> are attributes for the new
 | ||
| <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object.  See the documentation for <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> for more
 | ||
| information.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py method">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestParser.get_examples">
 | ||
| <span class="sig-name descname"><span class="pre">get_examples</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'<string>'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestParser.get_examples" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Extract all doctest examples from the given string, and return them as a list
 | ||
| of <a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a> objects.  Line numbers are 0-based.  The optional argument
 | ||
| <em>name</em> is a name identifying this string, and is only used for error messages.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py method">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestParser.parse">
 | ||
| <span class="sig-name descname"><span class="pre">parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'<string>'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestParser.parse" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Divide the given string into examples and intervening text, and return them as
 | ||
| a list of alternating <a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a>s and strings. Line numbers for the
 | ||
| <a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a>s are 0-based.  The optional argument <em>name</em> is a name
 | ||
| identifying this string, and is only used for error messages.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </section>
 | ||
| <section id="testresults-objects">
 | ||
| <h3>TestResults objects<a class="headerlink" href="#testresults-objects" title="Link to this heading">¶</a></h3>
 | ||
| <dl class="py class">
 | ||
| <dt class="sig sig-object py" id="doctest.TestResults">
 | ||
| <em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">TestResults</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">failed</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attempted</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.TestResults" title="Link to this definition">¶</a></dt>
 | ||
| <dd><dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.TestResults.failed">
 | ||
| <span class="sig-name descname"><span class="pre">failed</span></span><a class="headerlink" href="#doctest.TestResults.failed" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Number of failed tests.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.TestResults.attempted">
 | ||
| <span class="sig-name descname"><span class="pre">attempted</span></span><a class="headerlink" href="#doctest.TestResults.attempted" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Number of attempted tests.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.TestResults.skipped">
 | ||
| <span class="sig-name descname"><span class="pre">skipped</span></span><a class="headerlink" href="#doctest.TestResults.skipped" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Number of skipped tests.</p>
 | ||
| <div class="versionadded">
 | ||
| <p><span class="versionmodified added">Added in version 3.13.</span></p>
 | ||
| </div>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </section>
 | ||
| <section id="doctestrunner-objects">
 | ||
| <span id="doctest-doctestrunner"></span><h3>DocTestRunner objects<a class="headerlink" href="#doctestrunner-objects" title="Link to this heading">¶</a></h3>
 | ||
| <dl class="py class">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestRunner">
 | ||
| <em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DocTestRunner</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">checker</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optionflags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestRunner" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>A processing class used to execute and verify the interactive examples in a
 | ||
| <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a>.</p>
 | ||
| <p>The comparison between expected outputs and actual outputs is done by an
 | ||
| <a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a>.  This comparison may be customized with a number of
 | ||
| option flags; see section <a class="reference internal" href="#doctest-options"><span class="std std-ref">Option Flags</span></a> for more information.  If the
 | ||
| option flags are insufficient, then the comparison may also be customized by
 | ||
| passing a subclass of <a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a> to the constructor.</p>
 | ||
| <p>The test runner’s display output can be controlled in two ways. First, an output
 | ||
| function can be passed to <a class="reference internal" href="#doctest.DocTestRunner.run" title="doctest.DocTestRunner.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a>; this function will be called
 | ||
| with strings that should be displayed.  It defaults to <code class="docutils literal notranslate"><span class="pre">sys.stdout.write</span></code>.  If
 | ||
| capturing the output is not sufficient, then the display output can be also
 | ||
| customized by subclassing DocTestRunner, and overriding the methods
 | ||
| <a class="reference internal" href="#doctest.DocTestRunner.report_start" title="doctest.DocTestRunner.report_start"><code class="xref py py-meth docutils literal notranslate"><span class="pre">report_start()</span></code></a>, <a class="reference internal" href="#doctest.DocTestRunner.report_success" title="doctest.DocTestRunner.report_success"><code class="xref py py-meth docutils literal notranslate"><span class="pre">report_success()</span></code></a>,
 | ||
| <a class="reference internal" href="#doctest.DocTestRunner.report_unexpected_exception" title="doctest.DocTestRunner.report_unexpected_exception"><code class="xref py py-meth docutils literal notranslate"><span class="pre">report_unexpected_exception()</span></code></a>, and <a class="reference internal" href="#doctest.DocTestRunner.report_failure" title="doctest.DocTestRunner.report_failure"><code class="xref py py-meth docutils literal notranslate"><span class="pre">report_failure()</span></code></a>.</p>
 | ||
| <p>The optional keyword argument <em>checker</em> specifies the <a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a>
 | ||
| object (or drop-in replacement) that should be used to compare the expected
 | ||
| outputs to the actual outputs of doctest examples.</p>
 | ||
| <p>The optional keyword argument <em>verbose</em> controls the <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a>’s
 | ||
| verbosity.  If <em>verbose</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, then information is printed about each
 | ||
| example, as it is run.  If <em>verbose</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, then only failures are
 | ||
| printed.  If <em>verbose</em> is unspecified, or <code class="docutils literal notranslate"><span class="pre">None</span></code>, then verbose output is used
 | ||
| iff the command-line switch <code class="docutils literal notranslate"><span class="pre">-v</span></code> is used.</p>
 | ||
| <p>The optional keyword argument <em>optionflags</em> can be used to control how the test
 | ||
| runner compares expected output to actual output, and how it displays failures.
 | ||
| For more information, see section <a class="reference internal" href="#doctest-options"><span class="std std-ref">Option Flags</span></a>.</p>
 | ||
| <p>The test runner accumulates statistics. The aggregated number of attempted,
 | ||
| failed and skipped examples is also available via the <a class="reference internal" href="#doctest.DocTestRunner.tries" title="doctest.DocTestRunner.tries"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tries</span></code></a>,
 | ||
| <a class="reference internal" href="#doctest.DocTestRunner.failures" title="doctest.DocTestRunner.failures"><code class="xref py py-attr docutils literal notranslate"><span class="pre">failures</span></code></a> and <a class="reference internal" href="#doctest.DocTestRunner.skips" title="doctest.DocTestRunner.skips"><code class="xref py py-attr docutils literal notranslate"><span class="pre">skips</span></code></a> attributes. The <a class="reference internal" href="#doctest.DocTestRunner.run" title="doctest.DocTestRunner.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> and
 | ||
| <a class="reference internal" href="#doctest.DocTestRunner.summarize" title="doctest.DocTestRunner.summarize"><code class="xref py py-meth docutils literal notranslate"><span class="pre">summarize()</span></code></a> methods return a <a class="reference internal" href="#doctest.TestResults" title="doctest.TestResults"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResults</span></code></a> instance.</p>
 | ||
| <p><a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> defines the following methods:</p>
 | ||
| <dl class="py method">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestRunner.report_start">
 | ||
| <span class="sig-name descname"><span class="pre">report_start</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">out</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">example</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestRunner.report_start" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Report that the test runner is about to process the given example. This method
 | ||
| is provided to allow subclasses of <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> to customize their
 | ||
| output; it should not be called directly.</p>
 | ||
| <p><em>example</em> is the example about to be processed.  <em>test</em> is the test
 | ||
| <em>containing example</em>.  <em>out</em> is the output function that was passed to
 | ||
| <a class="reference internal" href="#doctest.DocTestRunner.run" title="doctest.DocTestRunner.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">DocTestRunner.run()</span></code></a>.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py method">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestRunner.report_success">
 | ||
| <span class="sig-name descname"><span class="pre">report_success</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">out</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">example</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">got</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestRunner.report_success" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Report that the given example ran successfully.  This method is provided to
 | ||
| allow subclasses of <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> to customize their output; it
 | ||
| should not be called directly.</p>
 | ||
| <p><em>example</em> is the example about to be processed.  <em>got</em> is the actual output
 | ||
| from the example.  <em>test</em> is the test containing <em>example</em>.  <em>out</em> is the
 | ||
| output function that was passed to <a class="reference internal" href="#doctest.DocTestRunner.run" title="doctest.DocTestRunner.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">DocTestRunner.run()</span></code></a>.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py method">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestRunner.report_failure">
 | ||
| <span class="sig-name descname"><span class="pre">report_failure</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">out</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">example</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">got</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestRunner.report_failure" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Report that the given example failed.  This method is provided to allow
 | ||
| subclasses of <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> to customize their output; it should not
 | ||
| be called directly.</p>
 | ||
| <p><em>example</em> is the example about to be processed.  <em>got</em> is the actual output
 | ||
| from the example.  <em>test</em> is the test containing <em>example</em>.  <em>out</em> is the
 | ||
| output function that was passed to <a class="reference internal" href="#doctest.DocTestRunner.run" title="doctest.DocTestRunner.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">DocTestRunner.run()</span></code></a>.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py method">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestRunner.report_unexpected_exception">
 | ||
| <span class="sig-name descname"><span class="pre">report_unexpected_exception</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">out</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">example</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exc_info</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestRunner.report_unexpected_exception" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Report that the given example raised an unexpected exception. This method is
 | ||
| provided to allow subclasses of <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> to customize their
 | ||
| output; it should not be called directly.</p>
 | ||
| <p><em>example</em> is the example about to be processed. <em>exc_info</em> is a tuple
 | ||
| containing information about the unexpected exception (as returned by
 | ||
| <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_info()</span></code></a>). <em>test</em> is the test containing <em>example</em>.  <em>out</em> is the
 | ||
| output function that was passed to <a class="reference internal" href="#doctest.DocTestRunner.run" title="doctest.DocTestRunner.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">DocTestRunner.run()</span></code></a>.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py method">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestRunner.run">
 | ||
| <span class="sig-name descname"><span class="pre">run</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compileflags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">out</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">clear_globs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestRunner.run" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Run the examples in <em>test</em> (a <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object), and display the
 | ||
| results using the writer function <em>out</em>. Return a <a class="reference internal" href="#doctest.TestResults" title="doctest.TestResults"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResults</span></code></a>
 | ||
| instance.</p>
 | ||
| <p>The examples are run in the namespace <code class="docutils literal notranslate"><span class="pre">test.globs</span></code>.  If <em>clear_globs</em> is
 | ||
| true (the default), then this namespace will be cleared after the test runs,
 | ||
| to help with garbage collection. If you would like to examine the namespace
 | ||
| after the test completes, then use <em>clear_globs=False</em>.</p>
 | ||
| <p><em>compileflags</em> gives the set of flags that should be used by the Python
 | ||
| compiler when running the examples.  If not specified, then it will default to
 | ||
| the set of future-import flags that apply to <em>globs</em>.</p>
 | ||
| <p>The output of each example is checked using the <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a>’s
 | ||
| output checker, and the results are formatted by the
 | ||
| <code class="xref py py-meth docutils literal notranslate"><span class="pre">DocTestRunner.report_*()</span></code> methods.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py method">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestRunner.summarize">
 | ||
| <span class="sig-name descname"><span class="pre">summarize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestRunner.summarize" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Print a summary of all the test cases that have been run by this DocTestRunner,
 | ||
| and return a <a class="reference internal" href="#doctest.TestResults" title="doctest.TestResults"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResults</span></code></a> instance.</p>
 | ||
| <p>The optional <em>verbose</em> argument controls how detailed the summary is.  If the
 | ||
| verbosity is not specified, then the <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a>’s verbosity is
 | ||
| used.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <p><a class="reference internal" href="#doctest.DocTestParser" title="doctest.DocTestParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestParser</span></code></a> has the following attributes:</p>
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestRunner.tries">
 | ||
| <span class="sig-name descname"><span class="pre">tries</span></span><a class="headerlink" href="#doctest.DocTestRunner.tries" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Number of attempted examples.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestRunner.failures">
 | ||
| <span class="sig-name descname"><span class="pre">failures</span></span><a class="headerlink" href="#doctest.DocTestRunner.failures" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Number of failed examples.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestRunner.skips">
 | ||
| <span class="sig-name descname"><span class="pre">skips</span></span><a class="headerlink" href="#doctest.DocTestRunner.skips" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Number of skipped examples.</p>
 | ||
| <div class="versionadded">
 | ||
| <p><span class="versionmodified added">Added in version 3.13.</span></p>
 | ||
| </div>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </section>
 | ||
| <section id="outputchecker-objects">
 | ||
| <span id="doctest-outputchecker"></span><h3>OutputChecker objects<a class="headerlink" href="#outputchecker-objects" title="Link to this heading">¶</a></h3>
 | ||
| <dl class="py class">
 | ||
| <dt class="sig sig-object py" id="doctest.OutputChecker">
 | ||
| <em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">OutputChecker</span></span><a class="headerlink" href="#doctest.OutputChecker" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>A class used to check the whether the actual output from a doctest example
 | ||
| matches the expected output.  <a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a> defines two methods:
 | ||
| <a class="reference internal" href="#doctest.OutputChecker.check_output" title="doctest.OutputChecker.check_output"><code class="xref py py-meth docutils literal notranslate"><span class="pre">check_output()</span></code></a>, which compares a given pair of outputs, and returns <code class="docutils literal notranslate"><span class="pre">True</span></code>
 | ||
| if they match; and <a class="reference internal" href="#doctest.OutputChecker.output_difference" title="doctest.OutputChecker.output_difference"><code class="xref py py-meth docutils literal notranslate"><span class="pre">output_difference()</span></code></a>, which returns a string describing
 | ||
| the differences between two outputs.</p>
 | ||
| <p><a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a> defines the following methods:</p>
 | ||
| <dl class="py method">
 | ||
| <dt class="sig sig-object py" id="doctest.OutputChecker.check_output">
 | ||
| <span class="sig-name descname"><span class="pre">check_output</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">want</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">got</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optionflags</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.OutputChecker.check_output" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> iff the actual output from an example (<em>got</em>) matches the
 | ||
| expected output (<em>want</em>).  These strings are always considered to match if
 | ||
| they are identical; but depending on what option flags the test runner is
 | ||
| using, several non-exact match types are also possible.  See section
 | ||
| <a class="reference internal" href="#doctest-options"><span class="std std-ref">Option Flags</span></a> for more information about option flags.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py method">
 | ||
| <dt class="sig sig-object py" id="doctest.OutputChecker.output_difference">
 | ||
| <span class="sig-name descname"><span class="pre">output_difference</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">example</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">got</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optionflags</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.OutputChecker.output_difference" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Return a string describing the differences between the expected output for a
 | ||
| given example (<em>example</em>) and the actual output (<em>got</em>).  <em>optionflags</em> is the
 | ||
| set of option flags used to compare <em>want</em> and <em>got</em>.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </section>
 | ||
| </section>
 | ||
| <section id="debugging">
 | ||
| <span id="doctest-debugging"></span><h2>Debugging<a class="headerlink" href="#debugging" title="Link to this heading">¶</a></h2>
 | ||
| <p>Doctest provides several mechanisms for debugging doctest examples:</p>
 | ||
| <ul>
 | ||
| <li><p>Several functions convert doctests to executable Python programs, which can be
 | ||
| run under the Python debugger, <a class="reference internal" href="pdb.html#module-pdb" title="pdb: The Python debugger for interactive interpreters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pdb</span></code></a>.</p></li>
 | ||
| <li><p>The <a class="reference internal" href="#doctest.DebugRunner" title="doctest.DebugRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DebugRunner</span></code></a> class is a subclass of <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> that
 | ||
| raises an exception for the first failing example, containing information about
 | ||
| that example. This information can be used to perform post-mortem debugging on
 | ||
| the example.</p></li>
 | ||
| <li><p>The <a class="reference internal" href="unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> cases generated by <a class="reference internal" href="#doctest.DocTestSuite" title="doctest.DocTestSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocTestSuite()</span></code></a> support the
 | ||
| <a class="reference internal" href="#doctest.debug" title="doctest.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">debug()</span></code></a> method defined by <a class="reference internal" href="unittest.html#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestCase</span></code></a>.</p></li>
 | ||
| <li><p>You can add a call to <a class="reference internal" href="pdb.html#pdb.set_trace" title="pdb.set_trace"><code class="xref py py-func docutils literal notranslate"><span class="pre">pdb.set_trace()</span></code></a> in a doctest example, and you’ll
 | ||
| drop into the Python debugger when that line is executed.  Then you can inspect
 | ||
| current values of variables, and so on.  For example, suppose <code class="file docutils literal notranslate"><span class="pre">a.py</span></code>
 | ||
| contains just this module docstring:</p>
 | ||
| <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
 | ||
| <span class="sd">>>> def f(x):</span>
 | ||
| <span class="sd">...     g(x*2)</span>
 | ||
| <span class="sd">>>> def g(x):</span>
 | ||
| <span class="sd">...     print(x+3)</span>
 | ||
| <span class="sd">...     import pdb; pdb.set_trace()</span>
 | ||
| <span class="sd">>>> f(3)</span>
 | ||
| <span class="sd">9</span>
 | ||
| <span class="sd">"""</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>Then an interactive Python session may look like this:</p>
 | ||
| <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">a</span><span class="o">,</span><span class="w"> </span><span class="nn">doctest</span>
 | ||
| <span class="gp">>>> </span><span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
 | ||
| <span class="go">--Return--</span>
 | ||
| <span class="go">> <doctest a[1]>(3)g()->None</span>
 | ||
| <span class="go">-> import pdb; pdb.set_trace()</span>
 | ||
| <span class="go">(Pdb) list</span>
 | ||
| <span class="go">  1     def g(x):</span>
 | ||
| <span class="go">  2         print(x+3)</span>
 | ||
| <span class="go">  3  ->     import pdb; pdb.set_trace()</span>
 | ||
| <span class="go">[EOF]</span>
 | ||
| <span class="go">(Pdb) p x</span>
 | ||
| <span class="go">6</span>
 | ||
| <span class="go">(Pdb) step</span>
 | ||
| <span class="go">--Return--</span>
 | ||
| <span class="go">> <doctest a[0]>(2)f()->None</span>
 | ||
| <span class="go">-> g(x*2)</span>
 | ||
| <span class="go">(Pdb) list</span>
 | ||
| <span class="go">  1     def f(x):</span>
 | ||
| <span class="go">  2  ->     g(x*2)</span>
 | ||
| <span class="go">[EOF]</span>
 | ||
| <span class="go">(Pdb) p x</span>
 | ||
| <span class="go">3</span>
 | ||
| <span class="go">(Pdb) step</span>
 | ||
| <span class="go">--Return--</span>
 | ||
| <span class="go">> <doctest a[2]>(1)?()->None</span>
 | ||
| <span class="go">-> f(3)</span>
 | ||
| <span class="go">(Pdb) cont</span>
 | ||
| <span class="go">(0, 3)</span>
 | ||
| <span class="gp">>>></span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| </li>
 | ||
| </ul>
 | ||
| <p>Functions that convert doctests to Python code, and possibly run the synthesized
 | ||
| code under the debugger:</p>
 | ||
| <dl class="py function">
 | ||
| <dt class="sig sig-object py" id="doctest.script_from_examples">
 | ||
| <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">script_from_examples</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.script_from_examples" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Convert text with examples to a script.</p>
 | ||
| <p>Argument <em>s</em> is a string containing doctest examples.  The string is converted
 | ||
| to a Python script, where doctest examples in <em>s</em> are converted to regular code,
 | ||
| and everything else is converted to Python comments.  The generated script is
 | ||
| returned as a string. For example,</p>
 | ||
| <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>
 | ||
| <span class="nb">print</span><span class="p">(</span><span class="n">doctest</span><span class="o">.</span><span class="n">script_from_examples</span><span class="p">(</span><span class="sa">r</span><span class="s2">"""</span>
 | ||
| <span class="s2">    Set x and y to 1 and 2.</span>
 | ||
| <span class="s2">    >>> x, y = 1, 2</span>
 | ||
| 
 | ||
| <span class="s2">    Print their sum:</span>
 | ||
| <span class="s2">    >>> print(x+y)</span>
 | ||
| <span class="s2">    3</span>
 | ||
| <span class="s2">"""</span><span class="p">))</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>displays:</p>
 | ||
| <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Set x and y to 1 and 2.</span>
 | ||
| <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span>
 | ||
| <span class="c1">#</span>
 | ||
| <span class="c1"># Print their sum:</span>
 | ||
| <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="p">)</span>
 | ||
| <span class="c1"># Expected:</span>
 | ||
| <span class="c1">## 3</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>This function is used internally by other functions (see below), but can also be
 | ||
| useful when you want to transform an interactive Python session into a Python
 | ||
| script.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py function">
 | ||
| <dt class="sig sig-object py" id="doctest.testsource">
 | ||
| <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">testsource</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.testsource" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Convert the doctest for an object to a script.</p>
 | ||
| <p>Argument <em>module</em> is a module object, or dotted name of a module, containing the
 | ||
| object whose doctests are of interest.  Argument <em>name</em> is the name (within the
 | ||
| module) of the object with the doctests of interest.  The result is a string,
 | ||
| containing the object’s docstring converted to a Python script, as described for
 | ||
| <a class="reference internal" href="#doctest.script_from_examples" title="doctest.script_from_examples"><code class="xref py py-func docutils literal notranslate"><span class="pre">script_from_examples()</span></code></a> above.  For example, if module <code class="file docutils literal notranslate"><span class="pre">a.py</span></code>
 | ||
| contains a top-level function <code class="xref py py-func docutils literal notranslate"><span class="pre">f()</span></code>, then</p>
 | ||
| <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">a</span><span class="o">,</span><span class="w"> </span><span class="nn">doctest</span>
 | ||
| <span class="nb">print</span><span class="p">(</span><span class="n">doctest</span><span class="o">.</span><span class="n">testsource</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s2">"a.f"</span><span class="p">))</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p>prints a script version of function <code class="xref py py-func docutils literal notranslate"><span class="pre">f()</span></code>’s docstring, with doctests
 | ||
| converted to code, and the rest placed in comments.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py function">
 | ||
| <dt class="sig sig-object py" id="doctest.debug">
 | ||
| <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">debug</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pm</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.debug" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Debug the doctests for an object.</p>
 | ||
| <p>The <em>module</em> and <em>name</em> arguments are the same as for function
 | ||
| <a class="reference internal" href="#doctest.testsource" title="doctest.testsource"><code class="xref py py-func docutils literal notranslate"><span class="pre">testsource()</span></code></a> above.  The synthesized Python script for the named object’s
 | ||
| docstring is written to a temporary file, and then that file is run under the
 | ||
| control of the Python debugger, <a class="reference internal" href="pdb.html#module-pdb" title="pdb: The Python debugger for interactive interpreters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pdb</span></code></a>.</p>
 | ||
| <p>A shallow copy of <code class="docutils literal notranslate"><span class="pre">module.__dict__</span></code> is used for both local and global
 | ||
| execution context.</p>
 | ||
| <p>Optional argument <em>pm</em> controls whether post-mortem debugging is used.  If <em>pm</em>
 | ||
| has a true value, the script file is run directly, and the debugger gets
 | ||
| involved only if the script terminates via raising an unhandled exception.  If
 | ||
| it does, then post-mortem debugging is invoked, via <a class="reference internal" href="pdb.html#pdb.post_mortem" title="pdb.post_mortem"><code class="xref py py-func docutils literal notranslate"><span class="pre">pdb.post_mortem()</span></code></a>,
 | ||
| passing the traceback object from the unhandled exception.  If <em>pm</em> is not
 | ||
| specified, or is false, the script is run under the debugger from the start, via
 | ||
| passing an appropriate <a class="reference internal" href="functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> call to <a class="reference internal" href="pdb.html#pdb.run" title="pdb.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">pdb.run()</span></code></a>.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py function">
 | ||
| <dt class="sig sig-object py" id="doctest.debug_src">
 | ||
| <span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">debug_src</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">src</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pm</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.debug_src" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>Debug the doctests in a string.</p>
 | ||
| <p>This is like function <a class="reference internal" href="#doctest.debug" title="doctest.debug"><code class="xref py py-func docutils literal notranslate"><span class="pre">debug()</span></code></a> above, except that a string containing
 | ||
| doctest examples is specified directly, via the <em>src</em> argument.</p>
 | ||
| <p>Optional argument <em>pm</em> has the same meaning as in function <a class="reference internal" href="#doctest.debug" title="doctest.debug"><code class="xref py py-func docutils literal notranslate"><span class="pre">debug()</span></code></a> above.</p>
 | ||
| <p>Optional argument <em>globs</em> gives a dictionary to use as both local and global
 | ||
| execution context.  If not specified, or <code class="docutils literal notranslate"><span class="pre">None</span></code>, an empty dictionary is used.
 | ||
| If specified, a shallow copy of the dictionary is used.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <p>The <a class="reference internal" href="#doctest.DebugRunner" title="doctest.DebugRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DebugRunner</span></code></a> class, and the special exceptions it may raise, are of
 | ||
| most interest to testing framework authors, and will only be sketched here.  See
 | ||
| the source code, and especially <a class="reference internal" href="#doctest.DebugRunner" title="doctest.DebugRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DebugRunner</span></code></a>’s docstring (which is a
 | ||
| doctest!) for more details:</p>
 | ||
| <dl class="py class">
 | ||
| <dt class="sig sig-object py" id="doctest.DebugRunner">
 | ||
| <em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DebugRunner</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">checker</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optionflags</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DebugRunner" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>A subclass of <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> that raises an exception as soon as a
 | ||
| failure is encountered.  If an unexpected exception occurs, an
 | ||
| <a class="reference internal" href="#doctest.UnexpectedException" title="doctest.UnexpectedException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnexpectedException</span></code></a> exception is raised, containing the test, the
 | ||
| example, and the original exception.  If the output doesn’t match, then a
 | ||
| <a class="reference internal" href="#doctest.DocTestFailure" title="doctest.DocTestFailure"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DocTestFailure</span></code></a> exception is raised, containing the test, the example, and
 | ||
| the actual output.</p>
 | ||
| <p>For information about the constructor parameters and methods, see the
 | ||
| documentation for <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> in section <a class="reference internal" href="#doctest-advanced-api"><span class="std std-ref">Advanced API</span></a>.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <p>There are two exceptions that may be raised by <a class="reference internal" href="#doctest.DebugRunner" title="doctest.DebugRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DebugRunner</span></code></a> instances:</p>
 | ||
| <dl class="py exception">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestFailure">
 | ||
| <em class="property"><span class="k"><span class="pre">exception</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DocTestFailure</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">example</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">got</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.DocTestFailure" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>An exception raised by <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> to signal that a doctest example’s
 | ||
| actual output did not match its expected output. The constructor arguments are
 | ||
| used to initialize the attributes of the same names.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <p><a class="reference internal" href="#doctest.DocTestFailure" title="doctest.DocTestFailure"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DocTestFailure</span></code></a> defines the following attributes:</p>
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestFailure.test">
 | ||
| <span class="sig-prename descclassname"><span class="pre">DocTestFailure.</span></span><span class="sig-name descname"><span class="pre">test</span></span><a class="headerlink" href="#doctest.DocTestFailure.test" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>The <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object that was being run when the example failed.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestFailure.example">
 | ||
| <span class="sig-prename descclassname"><span class="pre">DocTestFailure.</span></span><span class="sig-name descname"><span class="pre">example</span></span><a class="headerlink" href="#doctest.DocTestFailure.example" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>The <a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a> that failed.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.DocTestFailure.got">
 | ||
| <span class="sig-prename descclassname"><span class="pre">DocTestFailure.</span></span><span class="sig-name descname"><span class="pre">got</span></span><a class="headerlink" href="#doctest.DocTestFailure.got" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>The example’s actual output.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py exception">
 | ||
| <dt class="sig sig-object py" id="doctest.UnexpectedException">
 | ||
| <em class="property"><span class="k"><span class="pre">exception</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">UnexpectedException</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">test</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">example</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exc_info</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.UnexpectedException" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>An exception raised by <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> to signal that a doctest
 | ||
| example raised an unexpected exception.  The constructor arguments are used
 | ||
| to initialize the attributes of the same names.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <p><a class="reference internal" href="#doctest.UnexpectedException" title="doctest.UnexpectedException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnexpectedException</span></code></a> defines the following attributes:</p>
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.UnexpectedException.test">
 | ||
| <span class="sig-prename descclassname"><span class="pre">UnexpectedException.</span></span><span class="sig-name descname"><span class="pre">test</span></span><a class="headerlink" href="#doctest.UnexpectedException.test" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>The <a class="reference internal" href="#doctest.DocTest" title="doctest.DocTest"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTest</span></code></a> object that was being run when the example failed.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.UnexpectedException.example">
 | ||
| <span class="sig-prename descclassname"><span class="pre">UnexpectedException.</span></span><span class="sig-name descname"><span class="pre">example</span></span><a class="headerlink" href="#doctest.UnexpectedException.example" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>The <a class="reference internal" href="#doctest.Example" title="doctest.Example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Example</span></code></a> that failed.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| <dl class="py attribute">
 | ||
| <dt class="sig sig-object py" id="doctest.UnexpectedException.exc_info">
 | ||
| <span class="sig-prename descclassname"><span class="pre">UnexpectedException.</span></span><span class="sig-name descname"><span class="pre">exc_info</span></span><a class="headerlink" href="#doctest.UnexpectedException.exc_info" title="Link to this definition">¶</a></dt>
 | ||
| <dd><p>A tuple containing information about the unexpected exception, as returned by
 | ||
| <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_info()</span></code></a>.</p>
 | ||
| </dd></dl>
 | ||
| 
 | ||
| </section>
 | ||
| <section id="soapbox">
 | ||
| <span id="doctest-soapbox"></span><h2>Soapbox<a class="headerlink" href="#soapbox" title="Link to this heading">¶</a></h2>
 | ||
| <p>As mentioned in the introduction, <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> has grown to have three primary
 | ||
| uses:</p>
 | ||
| <ol class="arabic simple">
 | ||
| <li><p>Checking examples in docstrings.</p></li>
 | ||
| <li><p>Regression testing.</p></li>
 | ||
| <li><p>Executable documentation / literate testing.</p></li>
 | ||
| </ol>
 | ||
| <p>These uses have different requirements, and it is important to distinguish them.
 | ||
| In particular, filling your docstrings with obscure test cases makes for bad
 | ||
| documentation.</p>
 | ||
| <p>When writing a docstring, choose docstring examples with care. There’s an art to
 | ||
| this that needs to be learned—it may not be natural at first.  Examples should
 | ||
| add genuine value to the documentation.  A good example can often be worth many
 | ||
| words. If done with care, the examples will be invaluable for your users, and
 | ||
| will pay back the time it takes to collect them many times over as the years go
 | ||
| by and things change.  I’m still amazed at how often one of my <a class="reference internal" href="#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a>
 | ||
| examples stops working after a “harmless” change.</p>
 | ||
| <p>Doctest also makes an excellent tool for regression testing, especially if you
 | ||
| don’t skimp on explanatory text.  By interleaving prose and examples, it becomes
 | ||
| much easier to keep track of what’s actually being tested, and why.  When a test
 | ||
| fails, good prose can make it much easier to figure out what the problem is, and
 | ||
| how it should be fixed.  It’s true that you could write extensive comments in
 | ||
| code-based testing, but few programmers do. Many have found that using doctest
 | ||
| approaches instead leads to much clearer tests.  Perhaps this is simply because
 | ||
| doctest makes writing prose a little easier than writing code, while writing
 | ||
| comments in code is a little harder.  I think it goes deeper than just that:
 | ||
| the natural attitude when writing a doctest-based test is that you want to
 | ||
| explain the fine points of your software, and illustrate them with examples.
 | ||
| This in turn naturally leads to test files that start with the simplest
 | ||
| features, and logically progress to complications and edge cases.  A coherent
 | ||
| narrative is the result, instead of a collection of isolated functions that test
 | ||
| isolated bits of functionality seemingly at random.  It’s a different attitude,
 | ||
| and produces different results, blurring the distinction between testing and
 | ||
| explaining.</p>
 | ||
| <p>Regression testing is best confined to dedicated objects or files.  There are
 | ||
| several options for organizing tests:</p>
 | ||
| <ul class="simple">
 | ||
| <li><p>Write text files containing test cases as interactive examples, and test the
 | ||
| files using <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a> or <a class="reference internal" href="#doctest.DocFileSuite" title="doctest.DocFileSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocFileSuite()</span></code></a>.  This is recommended,
 | ||
| although is easiest to do for new projects, designed from the start to use
 | ||
| doctest.</p></li>
 | ||
| <li><p>Define functions named <code class="docutils literal notranslate"><span class="pre">_regrtest_topic</span></code> that consist of single docstrings,
 | ||
| containing test cases for the named topics.  These functions can be included in
 | ||
| the same file as the module, or separated out into a separate test file.</p></li>
 | ||
| <li><p>Define a <code class="docutils literal notranslate"><span class="pre">__test__</span></code> dictionary mapping from regression test topics to
 | ||
| docstrings containing test cases.</p></li>
 | ||
| </ul>
 | ||
| <p>When you have placed your tests in a module, the module can itself be the test
 | ||
| runner.  When a test fails, you can arrange for your test runner to re-run only
 | ||
| the failing doctest while you debug the problem.  Here is a minimal example of
 | ||
| such a test runner:</p>
 | ||
| <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
 | ||
|     <span class="kn">import</span><span class="w"> </span><span class="nn">doctest</span>
 | ||
|     <span class="n">flags</span> <span class="o">=</span> <span class="n">doctest</span><span class="o">.</span><span class="n">REPORT_NDIFF</span><span class="o">|</span><span class="n">doctest</span><span class="o">.</span><span class="n">FAIL_FAST</span>
 | ||
|     <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span>
 | ||
|         <span class="n">name</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
 | ||
|         <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="nb">globals</span><span class="p">():</span>
 | ||
|             <span class="n">obj</span> <span class="o">=</span> <span class="nb">globals</span><span class="p">()[</span><span class="n">name</span><span class="p">]</span>
 | ||
|         <span class="k">else</span><span class="p">:</span>
 | ||
|             <span class="n">obj</span> <span class="o">=</span> <span class="n">__test__</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
 | ||
|         <span class="n">doctest</span><span class="o">.</span><span class="n">run_docstring_examples</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="nb">globals</span><span class="p">(),</span> <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">,</span>
 | ||
|                                        <span class="n">optionflags</span><span class="o">=</span><span class="n">flags</span><span class="p">)</span>
 | ||
|     <span class="k">else</span><span class="p">:</span>
 | ||
|         <span class="n">fail</span><span class="p">,</span> <span class="n">total</span> <span class="o">=</span> <span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">(</span><span class="n">optionflags</span><span class="o">=</span><span class="n">flags</span><span class="p">)</span>
 | ||
|         <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">fail</span><span class="si">}</span><span class="s2"> failures out of </span><span class="si">{</span><span class="n">total</span><span class="si">}</span><span class="s2"> tests"</span><span class="p">)</span>
 | ||
| </pre></div>
 | ||
| </div>
 | ||
| <p class="rubric">Footnotes</p>
 | ||
| <aside class="footnote-list brackets">
 | ||
| <aside class="footnote brackets" id="id2" role="doc-footnote">
 | ||
| <span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id1">1</a><span class="fn-bracket">]</span></span>
 | ||
| <p>Examples containing both expected output and an exception are not supported.
 | ||
| Trying to guess where one ends and the other begins is too error-prone, and that
 | ||
| also makes for a confusing test.</p>
 | ||
| </aside>
 | ||
| </aside>
 | ||
| </section>
 | ||
| </section>
 | ||
| 
 | ||
| 
 | ||
|             <div class="clearer"></div>
 | ||
|           </div>
 | ||
|         </div>
 | ||
|       </div>
 | ||
|       <div class="sphinxsidebar" role="navigation" aria-label="Main">
 | ||
|         <div class="sphinxsidebarwrapper">
 | ||
|   <div>
 | ||
|     <h3><a href="../contents.html">Table of Contents</a></h3>
 | ||
|     <ul>
 | ||
| <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>
 | ||
| <li><a class="reference internal" href="#simple-usage-checking-examples-in-docstrings">Simple Usage: Checking Examples in Docstrings</a></li>
 | ||
| <li><a class="reference internal" href="#simple-usage-checking-examples-in-a-text-file">Simple Usage: Checking Examples in a Text File</a></li>
 | ||
| <li><a class="reference internal" href="#command-line-usage">Command-line Usage</a></li>
 | ||
| <li><a class="reference internal" href="#how-it-works">How It Works</a><ul>
 | ||
| <li><a class="reference internal" href="#which-docstrings-are-examined">Which Docstrings Are Examined?</a></li>
 | ||
| <li><a class="reference internal" href="#how-are-docstring-examples-recognized">How are Docstring Examples Recognized?</a></li>
 | ||
| <li><a class="reference internal" href="#what-s-the-execution-context">What’s the Execution Context?</a></li>
 | ||
| <li><a class="reference internal" href="#what-about-exceptions">What About Exceptions?</a></li>
 | ||
| <li><a class="reference internal" href="#option-flags">Option Flags</a></li>
 | ||
| <li><a class="reference internal" href="#directives">Directives</a></li>
 | ||
| <li><a class="reference internal" href="#warnings">Warnings</a></li>
 | ||
| </ul>
 | ||
| </li>
 | ||
| <li><a class="reference internal" href="#basic-api">Basic API</a></li>
 | ||
| <li><a class="reference internal" href="#unittest-api">Unittest API</a></li>
 | ||
| <li><a class="reference internal" href="#advanced-api">Advanced API</a><ul>
 | ||
| <li><a class="reference internal" href="#doctest-objects">DocTest Objects</a></li>
 | ||
| <li><a class="reference internal" href="#example-objects">Example Objects</a></li>
 | ||
| <li><a class="reference internal" href="#doctestfinder-objects">DocTestFinder objects</a></li>
 | ||
| <li><a class="reference internal" href="#doctestparser-objects">DocTestParser objects</a></li>
 | ||
| <li><a class="reference internal" href="#testresults-objects">TestResults objects</a></li>
 | ||
| <li><a class="reference internal" href="#doctestrunner-objects">DocTestRunner objects</a></li>
 | ||
| <li><a class="reference internal" href="#outputchecker-objects">OutputChecker objects</a></li>
 | ||
| </ul>
 | ||
| </li>
 | ||
| <li><a class="reference internal" href="#debugging">Debugging</a></li>
 | ||
| <li><a class="reference internal" href="#soapbox">Soapbox</a></li>
 | ||
| </ul>
 | ||
| </li>
 | ||
| </ul>
 | ||
| 
 | ||
|   </div>
 | ||
|   <div>
 | ||
|     <h4>Previous topic</h4>
 | ||
|     <p class="topless"><a href="devmode.html"
 | ||
|                           title="previous chapter">Python Development Mode</a></p>
 | ||
|   </div>
 | ||
|   <div>
 | ||
|     <h4>Next topic</h4>
 | ||
|     <p class="topless"><a href="unittest.html"
 | ||
|                           title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code> — Unit testing framework</a></p>
 | ||
|   </div>
 | ||
|   <div role="note" aria-label="source link">
 | ||
|     <h3>This Page</h3>
 | ||
|     <ul class="this-page-menu">
 | ||
|       <li><a href="../bugs.html">Report a Bug</a></li>
 | ||
|       <li>
 | ||
|         <a href="https://github.com/python/cpython/blob/main/Doc/library/doctest.rst"
 | ||
|             rel="nofollow">Show Source
 | ||
|         </a>
 | ||
|       </li>
 | ||
|     </ul>
 | ||
|   </div>
 | ||
|         </div>
 | ||
| <div id="sidebarbutton" title="Collapse sidebar">
 | ||
| <span>«</span>
 | ||
| </div>
 | ||
| 
 | ||
|       </div>
 | ||
|       <div class="clearer"></div>
 | ||
|     </div>  
 | ||
|     <div class="related" role="navigation" aria-label="Related">
 | ||
|       <h3>Navigation</h3>
 | ||
|       <ul>
 | ||
|         <li class="right" style="margin-right: 10px">
 | ||
|           <a href="../genindex.html" title="General Index"
 | ||
|              >index</a></li>
 | ||
|         <li class="right" >
 | ||
|           <a href="../py-modindex.html" title="Python Module Index"
 | ||
|              >modules</a> |</li>
 | ||
|         <li class="right" >
 | ||
|           <a href="unittest.html" title="unittest — Unit testing framework"
 | ||
|              >next</a> |</li>
 | ||
|         <li class="right" >
 | ||
|           <a href="devmode.html" title="Python Development Mode"
 | ||
|              >previous</a> |</li>
 | ||
| 
 | ||
|           <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
 | ||
|           <li><a href="https://www.python.org/">Python</a> »</li>
 | ||
|           <li class="switchers">
 | ||
|             <div class="language_switcher_placeholder"></div>
 | ||
|             <div class="version_switcher_placeholder"></div>
 | ||
|           </li>
 | ||
|           <li>
 | ||
|               
 | ||
|           </li>
 | ||
|     <li id="cpython-language-and-version">
 | ||
|       <a href="../index.html">3.13.3 Documentation</a> »
 | ||
|     </li>
 | ||
| 
 | ||
|           <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
 | ||
|           <li class="nav-item nav-item-2"><a href="development.html" >Development Tools</a> »</li>
 | ||
|         <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>
 | ||
|                 <li class="right">
 | ||
|                     
 | ||
| 
 | ||
|     <div class="inline-search" role="search">
 | ||
|         <form class="inline-search" action="../search.html" method="get">
 | ||
|           <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
 | ||
|           <input type="submit" value="Go" />
 | ||
|         </form>
 | ||
|     </div>
 | ||
|                      |
 | ||
|                 </li>
 | ||
|             <li class="right">
 | ||
| <label class="theme-selector-label">
 | ||
|     Theme
 | ||
|     <select class="theme-selector" oninput="activateTheme(this.value)">
 | ||
|         <option value="auto" selected>Auto</option>
 | ||
|         <option value="light">Light</option>
 | ||
|         <option value="dark">Dark</option>
 | ||
|     </select>
 | ||
| </label> |</li>
 | ||
|             
 | ||
|       </ul>
 | ||
|     </div>  
 | ||
|     <div class="footer">
 | ||
|     © 
 | ||
|       <a href="../copyright.html">
 | ||
|     
 | ||
|     Copyright
 | ||
|     
 | ||
|       </a>
 | ||
|      2001-2025, Python Software Foundation.
 | ||
|     <br />
 | ||
|     This page is licensed under the Python Software Foundation License Version 2.
 | ||
|     <br />
 | ||
|     Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
 | ||
|     <br />
 | ||
|     
 | ||
|       See <a href="/license.html">History and License</a> for more information.<br />
 | ||
|     
 | ||
|     
 | ||
|     <br />
 | ||
| 
 | ||
|     The Python Software Foundation is a non-profit corporation.
 | ||
| <a href="https://www.python.org/psf/donations/">Please donate.</a>
 | ||
| <br />
 | ||
|     <br />
 | ||
|       Last updated on Apr 08, 2025 (14:33 UTC).
 | ||
|     
 | ||
|       <a href="/bugs.html">Found a bug</a>?
 | ||
|     
 | ||
|     <br />
 | ||
| 
 | ||
|     Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.
 | ||
|     </div>
 | ||
| 
 | ||
|   </body>
 | ||
| </html> |