~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.11-docs-html/library/doctest.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

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