-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdoctest.html
More file actions
2198 lines (2059 loc) · 210 KB
/
Copy pathdoctest.html
File metadata and controls
2198 lines (2059 loc) · 210 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="fa" data-content_root="../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta property="og:title" content="doctest --- Test interactive Python examples" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://docs.python.org/3/library/doctest.html" />
<meta property="og:site_name" content="Python documentation" />
<meta property="og:description" content="Source code: Lib/doctest.py The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown. Th..." />
<meta property="og:image" content="_static/og-image.png" />
<meta property="og:image:alt" content="Python documentation" />
<meta name="description" content="Source code: Lib/doctest.py The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown. Th..." />
<meta name="theme-color" content="#3776ab">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
<title>doctest --- Test interactive Python examples — مستندات Python3.14.6</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
<link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=4365c8fe" />
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=0fc419ee" />
<script src="../_static/documentation_options.js?v=e254dbbb"></script>
<script src="../_static/doctools.js?v=9bcbadda"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../_static/translations.js?v=5df48d09"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="جستجو در مستندات Python3.14.6"
href="../_static/opensearch.xml"/>
<link rel="author" title="درباره این مستندات" href="../about.html" />
<link rel="index" title="فهرست" href="../genindex.html" />
<link rel="search" title="جستجو" href="../search.html" />
<link rel="copyright" title="حق چاپ" href="../copyright.html" />
<link rel="next" title="unittest --- Unit testing framework" href="unittest.html" />
<link rel="prev" title="Python Development Mode" href="devmode.html" />
<link rel="canonical" href="https://docs.python.org/3/library/doctest.html">
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
<link rel="shortcut icon" type="image/png" href="../_static/py.svg">
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
<script type="text/javascript" src="../_static/search-focus.js"></script>
<script type="text/javascript" src="../_static/themetoggle.js"></script>
<script type="text/javascript" src="../_static/rtd_switcher.js"></script>
<meta name="readthedocs-addons-api-version" content="1">
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">
<nav class="nav-content" role="navigation">
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<span class="nav-items-wrapper">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Python logo">
</a>
<span class="version_switcher_placeholder"></span>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
</svg>
<input placeholder="جستجو سریع" aria-label="جستجو سریع" type="search" name="q">
<input type="submit" value="برو">
</form>
</span>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>
<div>
<h3><a href="../contents.html">فهرست عناوین</a></h3>
<ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> --- Test interactive Python examples</a><ul>
<li><a class="reference internal" href="#simple-usage-checking-examples-in-docstrings">Simple Usage: Checking Examples in Docstrings</a></li>
<li><a class="reference internal" href="#simple-usage-checking-examples-in-a-text-file">Simple Usage: Checking Examples in a Text File</a></li>
<li><a class="reference internal" href="#command-line-usage">Command-line Usage</a></li>
<li><a class="reference internal" href="#how-it-works">How It Works</a><ul>
<li><a class="reference internal" href="#which-docstrings-are-examined">Which Docstrings Are Examined?</a></li>
<li><a class="reference internal" href="#how-are-docstring-examples-recognized">How are Docstring Examples Recognized?</a></li>
<li><a class="reference internal" href="#what-s-the-execution-context">What's the Execution Context?</a></li>
<li><a class="reference internal" href="#what-about-exceptions">What About Exceptions?</a></li>
<li><a class="reference internal" href="#option-flags">Option Flags</a></li>
<li><a class="reference internal" href="#directives">Directives</a></li>
<li><a class="reference internal" href="#warnings">Warnings</a></li>
</ul>
</li>
<li><a class="reference internal" href="#basic-api">Basic API</a></li>
<li><a class="reference internal" href="#unittest-api">Unittest API</a></li>
<li><a class="reference internal" href="#advanced-api">Advanced API</a><ul>
<li><a class="reference internal" href="#doctest-objects">DocTest Objects</a></li>
<li><a class="reference internal" href="#example-objects">Example Objects</a></li>
<li><a class="reference internal" href="#doctestfinder-objects">DocTestFinder objects</a></li>
<li><a class="reference internal" href="#doctestparser-objects">DocTestParser objects</a></li>
<li><a class="reference internal" href="#testresults-objects">TestResults objects</a></li>
<li><a class="reference internal" href="#doctestrunner-objects">DocTestRunner objects</a></li>
<li><a class="reference internal" href="#outputchecker-objects">OutputChecker objects</a></li>
</ul>
</li>
<li><a class="reference internal" href="#debugging">Debugging</a></li>
<li><a class="reference internal" href="#soapbox">Soapbox</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>موضوع قبلی</h4>
<p class="topless"><a href="devmode.html"
title="فصل قبلی">Python Development Mode</a></p>
</div>
<div>
<h4>موضوع بعدی</h4>
<p class="topless"><a href="unittest.html"
title="فصل بعدی"><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code> --- Unit testing framework</a></p>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const title = document.querySelector('meta[property="og:title"]').content;
const elements = document.querySelectorAll('.improvepage');
const pageurl = window.location.href.split('?')[0];
elements.forEach(element => {
const url = new URL(element.href.split('?')[0].replace("-nojs", ""));
url.searchParams.set('pagetitle', title);
url.searchParams.set('pageurl', pageurl);
url.searchParams.set('pagesource', "library/doctest.rst");
element.href = url.toString();
});
});
</script>
<div role="note" aria-label="source link">
<h3>این صفحه</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">گزارش یک اشکال</a></li>
<li><a class="improvepage" href="../improve-page-nojs.html">بهبود این صفحه</a></li>
<li>
<a href="https://github.com/python/cpython/blob/main/Doc/library/doctest.rst?plain=1"
rel="nofollow">نمایش منبع
</a>
</li>
<li>
<a href="https://github.com/python/python-docs-fa/blob/3.14/library/doctest.po?plain=1"
rel="nofollow">نمایش منبع ترجمه</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="related" role="navigation" aria-label="Related">
<h3>ناوبری</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="فهرست کلی"
accesskey="I">فهرست</a></li>
<li class="right" >
<a href="../py-modindex.html" title="نمایه ی ماژول های پایتون"
>ماژول ها</a> |</li>
<li class="right" >
<a href="unittest.html" title="unittest --- Unit testing framework"
accesskey="N">بعدی</a> |</li>
<li class="right" >
<a href="devmode.html" title="Python Development Mode"
accesskey="P">قبلی</a> |</li>
<li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.14.6 Documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
<li class="nav-item nav-item-2"><a href="development.html" accesskey="U">Development Tools</a> »</li>
<li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> --- Test interactive Python examples</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="جستجو سریع" aria-label="جستجو سریع" type="search" name="q" id="search-box">
<input type="submit" value="برو">
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label> |</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="module-doctest">
<span id="doctest-test-interactive-python-examples"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> --- Test interactive Python examples<a class="headerlink" href="#module-doctest" title="Link to this heading">¶</a></h1>
<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/3.14/Lib/doctest.py">Lib/doctest.py</a></p>
<hr class="docutils" />
<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> module searches for pieces of text that look like interactive
Python sessions, and then executes those sessions to verify that they work
exactly as shown. There are several common ways to use doctest:</p>
<ul class="simple">
<li><p>To check that a module's docstrings are up-to-date by verifying that all
interactive examples still work as documented.</p></li>
<li><p>To perform regression testing by verifying that interactive examples from a
test file or a test object work as expected.</p></li>
<li><p>To write tutorial documentation for a package, liberally illustrated with
input-output examples. Depending on whether the examples or the expository text
are emphasized, this has the flavor of "literate testing" or "executable
documentation".</p></li>
</ul>
<p>Here's a complete but small example module:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
<span class="sd">This is the "example" module.</span>
<span class="sd">The example module supplies one function, factorial(). For example,</span>
<span class="sd">>>> factorial(5)</span>
<span class="sd">120</span>
<span class="sd">"""</span>
<span class="k">def</span><span class="w"> </span><span class="nf">factorial</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""Return the factorial of n, an exact integer >= 0.</span>
<span class="sd"> >>> [factorial(n) for n in range(6)]</span>
<span class="sd"> [1, 1, 2, 6, 24, 120]</span>
<span class="sd"> >>> factorial(30)</span>
<span class="sd"> 265252859812191058636308480000000</span>
<span class="sd"> >>> factorial(-1)</span>
<span class="sd"> Traceback (most recent call last):</span>
<span class="sd"> ...</span>
<span class="sd"> ValueError: n must be >= 0</span>
<span class="sd"> Factorials of floats are OK, but the float must be an exact integer:</span>
<span class="sd"> >>> factorial(30.1)</span>
<span class="sd"> Traceback (most recent call last):</span>
<span class="sd"> ...</span>
<span class="sd"> ValueError: n must be exact integer</span>
<span class="sd"> >>> factorial(30.0)</span>
<span class="sd"> 265252859812191058636308480000000</span>
<span class="sd"> It must also not be ridiculously large:</span>
<span class="sd"> >>> factorial(1e100)</span>
<span class="sd"> Traceback (most recent call last):</span>
<span class="sd"> ...</span>
<span class="sd"> OverflowError: n too large</span>
<span class="sd"> """</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">math</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">n</span> <span class="o">>=</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"n must be >= 0"</span><span class="p">)</span>
<span class="k">if</span> <span class="n">math</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="o">!=</span> <span class="n">n</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"n must be exact integer"</span><span class="p">)</span>
<span class="k">if</span> <span class="n">n</span><span class="o">+</span><span class="mi">1</span> <span class="o">==</span> <span class="n">n</span><span class="p">:</span> <span class="c1"># catch a value like 1e300</span>
<span class="k">raise</span> <span class="ne">OverflowError</span><span class="p">(</span><span class="s2">"n too large"</span><span class="p">)</span>
<span class="n">result</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">factor</span> <span class="o">=</span> <span class="mi">2</span>
<span class="k">while</span> <span class="n">factor</span> <span class="o"><=</span> <span class="n">n</span><span class="p">:</span>
<span class="n">result</span> <span class="o">*=</span> <span class="n">factor</span>
<span class="n">factor</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">return</span> <span class="n">result</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">doctest</span>
<span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">()</span>
</pre></div>
</div>
<p>If you run <code class="file docutils literal notranslate"><span class="pre">example.py</span></code> directly from the command line, <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code>
works its magic:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>example.py
<span class="gp">$</span>
</pre></div>
</div>
<p>There's no output! That's normal, and it means all the examples worked. Pass
<code class="docutils literal notranslate"><span class="pre">-v</span></code> to the script, and <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> prints a detailed log of what
it's trying, and prints a summary at the end:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>example.py<span class="w"> </span>-v
<span class="go">Trying:</span>
<span class="go"> factorial(5)</span>
<span class="go">Expecting:</span>
<span class="go"> 120</span>
<span class="go">ok</span>
<span class="go">Trying:</span>
<span class="go"> [factorial(n) for n in range(6)]</span>
<span class="go">Expecting:</span>
<span class="go"> [1, 1, 2, 6, 24, 120]</span>
<span class="go">ok</span>
</pre></div>
</div>
<p>And so on, eventually ending with:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Trying:
factorial(1e100)
Expecting:
Traceback (most recent call last):
...
OverflowError: n too large
ok
2 items passed all tests:
1 test in __main__
6 tests in __main__.factorial
7 tests in 2 items.
7 passed.
Test passed.
$
</pre></div>
</div>
<p>That's all you need to know to start making productive use of <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code>!
Jump in. The following sections provide full details. Note that there are many
examples of doctests in the standard Python test suite and libraries.
Especially useful examples can be found in the standard test file
<code class="file docutils literal notranslate"><span class="pre">Lib/test/test_doctest/test_doctest.py</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.13: </span>Output is colorized by default and can be
<a class="reference internal" href="../using/cmdline.html#using-on-controlling-color"><span class="std std-ref">controlled using environment variables</span></a>.</p>
</div>
<section id="simple-usage-checking-examples-in-docstrings">
<span id="doctest-simple-testmod"></span><h2>Simple Usage: Checking Examples in Docstrings<a class="headerlink" href="#simple-usage-checking-examples-in-docstrings" title="Link to this heading">¶</a></h2>
<p>The simplest way to start using doctest (but not necessarily the way you'll
continue to do it) is to end each module <code class="xref py py-mod docutils literal notranslate"><span class="pre">M</span></code> with:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">doctest</span>
<span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">()</span>
</pre></div>
</div>
<p><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> then examines docstrings in module <code class="xref py py-mod docutils literal notranslate"><span class="pre">M</span></code>.</p>
<p>Running the module as a script causes the examples in the docstrings to get
executed and verified:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">M</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
<p>This won't display anything unless an example fails, in which case the failing
example(s) and the cause(s) of the failure(s) are printed to stdout, and the
final line of output is <code class="docutils literal notranslate"><span class="pre">***Test</span> <span class="pre">Failed***</span> <span class="pre">N</span> <span class="pre">failures.</span></code>, where <em>N</em> is the
number of examples that failed.</p>
<p>Run it with the <code class="docutils literal notranslate"><span class="pre">-v</span></code> switch instead:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">M</span><span class="o">.</span><span class="n">py</span> <span class="o">-</span><span class="n">v</span>
</pre></div>
</div>
<p>and a detailed report of all examples tried is printed to standard output, along
with assorted summaries at the end.</p>
<p>You can force verbose mode by passing <code class="docutils literal notranslate"><span class="pre">verbose=True</span></code> to <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>, or
prohibit it by passing <code class="docutils literal notranslate"><span class="pre">verbose=False</span></code>. In either of those cases,
<a class="reference internal" href="sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> is not examined by <code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code> (so passing <code class="docutils literal notranslate"><span class="pre">-v</span></code> or not
has no effect).</p>
<p>There is also a command line shortcut for running <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>, see section
<a class="reference internal" href="#doctest-cli"><span class="std std-ref">Command-line Usage</span></a>.</p>
<p>For more information on <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>, see section <a class="reference internal" href="#doctest-basic-api"><span class="std std-ref">Basic API</span></a>.</p>
</section>
<section id="simple-usage-checking-examples-in-a-text-file">
<span id="doctest-simple-testfile"></span><h2>Simple Usage: Checking Examples in a Text File<a class="headerlink" href="#simple-usage-checking-examples-in-a-text-file" title="Link to this heading">¶</a></h2>
<p>Another simple application of doctest is testing interactive examples in a text
file. This can be done with the <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a> function:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">doctest</span>
<span class="n">doctest</span><span class="o">.</span><span class="n">testfile</span><span class="p">(</span><span class="s2">"example.txt"</span><span class="p">)</span>
</pre></div>
</div>
<p>That short script executes and verifies any interactive Python examples
contained in the file <code class="file docutils literal notranslate"><span class="pre">example.txt</span></code>. The file content is treated as if it
were a single giant docstring; the file doesn't need to contain a Python
program! For example, perhaps <code class="file docutils literal notranslate"><span class="pre">example.txt</span></code> contains this:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>The ``example`` module
======================
Using ``factorial``
-------------------
This is an example text file in reStructuredText format. First import
``factorial`` from the ``example`` module:
>>> from example import factorial
Now use it:
>>> factorial(6)
120
</pre></div>
</div>
<p>Running <code class="docutils literal notranslate"><span class="pre">doctest.testfile("example.txt")</span></code> then finds the error in this
documentation:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">File</span> <span class="s2">"./example.txt"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">14</span><span class="p">,</span> <span class="ow">in</span> <span class="n">example</span><span class="o">.</span><span class="n">txt</span>
<span class="n">Failed</span> <span class="n">example</span><span class="p">:</span>
<span class="n">factorial</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
<span class="n">Expected</span><span class="p">:</span>
<span class="mi">120</span>
<span class="n">Got</span><span class="p">:</span>
<span class="mi">720</span>
</pre></div>
</div>
<p>As with <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>, <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a> won't display anything unless an
example fails. If an example does fail, then the failing example(s) and the
cause(s) of the failure(s) are printed to stdout, using the same format as
<code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code>.</p>
<p>By default, <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a> looks for files in the calling module's directory.
See section <a class="reference internal" href="#doctest-basic-api"><span class="std std-ref">Basic API</span></a> for a description of the optional arguments
that can be used to tell it to look for files in other locations.</p>
<p>Like <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a>, <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a>'s verbosity can be set with the
<code class="docutils literal notranslate"><span class="pre">-v</span></code> command-line switch or with the optional keyword argument
<em>verbose</em>.</p>
<p>There is also a command line shortcut for running <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a>, see section
<a class="reference internal" href="#doctest-cli"><span class="std std-ref">Command-line Usage</span></a>.</p>
<p>For more information on <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a>, see section <a class="reference internal" href="#doctest-basic-api"><span class="std std-ref">Basic API</span></a>.</p>
</section>
<section id="command-line-usage">
<span id="doctest-cli"></span><h2>Command-line Usage<a class="headerlink" href="#command-line-usage" title="Link to this heading">¶</a></h2>
<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> module can be invoked as a script from the command line:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>python<span class="w"> </span>-m<span class="w"> </span>doctest<span class="w"> </span><span class="o">[</span>-v<span class="o">]</span><span class="w"> </span><span class="o">[</span>-o<span class="w"> </span>OPTION<span class="o">]</span><span class="w"> </span><span class="o">[</span>-f<span class="o">]</span><span class="w"> </span>file<span class="w"> </span><span class="o">[</span>file<span class="w"> </span>...<span class="o">]</span>
</pre></div>
</div>
<dl class="std option">
<dt class="sig sig-object std" id="cmdoption-doctest-v">
<span id="cmdoption-doctest-verbose"></span><span class="sig-name descname"><span class="pre">-v</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--verbose</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-doctest-v" title="Link to this definition">¶</a></dt>
<dd><p>Detailed report of all examples tried is printed to standard output,
along with assorted summaries at the end:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">doctest</span> <span class="o">-</span><span class="n">v</span> <span class="n">example</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
<p>This will import <code class="file docutils literal notranslate"><span class="pre">example.py</span></code> as a standalone module and run
<a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a> on it. Note that this may not work correctly if the
file is part of a package and imports other submodules from that package.</p>
<p>If the file name does not end with <code class="file docutils literal notranslate"><span class="pre">.py</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> infers
that it must be run with <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a> instead:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">doctest</span> <span class="o">-</span><span class="n">v</span> <span class="n">example</span><span class="o">.</span><span class="n">txt</span>
</pre></div>
</div>
</dd></dl>
<dl class="std option">
<dt class="sig sig-object std" id="cmdoption-doctest-o">
<span id="cmdoption-doctest-option"></span><span class="sig-name descname"><span class="pre">-o</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--option</span></span><span class="sig-prename descclassname"> <span class="pre"><option></span></span><a class="headerlink" href="#cmdoption-doctest-o" title="Link to this definition">¶</a></dt>
<dd><p>Option flags control various aspects of doctest's behavior, see section
<a class="reference internal" href="#doctest-options"><span class="std std-ref">Option Flags</span></a>.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.4.</span></p>
</div>
</dd></dl>
<dl class="std option">
<dt class="sig sig-object std" id="cmdoption-doctest-f">
<span id="cmdoption-doctest-fail-fast"></span><span class="sig-name descname"><span class="pre">-f</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--fail-fast</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-doctest-f" title="Link to this definition">¶</a></dt>
<dd><p>This is shorthand for <code class="docutils literal notranslate"><span class="pre">-o</span> <span class="pre">FAIL_FAST</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.4.</span></p>
</div>
</dd></dl>
</section>
<section id="how-it-works">
<span id="doctest-how-it-works"></span><h2>How It Works<a class="headerlink" href="#how-it-works" title="Link to this heading">¶</a></h2>
<p>This section examines in detail how doctest works: which docstrings it looks at,
how it finds interactive examples, what execution context it uses, how it
handles exceptions, and how option flags can be used to control its behavior.
This is the information that you need to know to write doctest examples; for
information about actually running doctest on these examples, see the following
sections.</p>
<section id="which-docstrings-are-examined">
<span id="doctest-which-docstrings"></span><h3>Which Docstrings Are Examined?<a class="headerlink" href="#which-docstrings-are-examined" title="Link to this heading">¶</a></h3>
<p>The module docstring, and all function, class and method docstrings are
searched. Objects imported into the module are not searched.</p>
<p id="module.__test__">In addition, there are cases when you want tests to be part of a module but not part
of the help text, which requires that the tests not be included in the docstring.
Doctest looks for a module-level variable called <code class="docutils literal notranslate"><span class="pre">__test__</span></code> and uses it to locate other
tests. If <code class="docutils literal notranslate"><span class="pre">M.__test__</span></code> exists, it must be a dict, and each
entry maps a (string) name to a function object, class object, or string.
Function and class object docstrings found from <code class="docutils literal notranslate"><span class="pre">M.__test__</span></code> are searched, and
strings are treated as if they were docstrings. In output, a key <code class="docutils literal notranslate"><span class="pre">K</span></code> in
<code class="docutils literal notranslate"><span class="pre">M.__test__</span></code> appears with name <code class="docutils literal notranslate"><span class="pre">M.__test__.K</span></code>.</p>
<p>For example, place this block of code at the top of <code class="file docutils literal notranslate"><span class="pre">example.py</span></code>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">__test__</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">'numbers'</span><span class="p">:</span> <span class="s2">"""</span>
<span class="s2">>>> factorial(6)</span>
<span class="s2">720</span>
<span class="s2">>>> [factorial(n) for n in range(6)]</span>
<span class="s2">[1, 1, 2, 6, 24, 120]</span>
<span class="s2">"""</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The value of <code class="docutils literal notranslate"><span class="pre">example.__test__["numbers"]</span></code> will be treated as a
docstring and all the tests inside it will be run. It is
important to note that the value can be mapped to a function,
class object, or module; if so, <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code>
searches them recursively for docstrings, which are then scanned for tests.</p>
<p>Any classes found are recursively searched similarly, to test docstrings in
their contained methods and nested classes.</p>
<div class="admonition note">
<p class="admonition-title">توجه</p>
<p><code class="docutils literal notranslate"><span class="pre">doctest</span></code> can only automatically discover classes and functions that are
defined at the module level or inside other classes.</p>
<p>Since nested classes and functions only exist when an outer function
is called, they cannot be discovered. Define them outside to make them visible.</p>
</div>
</section>
<section id="how-are-docstring-examples-recognized">
<span id="doctest-finding-examples"></span><h3>How are Docstring Examples Recognized?<a class="headerlink" href="#how-are-docstring-examples-recognized" title="Link to this heading">¶</a></h3>
<p>In most cases a copy-and-paste of an interactive console session works fine,
but doctest isn't trying to do an exact emulation of any specific Python shell.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="c1"># comments are ignored</span>
<span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="mi">12</span>
<span class="gp">>>> </span><span class="n">x</span>
<span class="go">12</span>
<span class="gp">>>> </span><span class="k">if</span> <span class="n">x</span> <span class="o">==</span> <span class="mi">13</span><span class="p">:</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">"yes"</span><span class="p">)</span>
<span class="gp">... </span><span class="k">else</span><span class="p">:</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">"no"</span><span class="p">)</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">"NO"</span><span class="p">)</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">"NO!!!"</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">no</span>
<span class="go">NO</span>
<span class="go">NO!!!</span>
<span class="gp">>>></span>
</pre></div>
</div>
<p id="index-0">Any expected output must immediately follow the final <code class="docutils literal notranslate"><span class="pre">'>>></span> <span class="pre">'</span></code> or <code class="docutils literal notranslate"><span class="pre">'...</span> <span class="pre">'</span></code>
line containing the code, and the expected output (if any) extends to the next
<code class="docutils literal notranslate"><span class="pre">'>>></span> <span class="pre">'</span></code> or all-whitespace line.</p>
<p>The fine print:</p>
<ul>
<li><p>Expected output cannot contain an all-whitespace line, since such a line is
taken to signal the end of expected output. If expected output does contain a
blank line, put <code class="docutils literal notranslate"><span class="pre"><BLANKLINE></span></code> in your doctest example each place a blank line
is expected.</p></li>
<li><p>All hard tab characters are expanded to spaces, using 8-column tab stops.
Tabs in output generated by the tested code are not modified. Because any
hard tabs in the sample output <em>are</em> expanded, this means that if the code
output includes hard tabs, the only way the doctest can pass is if the
<a class="reference internal" href="#doctest.NORMALIZE_WHITESPACE" title="doctest.NORMALIZE_WHITESPACE"><code class="xref py py-const docutils literal notranslate"><span class="pre">NORMALIZE_WHITESPACE</span></code></a> option or <a class="reference internal" href="#doctest-directives"><span class="std std-ref">directive</span></a>
is in effect.
Alternatively, the test can be rewritten to capture the output and compare it
to an expected value as part of the test. This handling of tabs in the
source was arrived at through trial and error, and has proven to be the least
error prone way of handling them. It is possible to use a different
algorithm for handling tabs by writing a custom <a class="reference internal" href="#doctest.DocTestParser" title="doctest.DocTestParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestParser</span></code></a> class.</p></li>
<li><p>Output to stdout is captured, but not output to stderr (exception tracebacks
are captured via a different means).</p></li>
<li><p>If you continue a line via backslashing in an interactive session, or for any
other reason use a backslash, you should use a raw docstring, which will
preserve your backslashes exactly as you type them:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span><span class="w"> </span><span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="gp">... </span><span class="w"> </span><span class="sa">r</span><span class="sd">'''Backslashes in a raw docstring: m\n'''</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="vm">__doc__</span><span class="p">)</span>
<span class="go">Backslashes in a raw docstring: m\n</span>
</pre></div>
</div>
<p>Otherwise, the backslash will be interpreted as part of the string. For example,
the <code class="docutils literal notranslate"><span class="pre">\n</span></code> above would be interpreted as a newline character. Alternatively, you
can double each backslash in the doctest version (and not use a raw string):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span><span class="w"> </span><span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="gp">... </span><span class="w"> </span><span class="sd">'''Backslashes in a raw docstring: m\\n'''</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="vm">__doc__</span><span class="p">)</span>
<span class="go">Backslashes in a raw docstring: m\n</span>
</pre></div>
</div>
</li>
<li><p>The starting column doesn't matter:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">assert</span> <span class="s2">"Easy!"</span>
<span class="go"> >>> import math</span>
<span class="go"> >>> math.floor(1.9)</span>
<span class="go"> 1</span>
</pre></div>
</div>
<p>and as many leading whitespace characters are stripped from the expected output
as appeared in the initial <code class="docutils literal notranslate"><span class="pre">'>>></span> <span class="pre">'</span></code> line that started the example.</p>
</li>
</ul>
</section>
<section id="what-s-the-execution-context">
<span id="doctest-execution-context"></span><h3>What's the Execution Context?<a class="headerlink" href="#what-s-the-execution-context" title="Link to this heading">¶</a></h3>
<p>By default, each time <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> finds a docstring to test, it uses a
<em>shallow copy</em> of <code class="xref py py-mod docutils literal notranslate"><span class="pre">M</span></code>'s globals, so that running tests doesn't change the
module's real globals, and so that one test in <code class="xref py py-mod docutils literal notranslate"><span class="pre">M</span></code> can't leave behind
crumbs that accidentally allow another test to work. This means examples can
freely use any names defined at top-level in <code class="xref py py-mod docutils literal notranslate"><span class="pre">M</span></code>, and names defined earlier
in the docstring being run. Examples cannot see names defined in other
docstrings.</p>
<p>You can force use of your own dict as the execution context by passing
<code class="docutils literal notranslate"><span class="pre">globs=your_dict</span></code> to <a class="reference internal" href="#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">testmod()</span></code></a> or <a class="reference internal" href="#doctest.testfile" title="doctest.testfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">testfile()</span></code></a> instead.</p>
</section>
<section id="what-about-exceptions">
<span id="doctest-exceptions"></span><h3>What About Exceptions?<a class="headerlink" href="#what-about-exceptions" title="Link to this heading">¶</a></h3>
<p>No problem, provided that the traceback is the only output produced by the
example: just paste in the traceback. <a class="footnote-reference brackets" href="#id2" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> Since tracebacks contain details
that are likely to change rapidly (for example, exact file paths and line
numbers), this is one case where doctest works hard to be flexible in what it
accepts.</p>
<p>Simple example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
<span class="gr">ValueError</span>: <span class="n">list.remove(x): x not in list</span>
</pre></div>
</div>
<p>That doctest succeeds if <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised, with the <code class="docutils literal notranslate"><span class="pre">list.remove(x):</span>
<span class="pre">x</span> <span class="pre">not</span> <span class="pre">in</span> <span class="pre">list</span></code> detail as shown.</p>
<p>The expected output for an exception must start with a traceback header, which
may be either of the following two lines, indented the same as the first line of
the example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">innermost</span> <span class="n">last</span><span class="p">):</span>
</pre></div>
</div>
<p>The traceback header is followed by an optional traceback stack, whose contents
are ignored by doctest. The traceback stack is typically omitted, or copied
verbatim from an interactive session.</p>
<p>The traceback stack is followed by the most interesting part: the line(s)
containing the exception type and detail. This is usually the last line of a
traceback, but can extend across multiple lines if the exception has a
multi-line detail:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'multi</span><span class="se">\n</span><span class="s1"> line</span><span class="se">\n</span><span class="s1">detail'</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
<span class="gr">ValueError</span>: <span class="n">multi</span>
<span class="x"> line</span>
<span class="x">detail</span>
</pre></div>
</div>
<p>The last three lines (starting with <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>) are compared against the
exception's type and detail, and the rest are ignored.</p>
<p>Best practice is to omit the traceback stack, unless it adds significant
documentation value to the example. So the last example is probably better as:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">'multi</span><span class="se">\n</span><span class="s1"> line</span><span class="se">\n</span><span class="s1">detail'</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="w"> </span><span class="o">...</span>
<span class="gr">ValueError</span>: <span class="n">multi</span>
<span class="x"> line</span>
<span class="x">detail</span>
</pre></div>
</div>
<p>Note that tracebacks are treated very specially. In particular, in the
rewritten example, the use of <code class="docutils literal notranslate"><span class="pre">...</span></code> is independent of doctest's
<a class="reference internal" href="#doctest.ELLIPSIS" title="doctest.ELLIPSIS"><code class="xref py py-const docutils literal notranslate"><span class="pre">ELLIPSIS</span></code></a> option. The ellipsis in that example could be left out, or
could just as well be three (or three hundred) commas or digits, or an indented
transcript of a Monty Python skit.</p>
<p>Some details you should read once, but won't need to remember:</p>
<ul class="simple">
<li><p>Doctest can't guess whether your expected output came from an exception
traceback or from ordinary printing. So, e.g., an example that expects
<code class="docutils literal notranslate"><span class="pre">ValueError:</span> <span class="pre">42</span> <span class="pre">is</span> <span class="pre">prime</span></code> will pass whether <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is actually
raised or if the example merely prints that traceback text. In practice,
ordinary output rarely begins with a traceback header line, so this doesn't
create real problems.</p></li>
<li><p>Each line of the traceback stack (if present) must be indented further than
the first line of the example, <em>or</em> start with a non-alphanumeric character.
The first line following the traceback header indented the same and starting
with an alphanumeric is taken to be the start of the exception detail. Of
course this does the right thing for genuine tracebacks.</p></li>
<li><p>When the <a class="reference internal" href="#doctest.IGNORE_EXCEPTION_DETAIL" title="doctest.IGNORE_EXCEPTION_DETAIL"><code class="xref py py-const docutils literal notranslate"><span class="pre">IGNORE_EXCEPTION_DETAIL</span></code></a> doctest option is specified,
everything following the leftmost colon and any module information in the
exception name is ignored.</p></li>
<li><p>The interactive shell omits the traceback header line for some
<a class="reference internal" href="exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a>s. But doctest uses the traceback header line to
distinguish exceptions from non-exceptions. So in the rare case where you need
to test a <code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code> that omits the traceback header, you will need to
manually add the traceback header line to your test example.</p></li>
</ul>
<ul id="index-1">
<li><p>For some exceptions, Python displays the position of the error using <code class="docutils literal notranslate"><span class="pre">^</span></code>
markers and tildes:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mi">1</span> <span class="o">+</span> <span class="kc">None</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
<span class="w"> </span><span class="mi">1</span> <span class="o">+</span> <span class="kc">None</span>
<span class="w"> </span><span class="pm">~~^~~~~~</span>
<span class="gr">TypeError</span>: <span class="n">unsupported operand type(s) for +: 'int' and 'NoneType'</span>
</pre></div>
</div>
<p>Since the lines showing the position of the error come before the exception type
and detail, they are not checked by doctest. For example, the following test
would pass, even though it puts the <code class="docutils literal notranslate"><span class="pre">^</span></code> marker in the wrong location:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mi">1</span> <span class="o">+</span> <span class="kc">None</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
<span class="w"> </span><span class="mi">1</span> <span class="o">+</span> <span class="kc">None</span>
<span class="w"> </span><span class="pm">^~~~~~~~</span>
<span class="gr">TypeError</span>: <span class="n">unsupported operand type(s) for +: 'int' and 'NoneType'</span>
</pre></div>
</div>
</li>
</ul>
</section>
<section id="option-flags">
<span id="doctest-options"></span><span id="option-flags-and-directives"></span><h3>Option Flags<a class="headerlink" href="#option-flags" title="Link to this heading">¶</a></h3>
<p>A number of option flags control various aspects of doctest's behavior.
Symbolic names for the flags are supplied as module constants, which can be
<a class="reference internal" href="../reference/expressions.html#bitwise"><span class="std std-ref">bitwise ORed</span></a> together and passed to various functions.
The names can also be used in <a class="reference internal" href="#doctest-directives"><span class="std std-ref">doctest directives</span></a>,
and may be passed to the doctest command line interface via the <code class="docutils literal notranslate"><span class="pre">-o</span></code> option.</p>
<p>The first group of options define test semantics, controlling aspects of how
doctest decides whether actual output matches an example's expected output:</p>
<dl class="py data">
<dt class="sig sig-object py" id="doctest.DONT_ACCEPT_TRUE_FOR_1">
<span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DONT_ACCEPT_TRUE_FOR_1</span></span><a class="headerlink" href="#doctest.DONT_ACCEPT_TRUE_FOR_1" title="Link to this definition">¶</a></dt>
<dd><p>By default, if an expected output block contains just <code class="docutils literal notranslate"><span class="pre">1</span></code>, an actual output
block containing just <code class="docutils literal notranslate"><span class="pre">1</span></code> or just <code class="docutils literal notranslate"><span class="pre">True</span></code> is considered to be a match, and
similarly for <code class="docutils literal notranslate"><span class="pre">0</span></code> versus <code class="docutils literal notranslate"><span class="pre">False</span></code>. When <a class="reference internal" href="#doctest.DONT_ACCEPT_TRUE_FOR_1" title="doctest.DONT_ACCEPT_TRUE_FOR_1"><code class="xref py py-const docutils literal notranslate"><span class="pre">DONT_ACCEPT_TRUE_FOR_1</span></code></a> is
specified, neither substitution is allowed. The default behavior caters to that
Python changed the return type of many functions from integer to boolean;
doctests expecting "little integer" output still work in these cases. This
option will probably go away, but not for several years.</p>
</dd></dl>
<dl class="py data" id="index-2">
<dt class="sig sig-object py" id="doctest.DONT_ACCEPT_BLANKLINE">
<span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">DONT_ACCEPT_BLANKLINE</span></span><a class="headerlink" href="#doctest.DONT_ACCEPT_BLANKLINE" title="Link to this definition">¶</a></dt>
<dd><p>By default, if an expected output block contains a line containing only the
string <code class="docutils literal notranslate"><span class="pre"><BLANKLINE></span></code>, then that line will match a blank line in the actual
output. Because a genuinely blank line delimits the expected output, this is
the only way to communicate that a blank line is expected. When
<a class="reference internal" href="#doctest.DONT_ACCEPT_BLANKLINE" title="doctest.DONT_ACCEPT_BLANKLINE"><code class="xref py py-const docutils literal notranslate"><span class="pre">DONT_ACCEPT_BLANKLINE</span></code></a> is specified, this substitution is not allowed.</p>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="doctest.NORMALIZE_WHITESPACE">
<span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">NORMALIZE_WHITESPACE</span></span><a class="headerlink" href="#doctest.NORMALIZE_WHITESPACE" title="Link to this definition">¶</a></dt>
<dd><p>When specified, all sequences of whitespace (blanks and newlines) are treated as
equal. Any sequence of whitespace within the expected output will match any
sequence of whitespace within the actual output. By default, whitespace must
match exactly. <a class="reference internal" href="#doctest.NORMALIZE_WHITESPACE" title="doctest.NORMALIZE_WHITESPACE"><code class="xref py py-const docutils literal notranslate"><span class="pre">NORMALIZE_WHITESPACE</span></code></a> is especially useful when a line of
expected output is very long, and you want to wrap it across multiple lines in
your source.</p>
</dd></dl>
<dl class="py data" id="index-3">
<dt class="sig sig-object py" id="doctest.ELLIPSIS">
<span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">ELLIPSIS</span></span><a class="headerlink" href="#doctest.ELLIPSIS" title="Link to this definition">¶</a></dt>
<dd><p>When specified, an ellipsis marker (<code class="docutils literal notranslate"><span class="pre">...</span></code>) in the expected output can match
any substring in the actual output. This includes substrings that span line
boundaries, and empty substrings, so it's best to keep usage of this simple.
Complicated uses can lead to the same kinds of "oops, it matched too much!"
surprises that <code class="docutils literal notranslate"><span class="pre">.*</span></code> is prone to in regular expressions.</p>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="doctest.IGNORE_EXCEPTION_DETAIL">
<span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">IGNORE_EXCEPTION_DETAIL</span></span><a class="headerlink" href="#doctest.IGNORE_EXCEPTION_DETAIL" title="Link to this definition">¶</a></dt>
<dd><p>When specified, doctests expecting exceptions pass so long as an exception
of the expected type is raised, even if the details
(message and fully qualified exception name) don't match.</p>
<p>For example, an example expecting <code class="docutils literal notranslate"><span class="pre">ValueError:</span> <span class="pre">42</span></code> will pass if the actual
exception raised is <code class="docutils literal notranslate"><span class="pre">ValueError:</span> <span class="pre">3*14</span></code>, but will fail if, say, a
<a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised instead.
It will also ignore any fully qualified name included before the
exception class, which can vary between implementations and versions
of Python and the code/libraries in use.
Hence, all three of these variations will work with the flag specified:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">'message'</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="gr">Exception</span>: <span class="n">message</span>
<span class="gp">>>> </span><span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">'message'</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="gr">builtins.Exception</span>: <span class="n">message</span>
<span class="gp">>>> </span><span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">'message'</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="gr">__main__.Exception</span>: <span class="n">message</span>
</pre></div>
</div>
<p>Note that <a class="reference internal" href="#doctest.ELLIPSIS" title="doctest.ELLIPSIS"><code class="xref py py-const docutils literal notranslate"><span class="pre">ELLIPSIS</span></code></a> can also be used to ignore the
details of the exception message, but such a test may still fail based
on whether the module name is present or matches exactly.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">تغییر داده شده در نسخه 3.2: </span><a class="reference internal" href="#doctest.IGNORE_EXCEPTION_DETAIL" title="doctest.IGNORE_EXCEPTION_DETAIL"><code class="xref py py-const docutils literal notranslate"><span class="pre">IGNORE_EXCEPTION_DETAIL</span></code></a> now also ignores any information relating
to the module containing the exception under test.</p>
</div>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="doctest.SKIP">
<span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">SKIP</span></span><a class="headerlink" href="#doctest.SKIP" title="Link to this definition">¶</a></dt>
<dd><p>When specified, do not run the example at all. This can be useful in contexts
where doctest examples serve as both documentation and test cases, and an
example should be included for documentation purposes, but should not be
checked. E.g., the example's output might be random; or the example might
depend on resources which would be unavailable to the test driver.</p>
<p>The SKIP flag can also be used for temporarily "commenting out" examples.</p>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="doctest.COMPARISON_FLAGS">
<span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">COMPARISON_FLAGS</span></span><a class="headerlink" href="#doctest.COMPARISON_FLAGS" title="Link to this definition">¶</a></dt>
<dd><p>A bitmask or'ing together all the comparison flags above.</p>
</dd></dl>
<p>The second group of options controls how test failures are reported:</p>
<dl class="py data">
<dt class="sig sig-object py" id="doctest.REPORT_UDIFF">
<span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">REPORT_UDIFF</span></span><a class="headerlink" href="#doctest.REPORT_UDIFF" title="Link to this definition">¶</a></dt>
<dd><p>When specified, failures that involve multi-line expected and actual outputs are
displayed using a unified diff.</p>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="doctest.REPORT_CDIFF">
<span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">REPORT_CDIFF</span></span><a class="headerlink" href="#doctest.REPORT_CDIFF" title="Link to this definition">¶</a></dt>
<dd><p>When specified, failures that involve multi-line expected and actual outputs
will be displayed using a context diff.</p>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="doctest.REPORT_NDIFF">
<span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">REPORT_NDIFF</span></span><a class="headerlink" href="#doctest.REPORT_NDIFF" title="Link to this definition">¶</a></dt>
<dd><p>When specified, differences are computed by <code class="docutils literal notranslate"><span class="pre">difflib.Differ</span></code>, using the same
algorithm as the popular <code class="file docutils literal notranslate"><span class="pre">ndiff.py</span></code> utility. This is the only method that
marks differences within lines as well as across lines. For example, if a line
of expected output contains digit <code class="docutils literal notranslate"><span class="pre">1</span></code> where actual output contains letter
<code class="docutils literal notranslate"><span class="pre">l</span></code>, a line is inserted with a caret marking the mismatching column positions.</p>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="doctest.REPORT_ONLY_FIRST_FAILURE">
<span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">REPORT_ONLY_FIRST_FAILURE</span></span><a class="headerlink" href="#doctest.REPORT_ONLY_FIRST_FAILURE" title="Link to this definition">¶</a></dt>
<dd><p>When specified, display the first failing example in each doctest, but suppress
output for all remaining examples. This will prevent doctest from reporting
correct examples that break because of earlier failures; but it might also hide
incorrect examples that fail independently of the first failure. When
<a class="reference internal" href="#doctest.REPORT_ONLY_FIRST_FAILURE" title="doctest.REPORT_ONLY_FIRST_FAILURE"><code class="xref py py-const docutils literal notranslate"><span class="pre">REPORT_ONLY_FIRST_FAILURE</span></code></a> is specified, the remaining examples are
still run, and still count towards the total number of failures reported; only
the output is suppressed.</p>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="doctest.FAIL_FAST">
<span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">FAIL_FAST</span></span><a class="headerlink" href="#doctest.FAIL_FAST" title="Link to this definition">¶</a></dt>
<dd><p>When specified, exit after the first failing example and don't attempt to run
the remaining examples. Thus, the number of failures reported will be at most
1. This flag may be useful during debugging, since examples after the first
failure won't even produce debugging output.</p>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="doctest.REPORTING_FLAGS">
<span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">REPORTING_FLAGS</span></span><a class="headerlink" href="#doctest.REPORTING_FLAGS" title="Link to this definition">¶</a></dt>
<dd><p>A bitmask or'ing together all the reporting flags above.</p>
</dd></dl>
<p>There is also a way to register new option flag names, though this isn't
useful unless you intend to extend <code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> internals via subclassing:</p>
<dl class="py function">
<dt class="sig sig-object py" id="doctest.register_optionflag">
<span class="sig-prename descclassname"><span class="pre">doctest.</span></span><span class="sig-name descname"><span class="pre">register_optionflag</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#doctest.register_optionflag" title="Link to this definition">¶</a></dt>
<dd><p>Create a new option flag with a given name, and return the new flag's integer
value. <code class="xref py py-func docutils literal notranslate"><span class="pre">register_optionflag()</span></code> can be used when subclassing
<a class="reference internal" href="#doctest.OutputChecker" title="doctest.OutputChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code></a> or <a class="reference internal" href="#doctest.DocTestRunner" title="doctest.DocTestRunner"><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code></a> to create new options that are
supported by your subclasses. <code class="xref py py-func docutils literal notranslate"><span class="pre">register_optionflag()</span></code> should always be
called using the following idiom:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">MY_FLAG</span> <span class="o">=</span> <span class="n">register_optionflag</span><span class="p">(</span><span class="s1">'MY_FLAG'</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>
</section>
<section id="directives">
<span id="doctest-directives"></span><span id="index-4"></span><h3>Directives<a class="headerlink" href="#directives" title="Link to this heading">¶</a></h3>
<p>Doctest directives may be used to modify the <a class="reference internal" href="#doctest-options"><span class="std std-ref">option flags</span></a> for an individual example. Doctest directives are
special Python comments following an example's source code:</p>
<pre class="highlight">
<strong id="grammar-token-doctest-directive">directive</strong>: <span class="sx">"#"</span> <span class="sx">"doctest:"</span> <a class="reference internal" href="#grammar-token-doctest-directive_options"><code class="xref docutils literal notranslate"><span class="pre">directive_options</span></code></a>
<strong id="grammar-token-doctest-directive_options">directive_options</strong>: <a class="reference internal" href="#grammar-token-doctest-directive_option"><code class="xref docutils literal notranslate"><span class="pre">directive_option</span></code></a> (<span class="sx">","</span> <a class="reference internal" href="#grammar-token-doctest-directive_option"><code class="xref docutils literal notranslate"><span class="pre">directive_option</span></code></a>)*
<strong id="grammar-token-doctest-directive_option">directive_option</strong>: <a class="reference internal" href="#grammar-token-doctest-on_or_off"><code class="xref docutils literal notranslate"><span class="pre">on_or_off</span></code></a> <a class="reference internal" href="#grammar-token-doctest-directive_option_name"><code class="xref docutils literal notranslate"><span class="pre">directive_option_name</span></code></a>
<strong id="grammar-token-doctest-on_or_off">on_or_off</strong>: <span class="sx">"+"</span> | <span class="sx">"-"</span>
<strong id="grammar-token-doctest-directive_option_name">directive_option_name</strong>: <span class="sx">"DONT_ACCEPT_BLANKLINE"</span> | <span class="sx">"NORMALIZE_WHITESPACE"</span> | ...
</pre>
<p>Whitespace is not allowed between the <code class="docutils literal notranslate"><span class="pre">+</span></code> or <code class="docutils literal notranslate"><span class="pre">-</span></code> and the directive option
name. The directive option name can be any of the option flag names explained
above.</p>
<p>An example's doctest directives modify doctest's behavior for that single
example. Use <code class="docutils literal notranslate"><span class="pre">+</span></code> to enable the named behavior, or <code class="docutils literal notranslate"><span class="pre">-</span></code> to disable it.</p>
<p>For example, this test passes:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">20</span><span class="p">)))</span> <span class="c1"># doctest: +NORMALIZE_WHITESPACE</span>
<span class="go">[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,</span>
<span class="go">10, 11, 12, 13, 14, 15, 16, 17, 18, 19]</span>
</pre></div>
</div>
<p>Without the directive it would fail, both because the actual output doesn't have
two blanks before the single-digit list elements, and because the actual output
is on a single line. This test also passes, and also requires a directive to do
so:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">20</span><span class="p">)))</span> <span class="c1"># doctest: +ELLIPSIS</span>
<span class="go">[0, 1, ..., 18, 19]</span>
</pre></div>
</div>
<p>Multiple directives can be used on a single physical line, separated by
commas:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">20</span><span class="p">)))</span> <span class="c1"># doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE</span>
<span class="go">[0, 1, ..., 18, 19]</span>
</pre></div>
</div>
<p>If multiple directive comments are used for a single example, then they are
combined:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">20</span><span class="p">)))</span> <span class="c1"># doctest: +ELLIPSIS</span>
<span class="gp">... </span> <span class="c1"># doctest: +NORMALIZE_WHITESPACE</span>
<span class="go">[0, 1, ..., 18, 19]</span>
</pre></div>
</div>
<p>As the previous example shows, you can add <code class="docutils literal notranslate"><span class="pre">...</span></code> lines to your example
containing only directives. This can be useful when an example is too long for
a directive to comfortably fit on the same line:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span> <span class="o">+</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">))</span> <span class="o">+</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">30</span><span class="p">,</span> <span class="mi">40</span><span class="p">)))</span>
<span class="gp">... </span><span class="c1"># doctest: +ELLIPSIS</span>
<span class="go">[0, ..., 4, 10, ..., 19, 30, ..., 39]</span>
</pre></div>
</div>
<p>Note that since all options are disabled by default, and directives apply only
to the example they appear in, enabling options (via <code class="docutils literal notranslate"><span class="pre">+</span></code> in a directive) is
usually the only meaningful choice. However, option flags can also be passed to
functions that run doctests, establishing different defaults. In such cases,
disabling an option via <code class="docutils literal notranslate"><span class="pre">-</span></code> in a directive can be useful.</p>
</section>
<section id="warnings">
<span id="doctest-warnings"></span><h3>Warnings<a class="headerlink" href="#warnings" title="Link to this heading">¶</a></h3>
<p><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> is serious about requiring exact matches in expected output. If
even a single character doesn't match, the test fails. This will probably
surprise you a few times, as you learn exactly what Python does and doesn't
guarantee about output. For example, when printing a set, Python doesn't
guarantee that the element is printed in any particular order, so a test like</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">foo</span><span class="p">()</span>
<span class="go">{"spam", "eggs"}</span>
</pre></div>
</div>
<p>is vulnerable! One workaround is to do</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">foo</span><span class="p">()</span> <span class="o">==</span> <span class="p">{</span><span class="s2">"spam"</span><span class="p">,</span> <span class="s2">"eggs"</span><span class="p">}</span>
<span class="go">True</span>
</pre></div>
</div>
<p>instead. Another is to do</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">foo</span><span class="p">())</span>
<span class="gp">>>> </span><span class="n">d</span>
<span class="go">['eggs', 'spam']</span>
</pre></div>
</div>
<p>There are others, but you get the idea.</p>
<p>Another bad idea is to print things that embed an object address, like</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">id</span><span class="p">(</span><span class="mf">1.0</span><span class="p">)</span> <span class="c1"># certain to fail some of the time</span>
<span class="go">7948648</span>
<span class="gp">>>> </span><span class="k">class</span><span class="w"> </span><span class="nc">C</span><span class="p">:</span> <span class="k">pass</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">()</span> <span class="c1"># the default repr() for instances embeds an address</span>
<span class="go"><C object at 0x00AC18F0></span>
</pre></div>
</div>
<p>The <a class="reference internal" href="#doctest.ELLIPSIS" title="doctest.ELLIPSIS"><code class="xref py py-const docutils literal notranslate"><span class="pre">ELLIPSIS</span></code></a> directive gives a nice approach for the last example:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">C</span><span class="p">()</span> <span class="c1"># doctest: +ELLIPSIS</span>
<span class="go"><C object at 0x...></span>
</pre></div>
</div>
<p>Floating-point numbers are also subject to small output variations across
platforms, because Python defers to the platform C library for some
floating-point calculations, and C libraries vary widely in quality here.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mi">1000</span><span class="o">**</span><span class="mf">0.1</span> <span class="c1"># risky</span>