annotate Lib/test/test_doctest.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 4264cf00a75c
children 891e79d770d8 311107443cc1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2 Test script for doctest.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
3 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
4
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 19953
diff changeset
5 from test import test_support
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
6 import doctest
33243
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
7 import warnings
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
8
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 45468
diff changeset
9 # NOTE: There are some additional tests relating to interaction with
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 45468
diff changeset
10 # zipimport in the test_zipimport_support test module.
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 45468
diff changeset
11
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
12 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
13 ## Sample Objects (used by test cases)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
14 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
15
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
16 def sample_func(v):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
17 """
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
18 Blah blah
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
19
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
20 >>> print sample_func(22)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
21 44
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
22
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
23 Yee ha!
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
24 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
25 return v+v
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
26
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
27 class SampleClass:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
28 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
29 >>> print 1
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
30 1
33532
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
31
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
32 >>> # comments get ignored. so are empty PS1 and PS2 prompts:
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
33 >>>
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
34 ...
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
35
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
36 Multiline example:
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
37 >>> sc = SampleClass(3)
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
38 >>> for i in range(10):
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
39 ... sc = sc.double()
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
40 ... print sc.get(),
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
41 6 12 24 48 96 192 384 768 1536 3072
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
42 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
43 def __init__(self, val):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
44 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
45 >>> print SampleClass(12).get()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
46 12
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
47 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
48 self.val = val
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
49
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
50 def double(self):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
51 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
52 >>> print SampleClass(12).double().get()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
53 24
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
54 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
55 return SampleClass(self.val + self.val)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
56
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
57 def get(self):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
58 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
59 >>> print SampleClass(-5).get()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
60 -5
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
61 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
62 return self.val
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
63
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
64 def a_staticmethod(v):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
65 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
66 >>> print SampleClass.a_staticmethod(10)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
67 11
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
68 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
69 return v+1
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
70 a_staticmethod = staticmethod(a_staticmethod)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
71
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
72 def a_classmethod(cls, v):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
73 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
74 >>> print SampleClass.a_classmethod(10)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
75 12
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
76 >>> print SampleClass(0).a_classmethod(10)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
77 12
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
78 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
79 return v+2
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
80 a_classmethod = classmethod(a_classmethod)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
81
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
82 a_property = property(get, doc="""
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
83 >>> print SampleClass(22).a_property
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
84 22
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
85 """)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
86
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
87 class NestedClass:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
88 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
89 >>> x = SampleClass.NestedClass(5)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
90 >>> y = x.square()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
91 >>> print y.get()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
92 25
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
93 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
94 def __init__(self, val=0):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
95 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
96 >>> print SampleClass.NestedClass().get()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
97 0
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
98 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
99 self.val = val
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
100 def square(self):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
101 return SampleClass.NestedClass(self.val*self.val)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
102 def get(self):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
103 return self.val
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
104
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
105 class SampleNewStyleClass(object):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
106 r"""
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
107 >>> print '1\n2\n3'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
108 1
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
109 2
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
110 3
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
111 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
112 def __init__(self, val):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
113 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
114 >>> print SampleNewStyleClass(12).get()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
115 12
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
116 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
117 self.val = val
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
118
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
119 def double(self):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
120 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
121 >>> print SampleNewStyleClass(12).double().get()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
122 24
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
123 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
124 return SampleNewStyleClass(self.val + self.val)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
125
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
126 def get(self):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
127 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
128 >>> print SampleNewStyleClass(-5).get()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
129 -5
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
130 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
131 return self.val
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
132
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
133 ######################################################################
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
134 ## Fake stdin (for testing interactive debugging)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
135 ######################################################################
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
136
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
137 class _FakeInput:
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
138 """
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
139 A fake input stream for pdb's interactive debugger. Whenever a
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
140 line is read, print it (to simulate the user typing it), and then
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
141 return it. The set of lines to return is specified in the
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
142 constructor; they should not have trailing newlines.
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
143 """
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
144 def __init__(self, lines):
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
145 self.lines = lines
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
146
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
147 def readline(self):
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
148 line = self.lines.pop(0)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
149 print line
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
150 return line+'\n'
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
151
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
152 ######################################################################
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
153 ## Test Cases
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
154 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
155
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
156 def test_Example(): r"""
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
157 Unit tests for the `Example` class.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
158
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
159 Example is a simple container class that holds:
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
160 - `source`: A source string.
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
161 - `want`: An expected output string.
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
162 - `exc_msg`: An expected exception message string (or None if no
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
163 exception is expected).
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
164 - `lineno`: A line number (within the docstring).
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
165 - `indent`: The example's indentation in the input string.
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
166 - `options`: An option dictionary, mapping option flags to True or
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
167 False.
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
168
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
169 These attributes are set by the constructor. `source` and `want` are
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
170 required; the other attributes all have default values:
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
171
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
172 >>> example = doctest.Example('print 1', '1\n')
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
173 >>> (example.source, example.want, example.exc_msg,
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
174 ... example.lineno, example.indent, example.options)
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
175 ('print 1\n', '1\n', None, 0, 0, {})
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
176
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
177 The first three attributes (`source`, `want`, and `exc_msg`) may be
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
178 specified positionally; the remaining arguments should be specified as
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
179 keyword arguments:
33060
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
180
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
181 >>> exc_msg = 'IndexError: pop from an empty list'
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
182 >>> example = doctest.Example('[].pop()', '', exc_msg,
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
183 ... lineno=5, indent=4,
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
184 ... options={doctest.ELLIPSIS: True})
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
185 >>> (example.source, example.want, example.exc_msg,
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
186 ... example.lineno, example.indent, example.options)
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
187 ('[].pop()\n', '', 'IndexError: pop from an empty list\n', 5, 4, {8: True})
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
188
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
189 The constructor normalizes the `source` string to end in a newline:
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
190
33060
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
191 Source spans a single line: no terminating newline.
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
192 >>> e = doctest.Example('print 1', '1\n')
33060
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
193 >>> e.source, e.want
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
194 ('print 1\n', '1\n')
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
195
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
196 >>> e = doctest.Example('print 1\n', '1\n')
33060
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
197 >>> e.source, e.want
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
198 ('print 1\n', '1\n')
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
199
33060
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
200 Source spans multiple lines: require terminating newline.
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
201 >>> e = doctest.Example('print 1;\nprint 2\n', '1\n2\n')
33060
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
202 >>> e.source, e.want
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
203 ('print 1;\nprint 2\n', '1\n2\n')
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
204
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
205 >>> e = doctest.Example('print 1;\nprint 2', '1\n2\n')
33060
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
206 >>> e.source, e.want
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
207 ('print 1;\nprint 2\n', '1\n2\n')
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
208
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
209 Empty source string (which should never appear in real examples)
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
210 >>> e = doctest.Example('', '')
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
211 >>> e.source, e.want
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
212 ('\n', '')
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
213
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
214 The constructor normalizes the `want` string to end in a newline,
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
215 unless it's the empty string:
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
216
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
217 >>> e = doctest.Example('print 1', '1\n')
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
218 >>> e.source, e.want
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
219 ('print 1\n', '1\n')
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
220
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
221 >>> e = doctest.Example('print 1', '1')
33060
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
222 >>> e.source, e.want
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
223 ('print 1\n', '1\n')
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
224
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
225 >>> e = doctest.Example('print', '')
33060
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
226 >>> e.source, e.want
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
227 ('print\n', '')
33277
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
228
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
229 The constructor normalizes the `exc_msg` string to end in a newline,
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
230 unless it's `None`:
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
231
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
232 Message spans one line
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
233 >>> exc_msg = 'IndexError: pop from an empty list'
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
234 >>> e = doctest.Example('[].pop()', '', exc_msg)
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
235 >>> e.exc_msg
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
236 'IndexError: pop from an empty list\n'
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
237
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
238 >>> exc_msg = 'IndexError: pop from an empty list\n'
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
239 >>> e = doctest.Example('[].pop()', '', exc_msg)
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
240 >>> e.exc_msg
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
241 'IndexError: pop from an empty list\n'
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
242
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
243 Message spans multiple lines
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
244 >>> exc_msg = 'ValueError: 1\n 2'
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
245 >>> e = doctest.Example('raise ValueError("1\n 2")', '', exc_msg)
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
246 >>> e.exc_msg
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
247 'ValueError: 1\n 2\n'
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
248
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
249 >>> exc_msg = 'ValueError: 1\n 2\n'
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
250 >>> e = doctest.Example('raise ValueError("1\n 2")', '', exc_msg)
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
251 >>> e.exc_msg
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
252 'ValueError: 1\n 2\n'
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
253
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
254 Empty (but non-None) exception message (which should never appear
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
255 in real examples)
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
256 >>> exc_msg = ''
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
257 >>> e = doctest.Example('raise X()', '', exc_msg)
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
258 >>> e.exc_msg
0c734932c6f4 Added an "exc_msg" attribute to Example (containing the expected
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33274
diff changeset
259 '\n'
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
260 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
261
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
262 def test_DocTest(): r"""
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
263 Unit tests for the `DocTest` class.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
264
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
265 DocTest is a collection of examples, extracted from a docstring, along
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
266 with information about where the docstring comes from (a name,
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
267 filename, and line number). The docstring is parsed by the `DocTest`
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
268 constructor:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
269
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
270 >>> docstring = '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
271 ... >>> print 12
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
272 ... 12
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
273 ...
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
274 ... Non-example text.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
275 ...
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
276 ... >>> print 'another\example'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
277 ... another
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
278 ... example
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
279 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
280 >>> globs = {} # globals to run the test in.
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
281 >>> parser = doctest.DocTestParser()
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
282 >>> test = parser.get_doctest(docstring, globs, 'some_test',
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
283 ... 'some_file', 20)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
284 >>> print test
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
285 <DocTest some_test from some_file:20 (2 examples)>
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
286 >>> len(test.examples)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
287 2
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
288 >>> e1, e2 = test.examples
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
289 >>> (e1.source, e1.want, e1.lineno)
33060
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
290 ('print 12\n', '12\n', 1)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
291 >>> (e2.source, e2.want, e2.lineno)
33060
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
292 ("print 'another\\example'\n", 'another\nexample\n', 6)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
293
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
294 Source information (name, filename, and line number) is available as
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
295 attributes on the doctest object:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
296
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
297 >>> (test.name, test.filename, test.lineno)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
298 ('some_test', 'some_file', 20)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
299
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
300 The line number of an example within its containing file is found by
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
301 adding the line number of the example and the line number of its
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
302 containing test:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
303
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
304 >>> test.lineno + e1.lineno
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
305 21
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
306 >>> test.lineno + e2.lineno
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
307 26
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
308
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
309 If the docstring contains inconsistant leading whitespace in the
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
310 expected output of an example, then `DocTest` will raise a ValueError:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
311
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
312 >>> docstring = r'''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
313 ... >>> print 'bad\nindentation'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
314 ... bad
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
315 ... indentation
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
316 ... '''
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
317 >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
318 Traceback (most recent call last):
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
319 ValueError: line 4 of the docstring for some_test has inconsistent leading whitespace: 'indentation'
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
320
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
321 If the docstring contains inconsistent leading whitespace on
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
322 continuation lines, then `DocTest` will raise a ValueError:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
323
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
324 >>> docstring = r'''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
325 ... >>> print ('bad indentation',
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
326 ... ... 2)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
327 ... ('bad', 'indentation')
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
328 ... '''
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
329 >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
330 Traceback (most recent call last):
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
331 ValueError: line 2 of the docstring for some_test has inconsistent leading whitespace: '... 2)'
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
332
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
333 If there's no blank space after a PS1 prompt ('>>>'), then `DocTest`
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
334 will raise a ValueError:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
335
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
336 >>> docstring = '>>>print 1\n1'
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
337 >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
338 Traceback (most recent call last):
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
339 ValueError: line 1 of the docstring for some_test lacks blank after >>>: '>>>print 1'
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
340
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
341 If there's no blank space after a PS2 prompt ('...'), then `DocTest`
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
342 will raise a ValueError:
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
343
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
344 >>> docstring = '>>> if 1:\n...print 1\n1'
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
345 >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0)
33055
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
346 Traceback (most recent call last):
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
347 ValueError: line 2 of the docstring for some_test lacks blank after ...: '...print 1'
fbea2be35562 Rewrote Parser, using regular expressions instead of walking though
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33045
diff changeset
348
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
349 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
350
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
351 def test_DocTestFinder(): r"""
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
352 Unit tests for the `DocTestFinder` class.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
353
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
354 DocTestFinder is used to extract DocTests from an object's docstring
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
355 and the docstrings of its contained objects. It can be used with
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
356 modules, functions, classes, methods, staticmethods, classmethods, and
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
357 properties.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
358
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
359 Finding Tests in Functions
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
360 ~~~~~~~~~~~~~~~~~~~~~~~~~~
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
361 For a function whose docstring contains examples, DocTestFinder.find()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
362 will return a single test (for that function's docstring):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
363
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
364 >>> finder = doctest.DocTestFinder()
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
365
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
366 We'll simulate a __file__ attr that ends in pyc:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
367
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
368 >>> import test.test_doctest
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
369 >>> old = test.test_doctest.__file__
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
370 >>> test.test_doctest.__file__ = 'test_doctest.pyc'
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
371
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
372 >>> tests = finder.find(sample_func)
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
373
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
374 >>> print tests # doctest: +ELLIPSIS
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 45468
diff changeset
375 [<DocTest sample_func from ...:16 (1 example)>]
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
376
33244
395bb3746d1d test_DocTestFinder(): This test failed when test_doctest was run
Tim Peters <tim.peters@gmail.com>
parents: 33243
diff changeset
377 The exact name depends on how test_doctest was invoked, so allow for
395bb3746d1d test_DocTestFinder(): This test failed when test_doctest was run
Tim Peters <tim.peters@gmail.com>
parents: 33243
diff changeset
378 leading path components.
395bb3746d1d test_DocTestFinder(): This test failed when test_doctest was run
Tim Peters <tim.peters@gmail.com>
parents: 33243
diff changeset
379
395bb3746d1d test_DocTestFinder(): This test failed when test_doctest was run
Tim Peters <tim.peters@gmail.com>
parents: 33243
diff changeset
380 >>> tests[0].filename # doctest: +ELLIPSIS
395bb3746d1d test_DocTestFinder(): This test failed when test_doctest was run
Tim Peters <tim.peters@gmail.com>
parents: 33243
diff changeset
381 '...test_doctest.py'
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
382
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
383 >>> test.test_doctest.__file__ = old
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
384
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
385
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
386 >>> e = tests[0].examples[0]
33060
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
387 >>> (e.source, e.want, e.lineno)
31ccea9e98a7 Drop the excruciating newline requirements on arguments to
Tim Peters <tim.peters@gmail.com>
parents: 33055
diff changeset
388 ('print sample_func(22)\n', '44\n', 3)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
389
33469
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
390 By default, tests are created for objects with no docstring:
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
391
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
392 >>> def no_docstring(v):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
393 ... pass
33473
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
394 >>> finder.find(no_docstring)
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
395 []
33469
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
396
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
397 However, the optional argument `exclude_empty` to the DocTestFinder
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
398 constructor can be used to exclude tests for objects with empty
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
399 docstrings:
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
400
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
401 >>> def no_docstring(v):
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
402 ... pass
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
403 >>> excl_empty_finder = doctest.DocTestFinder(exclude_empty=True)
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
404 >>> excl_empty_finder.find(no_docstring)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
405 []
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
406
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
407 If the function has a docstring with no examples, then a test with no
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
408 examples is returned. (This lets `DocTestRunner` collect statistics
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
409 about which functions have no tests -- but is that useful? And should
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
410 an empty test also be created when there's no docstring?)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
411
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
412 >>> def no_examples(v):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
413 ... ''' no doctest examples '''
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
414 >>> finder.find(no_examples) # doctest: +ELLIPSIS
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
415 [<DocTest no_examples from ...:1 (no examples)>]
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
416
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
417 Finding Tests in Classes
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
418 ~~~~~~~~~~~~~~~~~~~~~~~~
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
419 For a class, DocTestFinder will create a test for the class's
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
420 docstring, and will recursively explore its contents, including
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
421 methods, classmethods, staticmethods, properties, and nested classes.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
422
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
423 >>> finder = doctest.DocTestFinder()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
424 >>> tests = finder.find(SampleClass)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
425 >>> for t in tests:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
426 ... print '%2s %s' % (len(t.examples), t.name)
33532
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
427 3 SampleClass
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
428 3 SampleClass.NestedClass
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
429 1 SampleClass.NestedClass.__init__
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
430 1 SampleClass.__init__
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
431 2 SampleClass.a_classmethod
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
432 1 SampleClass.a_property
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
433 1 SampleClass.a_staticmethod
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
434 1 SampleClass.double
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
435 1 SampleClass.get
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
436
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
437 New-style classes are also supported:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
438
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
439 >>> tests = finder.find(SampleNewStyleClass)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
440 >>> for t in tests:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
441 ... print '%2s %s' % (len(t.examples), t.name)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
442 1 SampleNewStyleClass
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
443 1 SampleNewStyleClass.__init__
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
444 1 SampleNewStyleClass.double
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
445 1 SampleNewStyleClass.get
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
446
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
447 Finding Tests in Modules
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
448 ~~~~~~~~~~~~~~~~~~~~~~~~
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
449 For a module, DocTestFinder will create a test for the class's
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
450 docstring, and will recursively explore its contents, including
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
451 functions, classes, and the `__test__` dictionary, if it exists:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
452
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
453 >>> # A module
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 43977
diff changeset
454 >>> import types
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 43977
diff changeset
455 >>> m = types.ModuleType('some_module')
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
456 >>> def triple(val):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
457 ... '''
33532
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
458 ... >>> print triple(11)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
459 ... 33
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
460 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
461 ... return val*3
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
462 >>> m.__dict__.update({
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
463 ... 'sample_func': sample_func,
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
464 ... 'SampleClass': SampleClass,
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
465 ... '__doc__': '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
466 ... Module docstring.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
467 ... >>> print 'module'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
468 ... module
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
469 ... ''',
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
470 ... '__test__': {
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
471 ... 'd': '>>> print 6\n6\n>>> print 7\n7\n',
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
472 ... 'c': triple}})
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
473
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
474 >>> finder = doctest.DocTestFinder()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
475 >>> # Use module=test.test_doctest, to prevent doctest from
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
476 >>> # ignoring the objects since they weren't defined in m.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
477 >>> import test.test_doctest
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
478 >>> tests = finder.find(m, module=test.test_doctest)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
479 >>> for t in tests:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
480 ... print '%2s %s' % (len(t.examples), t.name)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
481 1 some_module
33532
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
482 3 some_module.SampleClass
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
483 3 some_module.SampleClass.NestedClass
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
484 1 some_module.SampleClass.NestedClass.__init__
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
485 1 some_module.SampleClass.__init__
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
486 2 some_module.SampleClass.a_classmethod
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
487 1 some_module.SampleClass.a_property
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
488 1 some_module.SampleClass.a_staticmethod
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
489 1 some_module.SampleClass.double
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
490 1 some_module.SampleClass.get
33468
a04515a98831 DocTestFinder._find(): for tests derived from a module __test__ global,
Tim Peters <tim.peters@gmail.com>
parents: 33459
diff changeset
491 1 some_module.__test__.c
a04515a98831 DocTestFinder._find(): for tests derived from a module __test__ global,
Tim Peters <tim.peters@gmail.com>
parents: 33459
diff changeset
492 2 some_module.__test__.d
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
493 1 some_module.sample_func
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
494
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
495 Duplicate Removal
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
496 ~~~~~~~~~~~~~~~~~
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
497 If a single object is listed twice (under different names), then tests
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
498 will only be generated for it once:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
499
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33040
diff changeset
500 >>> from test import doctest_aliases
33469
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
501 >>> tests = excl_empty_finder.find(doctest_aliases)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
502 >>> print len(tests)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
503 2
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
504 >>> print tests[0].name
33045
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33040
diff changeset
505 test.doctest_aliases.TwoNames
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33040
diff changeset
506
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33040
diff changeset
507 TwoNames.f and TwoNames.g are bound to the same object.
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33040
diff changeset
508 We can't guess which will be found in doctest's traversal of
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33040
diff changeset
509 TwoNames.__dict__ first, so we have to allow for either.
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33040
diff changeset
510
163621517b98 Get rid of the ignore_imports argument to DocTestFinder.find().
Tim Peters <tim.peters@gmail.com>
parents: 33040
diff changeset
511 >>> tests[1].name.split('.')[-1] in ['f', 'g']
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
512 True
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
513
38601
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38459
diff changeset
514 Empty Tests
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38459
diff changeset
515 ~~~~~~~~~~~
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38459
diff changeset
516 By default, an object with no doctests doesn't create any tests:
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
517
38601
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38459
diff changeset
518 >>> tests = doctest.DocTestFinder().find(SampleClass)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
519 >>> for t in tests:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
520 ... print '%2s %s' % (len(t.examples), t.name)
33532
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
521 3 SampleClass
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
522 3 SampleClass.NestedClass
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
523 1 SampleClass.NestedClass.__init__
33473
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
524 1 SampleClass.__init__
38601
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38459
diff changeset
525 2 SampleClass.a_classmethod
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38459
diff changeset
526 1 SampleClass.a_property
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38459
diff changeset
527 1 SampleClass.a_staticmethod
33473
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
528 1 SampleClass.double
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
529 1 SampleClass.get
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
530
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
531 By default, that excluded objects with no doctests. exclude_empty=False
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
532 tells it to include (empty) tests for objects with no doctests. This feature
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
533 is really to support backward compatibility in what doctest.master.summarize()
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
534 displays.
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
535
38601
1c9d8077ecfb Remove doctest.testmod's deprecated (in 2.4) `isprivate`
Tim Peters <tim.peters@gmail.com>
parents: 38459
diff changeset
536 >>> tests = doctest.DocTestFinder(exclude_empty=False).find(SampleClass)
33473
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
537 >>> for t in tests:
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
538 ... print '%2s %s' % (len(t.examples), t.name)
33532
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
539 3 SampleClass
33473
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
540 3 SampleClass.NestedClass
3f86f2bb9b56 exclude_empty: make the default True for DocTestFinder, and introduce it
Tim Peters <tim.peters@gmail.com>
parents: 33469
diff changeset
541 1 SampleClass.NestedClass.__init__
33469
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
542 0 SampleClass.NestedClass.get
925cf9b3285b Added new parameter exclude_empty to DocTestFinder.__init__, which
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33468
diff changeset
543 0 SampleClass.NestedClass.square
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
544 1 SampleClass.__init__
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
545 2 SampleClass.a_classmethod
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
546 1 SampleClass.a_property
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
547 1 SampleClass.a_staticmethod
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
548 1 SampleClass.double
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
549 1 SampleClass.get
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
550
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
551 Turning off Recursion
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
552 ~~~~~~~~~~~~~~~~~~~~~
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
553 DocTestFinder can be told not to look for tests in contained objects
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
554 using the `recurse` flag:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
555
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
556 >>> tests = doctest.DocTestFinder(recurse=False).find(SampleClass)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
557 >>> for t in tests:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
558 ... print '%2s %s' % (len(t.examples), t.name)
33532
695e5bb08382 - Changed SampleClass docstrings to test docstring parsing a little
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33522
diff changeset
559 3 SampleClass
33165
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
560
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
561 Line numbers
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
562 ~~~~~~~~~~~~
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
563 DocTestFinder finds the line number of each example:
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
564
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
565 >>> def f(x):
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
566 ... '''
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
567 ... >>> x = 12
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
568 ...
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
569 ... some text
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
570 ...
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
571 ... >>> # examples are not created for comments & bare prompts.
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
572 ... >>>
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
573 ... ...
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
574 ...
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
575 ... >>> for x in range(10):
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
576 ... ... print x,
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
577 ... 0 1 2 3 4 5 6 7 8 9
37266
5d625f589f2a Part of bug 1459808: fiddle so that this passes
Tim Peters <tim.peters@gmail.com>
parents: 35742
diff changeset
578 ... >>> x//2
33165
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
579 ... 6
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
580 ... '''
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
581 >>> test = doctest.DocTestFinder().find(f)[0]
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
582 >>> [e.lineno for e in test.examples]
357b24c346bb Fixed bug in line-number finding for examples (DocTestParser wasn't
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33089
diff changeset
583 [1, 9, 12]
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
584 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
585
33299
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
586 def test_DocTestParser(): r"""
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
587 Unit tests for the `DocTestParser` class.
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
588
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
589 DocTestParser is used to parse docstrings containing doctest examples.
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
590
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
591 The `parse` method divides a docstring into examples and intervening
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
592 text:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
593
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
594 >>> s = '''
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
595 ... >>> x, y = 2, 3 # no output expected
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
596 ... >>> if 1:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
597 ... ... print x
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
598 ... ... print y
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
599 ... 2
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
600 ... 3
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
601 ...
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
602 ... Some text.
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
603 ... >>> x+y
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
604 ... 5
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
605 ... '''
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
606 >>> parser = doctest.DocTestParser()
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
607 >>> for piece in parser.parse(s):
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
608 ... if isinstance(piece, doctest.Example):
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
609 ... print 'Example:', (piece.source, piece.want, piece.lineno)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
610 ... else:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
611 ... print ' Text:', `piece`
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
612 Text: '\n'
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
613 Example: ('x, y = 2, 3 # no output expected\n', '', 1)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
614 Text: ''
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
615 Example: ('if 1:\n print x\n print y\n', '2\n3\n', 2)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
616 Text: '\nSome text.\n'
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
617 Example: ('x+y\n', '5\n', 9)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
618 Text: ''
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
619
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
620 The `get_examples` method returns just the examples:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
621
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
622 >>> for piece in parser.get_examples(s):
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
623 ... print (piece.source, piece.want, piece.lineno)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
624 ('x, y = 2, 3 # no output expected\n', '', 1)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
625 ('if 1:\n print x\n print y\n', '2\n3\n', 2)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
626 ('x+y\n', '5\n', 9)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
627
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
628 The `get_doctest` method creates a Test from the examples, along with the
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
629 given arguments:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
630
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
631 >>> test = parser.get_doctest(s, {}, 'name', 'filename', lineno=5)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
632 >>> (test.name, test.filename, test.lineno)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
633 ('name', 'filename', 5)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
634 >>> for piece in test.examples:
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
635 ... print (piece.source, piece.want, piece.lineno)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
636 ('x, y = 2, 3 # no output expected\n', '', 1)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
637 ('if 1:\n print x\n print y\n', '2\n3\n', 2)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
638 ('x+y\n', '5\n', 9)
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
639 """
b2910e479f07 - Added DocTestParser.parse(), which parses a docstring into Examples
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33296
diff changeset
640
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
641 class test_DocTestRunner:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
642 def basics(): r"""
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
643 Unit tests for the `DocTestRunner` class.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
644
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
645 DocTestRunner is used to run DocTest test cases, and to accumulate
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
646 statistics. Here's a simple DocTest case we can use:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
647
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
648 >>> def f(x):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
649 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
650 ... >>> x = 12
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
651 ... >>> print x
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
652 ... 12
37266
5d625f589f2a Part of bug 1459808: fiddle so that this passes
Tim Peters <tim.peters@gmail.com>
parents: 35742
diff changeset
653 ... >>> x//2
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
654 ... 6
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
655 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
656 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
657
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
658 The main DocTestRunner interface is the `run` method, which runs a
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
659 given DocTest case in a given namespace (globs). It returns a tuple
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
660 `(f,t)`, where `f` is the number of failed tests and `t` is the number
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
661 of tried tests.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
662
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
663 >>> doctest.DocTestRunner(verbose=False).run(test)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
664 TestResults(failed=0, attempted=3)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
665
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
666 If any example produces incorrect output, then the test runner reports
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
667 the failure and proceeds to the next example:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
668
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
669 >>> def f(x):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
670 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
671 ... >>> x = 12
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
672 ... >>> print x
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
673 ... 14
37266
5d625f589f2a Part of bug 1459808: fiddle so that this passes
Tim Peters <tim.peters@gmail.com>
parents: 35742
diff changeset
674 ... >>> x//2
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
675 ... 6
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
676 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
677 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
678 >>> doctest.DocTestRunner(verbose=True).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
679 ... # doctest: +ELLIPSIS
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
680 Trying:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
681 x = 12
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
682 Expecting nothing
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
683 ok
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
684 Trying:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
685 print x
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
686 Expecting:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
687 14
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
688 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
689 File ..., line 4, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
690 Failed example:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
691 print x
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
692 Expected:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
693 14
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
694 Got:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
695 12
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
696 Trying:
37266
5d625f589f2a Part of bug 1459808: fiddle so that this passes
Tim Peters <tim.peters@gmail.com>
parents: 35742
diff changeset
697 x//2
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
698 Expecting:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
699 6
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
700 ok
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
701 TestResults(failed=1, attempted=3)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
702 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
703 def verbose_flag(): r"""
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
704 The `verbose` flag makes the test runner generate more detailed
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
705 output:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
706
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
707 >>> def f(x):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
708 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
709 ... >>> x = 12
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
710 ... >>> print x
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
711 ... 12
37266
5d625f589f2a Part of bug 1459808: fiddle so that this passes
Tim Peters <tim.peters@gmail.com>
parents: 35742
diff changeset
712 ... >>> x//2
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
713 ... 6
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
714 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
715 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
716
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
717 >>> doctest.DocTestRunner(verbose=True).run(test)
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
718 Trying:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
719 x = 12
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
720 Expecting nothing
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
721 ok
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
722 Trying:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
723 print x
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
724 Expecting:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
725 12
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
726 ok
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
727 Trying:
37266
5d625f589f2a Part of bug 1459808: fiddle so that this passes
Tim Peters <tim.peters@gmail.com>
parents: 35742
diff changeset
728 x//2
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
729 Expecting:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
730 6
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
731 ok
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
732 TestResults(failed=0, attempted=3)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
733
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
734 If the `verbose` flag is unspecified, then the output will be verbose
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
735 iff `-v` appears in sys.argv:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
736
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
737 >>> # Save the real sys.argv list.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
738 >>> old_argv = sys.argv
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
739
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
740 >>> # If -v does not appear in sys.argv, then output isn't verbose.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
741 >>> sys.argv = ['test']
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
742 >>> doctest.DocTestRunner().run(test)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
743 TestResults(failed=0, attempted=3)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
744
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
745 >>> # If -v does appear in sys.argv, then output is verbose.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
746 >>> sys.argv = ['test', '-v']
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
747 >>> doctest.DocTestRunner().run(test)
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
748 Trying:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
749 x = 12
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
750 Expecting nothing
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
751 ok
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
752 Trying:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
753 print x
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
754 Expecting:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
755 12
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
756 ok
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
757 Trying:
37266
5d625f589f2a Part of bug 1459808: fiddle so that this passes
Tim Peters <tim.peters@gmail.com>
parents: 35742
diff changeset
758 x//2
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
759 Expecting:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
760 6
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
761 ok
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
762 TestResults(failed=0, attempted=3)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
763
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
764 >>> # Restore sys.argv
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
765 >>> sys.argv = old_argv
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
766
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
767 In the remaining examples, the test runner's verbosity will be
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
768 explicitly set, to ensure that the test behavior is consistent.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
769 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
770 def exceptions(): r"""
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
771 Tests of `DocTestRunner`'s exception handling.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
772
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
773 An expected exception is specified with a traceback message. The
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
774 lines between the first line and the type/value may be omitted or
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
775 replaced with any other string:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
776
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
777 >>> def f(x):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
778 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
779 ... >>> x = 12
37266
5d625f589f2a Part of bug 1459808: fiddle so that this passes
Tim Peters <tim.peters@gmail.com>
parents: 35742
diff changeset
780 ... >>> print x//0
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
781 ... Traceback (most recent call last):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
782 ... ZeroDivisionError: integer division or modulo by zero
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
783 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
784 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
785 >>> doctest.DocTestRunner(verbose=False).run(test)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
786 TestResults(failed=0, attempted=2)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
787
33274
2727718e9e8f Only recognize the expected output as an exception if it *starts* with
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33244
diff changeset
788 An example may not generate output before it raises an exception; if
2727718e9e8f Only recognize the expected output as an exception if it *starts* with
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33244
diff changeset
789 it does, then the traceback message will not be recognized as
2727718e9e8f Only recognize the expected output as an exception if it *starts* with
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33244
diff changeset
790 signaling an expected exception, so the example will be reported as an
2727718e9e8f Only recognize the expected output as an exception if it *starts* with
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33244
diff changeset
791 unexpected exception:
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
792
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
793 >>> def f(x):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
794 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
795 ... >>> x = 12
37266
5d625f589f2a Part of bug 1459808: fiddle so that this passes
Tim Peters <tim.peters@gmail.com>
parents: 35742
diff changeset
796 ... >>> print 'pre-exception output', x//0
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
797 ... pre-exception output
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
798 ... Traceback (most recent call last):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
799 ... ZeroDivisionError: integer division or modulo by zero
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
800 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
801 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
802 >>> doctest.DocTestRunner(verbose=False).run(test)
33274
2727718e9e8f Only recognize the expected output as an exception if it *starts* with
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33244
diff changeset
803 ... # doctest: +ELLIPSIS
2727718e9e8f Only recognize the expected output as an exception if it *starts* with
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33244
diff changeset
804 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
805 File ..., line 4, in f
33274
2727718e9e8f Only recognize the expected output as an exception if it *starts* with
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33244
diff changeset
806 Failed example:
37266
5d625f589f2a Part of bug 1459808: fiddle so that this passes
Tim Peters <tim.peters@gmail.com>
parents: 35742
diff changeset
807 print 'pre-exception output', x//0
33274
2727718e9e8f Only recognize the expected output as an exception if it *starts* with
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33244
diff changeset
808 Exception raised:
2727718e9e8f Only recognize the expected output as an exception if it *starts* with
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33244
diff changeset
809 ...
2727718e9e8f Only recognize the expected output as an exception if it *starts* with
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33244
diff changeset
810 ZeroDivisionError: integer division or modulo by zero
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
811 TestResults(failed=1, attempted=2)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
812
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
813 Exception messages may contain newlines:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
814
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
815 >>> def f(x):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
816 ... r'''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
817 ... >>> raise ValueError, 'multi\nline\nmessage'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
818 ... Traceback (most recent call last):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
819 ... ValueError: multi
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
820 ... line
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
821 ... message
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
822 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
823 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
824 >>> doctest.DocTestRunner(verbose=False).run(test)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
825 TestResults(failed=0, attempted=1)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
826
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
827 If an exception is expected, but an exception with the wrong type or
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
828 message is raised, then it is reported as a failure:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
829
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
830 >>> def f(x):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
831 ... r'''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
832 ... >>> raise ValueError, 'message'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
833 ... Traceback (most recent call last):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
834 ... ValueError: wrong message
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
835 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
836 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
837 >>> doctest.DocTestRunner(verbose=False).run(test)
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
838 ... # doctest: +ELLIPSIS
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
839 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
840 File ..., line 3, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
841 Failed example:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
842 raise ValueError, 'message'
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
843 Expected:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
844 Traceback (most recent call last):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
845 ValueError: wrong message
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
846 Got:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
847 Traceback (most recent call last):
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
848 ...
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
849 ValueError: message
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
850 TestResults(failed=1, attempted=1)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
851
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
852 However, IGNORE_EXCEPTION_DETAIL can be used to allow a mismatch in the
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
853 detail:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
854
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
855 >>> def f(x):
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
856 ... r'''
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
857 ... >>> raise ValueError, 'message' #doctest: +IGNORE_EXCEPTION_DETAIL
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
858 ... Traceback (most recent call last):
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
859 ... ValueError: wrong message
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
860 ... '''
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
861 >>> test = doctest.DocTestFinder().find(f)[0]
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
862 >>> doctest.DocTestRunner(verbose=False).run(test)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
863 TestResults(failed=0, attempted=1)
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
864
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
865 But IGNORE_EXCEPTION_DETAIL does not allow a mismatch in the exception type:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
866
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
867 >>> def f(x):
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
868 ... r'''
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
869 ... >>> raise ValueError, 'message' #doctest: +IGNORE_EXCEPTION_DETAIL
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
870 ... Traceback (most recent call last):
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
871 ... TypeError: wrong type
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
872 ... '''
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
873 >>> test = doctest.DocTestFinder().find(f)[0]
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
874 >>> doctest.DocTestRunner(verbose=False).run(test)
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
875 ... # doctest: +ELLIPSIS
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
876 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
877 File ..., line 3, in f
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
878 Failed example:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
879 raise ValueError, 'message' #doctest: +IGNORE_EXCEPTION_DETAIL
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
880 Expected:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
881 Traceback (most recent call last):
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
882 TypeError: wrong type
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
883 Got:
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
884 Traceback (most recent call last):
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
885 ...
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
886 ValueError: message
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
887 TestResults(failed=1, attempted=1)
33404
97c207db90a1 Added IGNORE_EXCEPTION_DETAIL comparison option. The need is explained
Tim Peters <tim.peters@gmail.com>
parents: 33315
diff changeset
888
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
889 If an exception is raised but not expected, then it is reported as an
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
890 unexpected exception:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
891
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
892 >>> def f(x):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
893 ... r'''
37266
5d625f589f2a Part of bug 1459808: fiddle so that this passes
Tim Peters <tim.peters@gmail.com>
parents: 35742
diff changeset
894 ... >>> 1//0
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
895 ... 0
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
896 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
897 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
898 >>> doctest.DocTestRunner(verbose=False).run(test)
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
899 ... # doctest: +ELLIPSIS
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
900 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
901 File ..., line 3, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
902 Failed example:
37266
5d625f589f2a Part of bug 1459808: fiddle so that this passes
Tim Peters <tim.peters@gmail.com>
parents: 35742
diff changeset
903 1//0
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
904 Exception raised:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
905 Traceback (most recent call last):
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
906 ...
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
907 ZeroDivisionError: integer division or modulo by zero
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
908 TestResults(failed=1, attempted=1)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
909 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
910 def optionflags(): r"""
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
911 Tests of `DocTestRunner`'s option flag handling.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
912
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
913 Several option flags can be used to customize the behavior of the test
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
914 runner. These are defined as module constants in doctest, and passed
44497
01d5b8e7d121 Doc patch #1730 from Robin Stocker; minor corrections mostly to os.rst.
Georg Brandl <georg@python.org>
parents: 44034
diff changeset
915 to the DocTestRunner constructor (multiple constants should be ORed
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
916 together).
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
917
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
918 The DONT_ACCEPT_TRUE_FOR_1 flag disables matches between True/False
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
919 and 1/0:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
920
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
921 >>> def f(x):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
922 ... '>>> True\n1\n'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
923
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
924 >>> # Without the flag:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
925 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
926 >>> doctest.DocTestRunner(verbose=False).run(test)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
927 TestResults(failed=0, attempted=1)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
928
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
929 >>> # With the flag:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
930 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
931 >>> flags = doctest.DONT_ACCEPT_TRUE_FOR_1
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
932 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
933 ... # doctest: +ELLIPSIS
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
934 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
935 File ..., line 2, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
936 Failed example:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
937 True
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
938 Expected:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
939 1
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
940 Got:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
941 True
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
942 TestResults(failed=1, attempted=1)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
943
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
944 The DONT_ACCEPT_BLANKLINE flag disables the match between blank lines
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
945 and the '<BLANKLINE>' marker:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
946
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
947 >>> def f(x):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
948 ... '>>> print "a\\n\\nb"\na\n<BLANKLINE>\nb\n'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
949
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
950 >>> # Without the flag:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
951 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
952 >>> doctest.DocTestRunner(verbose=False).run(test)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
953 TestResults(failed=0, attempted=1)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
954
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
955 >>> # With the flag:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
956 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
957 >>> flags = doctest.DONT_ACCEPT_BLANKLINE
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
958 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
959 ... # doctest: +ELLIPSIS
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
960 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
961 File ..., line 2, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
962 Failed example:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
963 print "a\n\nb"
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
964 Expected:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
965 a
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
966 <BLANKLINE>
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
967 b
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
968 Got:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
969 a
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
970 <BLANKLINE>
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
971 b
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
972 TestResults(failed=1, attempted=1)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
973
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
974 The NORMALIZE_WHITESPACE flag causes all sequences of whitespace to be
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
975 treated as equal:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
976
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
977 >>> def f(x):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
978 ... '>>> print 1, 2, 3\n 1 2\n 3'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
979
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
980 >>> # Without the flag:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
981 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
982 >>> doctest.DocTestRunner(verbose=False).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
983 ... # doctest: +ELLIPSIS
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
984 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
985 File ..., line 2, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
986 Failed example:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
987 print 1, 2, 3
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
988 Expected:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
989 1 2
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
990 3
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
991 Got:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
992 1 2 3
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
993 TestResults(failed=1, attempted=1)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
994
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
995 >>> # With the flag:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
996 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
997 >>> flags = doctest.NORMALIZE_WHITESPACE
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
998 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
999 TestResults(failed=0, attempted=1)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1000
33192
bbe68474e522 Now that they've settled down, document doctest directives.
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
1001 An example from the docs:
bbe68474e522 Now that they've settled down, document doctest directives.
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
1002 >>> print range(20) #doctest: +NORMALIZE_WHITESPACE
bbe68474e522 Now that they've settled down, document doctest directives.
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
1003 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
bbe68474e522 Now that they've settled down, document doctest directives.
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
1004 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
bbe68474e522 Now that they've settled down, document doctest directives.
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
1005
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1006 The ELLIPSIS flag causes ellipsis marker ("...") in the expected
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1007 output to match any substring in the actual output:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1008
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1009 >>> def f(x):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1010 ... '>>> print range(15)\n[0, 1, 2, ..., 14]\n'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1011
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1012 >>> # Without the flag:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1013 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1014 >>> doctest.DocTestRunner(verbose=False).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1015 ... # doctest: +ELLIPSIS
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1016 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1017 File ..., line 2, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1018 Failed example:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1019 print range(15)
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1020 Expected:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1021 [0, 1, 2, ..., 14]
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1022 Got:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1023 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1024 TestResults(failed=1, attempted=1)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1025
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1026 >>> # With the flag:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1027 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1028 >>> flags = doctest.ELLIPSIS
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1029 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1030 TestResults(failed=0, attempted=1)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1031
33217
8252e989cbb1 _ellipsis_match(): Removed special-casing of "...\n". The semantics
Tim Peters <tim.peters@gmail.com>
parents: 33192
diff changeset
1032 ... also matches nothing:
33186
4c78fcb60df3 ELLIPSIS implementation: an ellipsis couldn't match nothing if it
Tim Peters <tim.peters@gmail.com>
parents: 33165
diff changeset
1033
4c78fcb60df3 ELLIPSIS implementation: an ellipsis couldn't match nothing if it
Tim Peters <tim.peters@gmail.com>
parents: 33165
diff changeset
1034 >>> for i in range(100):
33217
8252e989cbb1 _ellipsis_match(): Removed special-casing of "...\n". The semantics
Tim Peters <tim.peters@gmail.com>
parents: 33192
diff changeset
1035 ... print i**2, #doctest: +ELLIPSIS
8252e989cbb1 _ellipsis_match(): Removed special-casing of "...\n". The semantics
Tim Peters <tim.peters@gmail.com>
parents: 33192
diff changeset
1036 0 1...4...9 16 ... 36 49 64 ... 9801
33186
4c78fcb60df3 ELLIPSIS implementation: an ellipsis couldn't match nothing if it
Tim Peters <tim.peters@gmail.com>
parents: 33165
diff changeset
1037
33192
bbe68474e522 Now that they've settled down, document doctest directives.
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
1038 ... can be surprising; e.g., this test passes:
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
1039
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
1040 >>> for i in range(21): #doctest: +ELLIPSIS
33217
8252e989cbb1 _ellipsis_match(): Removed special-casing of "...\n". The semantics
Tim Peters <tim.peters@gmail.com>
parents: 33192
diff changeset
1041 ... print i,
8252e989cbb1 _ellipsis_match(): Removed special-casing of "...\n". The semantics
Tim Peters <tim.peters@gmail.com>
parents: 33192
diff changeset
1042 0 1 2 ...1...2...0
33187
35bb62ac55fe Replaced the ELLIPSIS implementation with a worst-case linear-time one.
Tim Peters <tim.peters@gmail.com>
parents: 33186
diff changeset
1043
33192
bbe68474e522 Now that they've settled down, document doctest directives.
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
1044 Examples from the docs:
bbe68474e522 Now that they've settled down, document doctest directives.
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
1045
bbe68474e522 Now that they've settled down, document doctest directives.
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
1046 >>> print range(20) # doctest:+ELLIPSIS
bbe68474e522 Now that they've settled down, document doctest directives.
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
1047 [0, 1, ..., 18, 19]
bbe68474e522 Now that they've settled down, document doctest directives.
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
1048
bbe68474e522 Now that they've settled down, document doctest directives.
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
1049 >>> print range(20) # doctest: +ELLIPSIS
bbe68474e522 Now that they've settled down, document doctest directives.
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
1050 ... # doctest: +NORMALIZE_WHITESPACE
bbe68474e522 Now that they've settled down, document doctest directives.
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
1051 [0, 1, ..., 18, 19]
bbe68474e522 Now that they've settled down, document doctest directives.
Tim Peters <tim.peters@gmail.com>
parents: 33187
diff changeset
1052
37967
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1053 The SKIP flag causes an example to be skipped entirely. I.e., the
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1054 example is not run. It can be useful in contexts where doctest
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1055 examples serve as both documentation and test cases, and an example
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1056 should be included for documentation purposes, but should not be
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1057 checked (e.g., because its output is random, or depends on resources
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1058 which would be unavailable.) The SKIP flag can also be used for
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1059 'commenting out' broken examples.
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1060
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1061 >>> import unavailable_resource # doctest: +SKIP
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1062 >>> unavailable_resource.do_something() # doctest: +SKIP
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1063 >>> unavailable_resource.blow_up() # doctest: +SKIP
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1064 Traceback (most recent call last):
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1065 ...
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1066 UncheckedBlowUpError: Nobody checks me.
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1067
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1068 >>> import random
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1069 >>> print random.random() # doctest: +SKIP
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1070 0.721216923889
00dd1dae788b Patch #1475231: add a new SKIP doctest option, thanks to
Tim Peters <tim.peters@gmail.com>
parents: 37266
diff changeset
1071
33284
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1072 The REPORT_UDIFF flag causes failures that involve multi-line expected
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1073 and actual outputs to be displayed using a unified diff:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1074
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1075 >>> def f(x):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1076 ... r'''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1077 ... >>> print '\n'.join('abcdefg')
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1078 ... a
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1079 ... B
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1080 ... c
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1081 ... d
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1082 ... f
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1083 ... g
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1084 ... h
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1085 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1086
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1087 >>> # Without the flag:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1088 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1089 >>> doctest.DocTestRunner(verbose=False).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1090 ... # doctest: +ELLIPSIS
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1091 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1092 File ..., line 3, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1093 Failed example:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1094 print '\n'.join('abcdefg')
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1095 Expected:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1096 a
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1097 B
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1098 c
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1099 d
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1100 f
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1101 g
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1102 h
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1103 Got:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1104 a
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1105 b
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1106 c
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1107 d
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1108 e
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1109 f
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1110 g
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1111 TestResults(failed=1, attempted=1)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1112
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1113 >>> # With the flag:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1114 >>> test = doctest.DocTestFinder().find(f)[0]
33284
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1115 >>> flags = doctest.REPORT_UDIFF
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1116 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1117 ... # doctest: +ELLIPSIS
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1118 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1119 File ..., line 3, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1120 Failed example:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1121 print '\n'.join('abcdefg')
33283
2747dbd5938b Shortened diff output for unified & context diffs
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33282
diff changeset
1122 Differences (unified diff with -expected +actual):
33296
3e25d6e3da19 output_difference(): In fancy-diff cases, the way this split expected &
Tim Peters <tim.peters@gmail.com>
parents: 33286
diff changeset
1123 @@ -1,7 +1,7 @@
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1124 a
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1125 -B
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1126 +b
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1127 c
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1128 d
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1129 +e
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1130 f
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1131 g
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1132 -h
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1133 TestResults(failed=1, attempted=1)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1134
33284
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1135 The REPORT_CDIFF flag causes failures that involve multi-line expected
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1136 and actual outputs to be displayed using a context diff:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1137
33284
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1138 >>> # Reuse f() from the REPORT_UDIFF example, above.
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1139 >>> test = doctest.DocTestFinder().find(f)[0]
33284
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1140 >>> flags = doctest.REPORT_CDIFF
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1141 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1142 ... # doctest: +ELLIPSIS
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1143 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1144 File ..., line 3, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1145 Failed example:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1146 print '\n'.join('abcdefg')
33283
2747dbd5938b Shortened diff output for unified & context diffs
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33282
diff changeset
1147 Differences (context diff with expected followed by actual):
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1148 ***************
33296
3e25d6e3da19 output_difference(): In fancy-diff cases, the way this split expected &
Tim Peters <tim.peters@gmail.com>
parents: 33286
diff changeset
1149 *** 1,7 ****
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1150 a
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1151 ! B
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1152 c
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1153 d
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1154 f
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1155 g
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1156 - h
33296
3e25d6e3da19 output_difference(): In fancy-diff cases, the way this split expected &
Tim Peters <tim.peters@gmail.com>
parents: 33286
diff changeset
1157 --- 1,7 ----
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1158 a
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1159 ! b
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1160 c
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1161 d
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1162 + e
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1163 f
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1164 g
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1165 TestResults(failed=1, attempted=1)
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1166
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1167
33284
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1168 The REPORT_NDIFF flag causes failures to use the difflib.Differ algorithm
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1169 used by the popular ndiff.py utility. This does intraline difference
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1170 marking, as well as interline differences.
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1171
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1172 >>> def f(x):
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1173 ... r'''
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1174 ... >>> print "a b c d e f g h i j k l m"
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1175 ... a b c d e f g h i j k 1 m
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1176 ... '''
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1177 >>> test = doctest.DocTestFinder().find(f)[0]
33284
345fac66daa9 Renamed UNIFIED_DIFF->REPORT_UDIFF; CONTEXT_DIFF->REPORT_CDIFF; and
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33283
diff changeset
1178 >>> flags = doctest.REPORT_NDIFF
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1179 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1180 ... # doctest: +ELLIPSIS
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1181 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1182 File ..., line 3, in f
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1183 Failed example:
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1184 print "a b c d e f g h i j k l m"
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1185 Differences (ndiff with -expected +actual):
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1186 - a b c d e f g h i j k 1 m
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1187 ? ^
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1188 + a b c d e f g h i j k l m
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1189 ? + ++ ^
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1190 TestResults(failed=1, attempted=1)
33286
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1191
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1192 The REPORT_ONLY_FIRST_FAILURE supresses result output after the first
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1193 failing example:
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1194
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1195 >>> def f(x):
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1196 ... r'''
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1197 ... >>> print 1 # first success
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1198 ... 1
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1199 ... >>> print 2 # first failure
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1200 ... 200
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1201 ... >>> print 3 # second failure
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1202 ... 300
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1203 ... >>> print 4 # second success
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1204 ... 4
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1205 ... >>> print 5 # third failure
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1206 ... 500
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1207 ... '''
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1208 >>> test = doctest.DocTestFinder().find(f)[0]
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1209 >>> flags = doctest.REPORT_ONLY_FIRST_FAILURE
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1210 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1211 ... # doctest: +ELLIPSIS
33286
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1212 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1213 File ..., line 5, in f
33286
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1214 Failed example:
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1215 print 2 # first failure
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1216 Expected:
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1217 200
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1218 Got:
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1219 2
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1220 TestResults(failed=3, attempted=5)
33286
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1221
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1222 However, output from `report_start` is not supressed:
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1223
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1224 >>> doctest.DocTestRunner(verbose=True, optionflags=flags).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1225 ... # doctest: +ELLIPSIS
33286
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1226 Trying:
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1227 print 1 # first success
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1228 Expecting:
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1229 1
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1230 ok
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1231 Trying:
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1232 print 2 # first failure
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1233 Expecting:
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1234 200
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1235 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1236 File ..., line 5, in f
33286
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1237 Failed example:
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1238 print 2 # first failure
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1239 Expected:
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1240 200
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1241 Got:
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1242 2
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1243 TestResults(failed=3, attempted=5)
33286
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1244
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1245 For the purposes of REPORT_ONLY_FIRST_FAILURE, unexpected exceptions
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1246 count as failures:
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1247
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1248 >>> def f(x):
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1249 ... r'''
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1250 ... >>> print 1 # first success
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1251 ... 1
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1252 ... >>> raise ValueError(2) # first failure
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1253 ... 200
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1254 ... >>> print 3 # second failure
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1255 ... 300
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1256 ... >>> print 4 # second success
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1257 ... 4
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1258 ... >>> print 5 # third failure
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1259 ... 500
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1260 ... '''
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1261 >>> test = doctest.DocTestFinder().find(f)[0]
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1262 >>> flags = doctest.REPORT_ONLY_FIRST_FAILURE
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1263 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1264 ... # doctest: +ELLIPSIS
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1265 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1266 File ..., line 5, in f
33286
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1267 Failed example:
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1268 raise ValueError(2) # first failure
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1269 Exception raised:
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1270 ...
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1271 ValueError: 2
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1272 TestResults(failed=3, attempted=5)
33286
e586cbecdeba Added REPORT_ONLY_FIRST_FAILURE flag, which supresses output after the
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33284
diff changeset
1273
38136
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1274 New option flags can also be registered, via register_optionflag(). Here
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1275 we reach into doctest's internals a bit.
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1276
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1277 >>> unlikely = "UNLIKELY_OPTION_NAME"
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1278 >>> unlikely in doctest.OPTIONFLAGS_BY_NAME
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1279 False
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1280 >>> new_flag_value = doctest.register_optionflag(unlikely)
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1281 >>> unlikely in doctest.OPTIONFLAGS_BY_NAME
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1282 True
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1283
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1284 Before 2.4.4/2.5, registering a name more than once erroneously created
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1285 more than one flag value. Here we verify that's fixed:
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1286
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1287 >>> redundant_flag_value = doctest.register_optionflag(unlikely)
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1288 >>> redundant_flag_value == new_flag_value
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1289 True
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1290
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1291 Clean up.
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1292 >>> del doctest.OPTIONFLAGS_BY_NAME[unlikely]
0606b9c08ac2 Variant of patch #1478292. doctest.register_optionflag(name)
Tim Peters <tim.peters@gmail.com>
parents: 37967
diff changeset
1293
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1294 """
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
1295
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1296 def option_directives(): r"""
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1297 Tests of `DocTestRunner`'s option directive mechanism.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1298
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1299 Option directives can be used to turn option flags on or off for a
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1300 single example. To turn an option on for an example, follow that
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1301 example with a comment of the form ``# doctest: +OPTION``:
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1302
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1303 >>> def f(x): r'''
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1304 ... >>> print range(10) # should fail: no ellipsis
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1305 ... [0, 1, ..., 9]
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1306 ...
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1307 ... >>> print range(10) # doctest: +ELLIPSIS
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1308 ... [0, 1, ..., 9]
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1309 ... '''
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1310 >>> test = doctest.DocTestFinder().find(f)[0]
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1311 >>> doctest.DocTestRunner(verbose=False).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1312 ... # doctest: +ELLIPSIS
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1313 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1314 File ..., line 2, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1315 Failed example:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1316 print range(10) # should fail: no ellipsis
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1317 Expected:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1318 [0, 1, ..., 9]
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1319 Got:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1320 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1321 TestResults(failed=1, attempted=2)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1322
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1323 To turn an option off for an example, follow that example with a
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1324 comment of the form ``# doctest: -OPTION``:
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1325
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1326 >>> def f(x): r'''
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1327 ... >>> print range(10)
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1328 ... [0, 1, ..., 9]
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1329 ...
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1330 ... >>> # should fail: no ellipsis
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1331 ... >>> print range(10) # doctest: -ELLIPSIS
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1332 ... [0, 1, ..., 9]
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1333 ... '''
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1334 >>> test = doctest.DocTestFinder().find(f)[0]
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1335 >>> doctest.DocTestRunner(verbose=False,
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1336 ... optionflags=doctest.ELLIPSIS).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1337 ... # doctest: +ELLIPSIS
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1338 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1339 File ..., line 6, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1340 Failed example:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1341 print range(10) # doctest: -ELLIPSIS
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1342 Expected:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1343 [0, 1, ..., 9]
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1344 Got:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1345 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1346 TestResults(failed=1, attempted=2)
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1347
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1348 Option directives affect only the example that they appear with; they
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1349 do not change the options for surrounding examples:
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
1350
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1351 >>> def f(x): r'''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1352 ... >>> print range(10) # Should fail: no ellipsis
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1353 ... [0, 1, ..., 9]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1354 ...
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1355 ... >>> print range(10) # doctest: +ELLIPSIS
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1356 ... [0, 1, ..., 9]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1357 ...
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1358 ... >>> print range(10) # Should fail: no ellipsis
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1359 ... [0, 1, ..., 9]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1360 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1361 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1362 >>> doctest.DocTestRunner(verbose=False).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1363 ... # doctest: +ELLIPSIS
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1364 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1365 File ..., line 2, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1366 Failed example:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1367 print range(10) # Should fail: no ellipsis
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1368 Expected:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1369 [0, 1, ..., 9]
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1370 Got:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1371 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1372 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1373 File ..., line 8, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1374 Failed example:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1375 print range(10) # Should fail: no ellipsis
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1376 Expected:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1377 [0, 1, ..., 9]
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1378 Got:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1379 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1380 TestResults(failed=2, attempted=3)
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1381
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1382 Multiple options may be modified by a single option directive. They
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1383 may be separated by whitespace, commas, or both:
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1384
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1385 >>> def f(x): r'''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1386 ... >>> print range(10) # Should fail
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1387 ... [0, 1, ..., 9]
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1388 ... >>> print range(10) # Should succeed
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1389 ... ... # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1390 ... [0, 1, ..., 9]
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1391 ... '''
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1392 >>> test = doctest.DocTestFinder().find(f)[0]
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1393 >>> doctest.DocTestRunner(verbose=False).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1394 ... # doctest: +ELLIPSIS
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1395 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1396 File ..., line 2, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1397 Failed example:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1398 print range(10) # Should fail
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1399 Expected:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1400 [0, 1, ..., 9]
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1401 Got:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1402 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1403 TestResults(failed=1, attempted=2)
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1404
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1405 >>> def f(x): r'''
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1406 ... >>> print range(10) # Should fail
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1407 ... [0, 1, ..., 9]
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1408 ... >>> print range(10) # Should succeed
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1409 ... ... # doctest: +ELLIPSIS,+NORMALIZE_WHITESPACE
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1410 ... [0, 1, ..., 9]
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1411 ... '''
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1412 >>> test = doctest.DocTestFinder().find(f)[0]
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1413 >>> doctest.DocTestRunner(verbose=False).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1414 ... # doctest: +ELLIPSIS
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1415 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1416 File ..., line 2, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1417 Failed example:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1418 print range(10) # Should fail
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1419 Expected:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1420 [0, 1, ..., 9]
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1421 Got:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1422 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1423 TestResults(failed=1, attempted=2)
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1424
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1425 >>> def f(x): r'''
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1426 ... >>> print range(10) # Should fail
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1427 ... [0, 1, ..., 9]
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1428 ... >>> print range(10) # Should succeed
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1429 ... ... # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1430 ... [0, 1, ..., 9]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1431 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1432 >>> test = doctest.DocTestFinder().find(f)[0]
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1433 >>> doctest.DocTestRunner(verbose=False).run(test)
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1434 ... # doctest: +ELLIPSIS
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1435 **********************************************************************
33459
9497c58126be Recover from inspect.getmodule() changes. It returns a module for
Tim Peters <tim.peters@gmail.com>
parents: 33404
diff changeset
1436 File ..., line 2, in f
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1437 Failed example:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1438 print range(10) # Should fail
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1439 Expected:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1440 [0, 1, ..., 9]
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1441 Got:
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
1442 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1443 TestResults(failed=1, attempted=2)
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1444
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1445 The option directive may be put on the line following the source, as
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1446 long as a continuation prompt is used:
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1447
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1448 >>> def f(x): r'''
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1449 ... >>> print range(10)
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1450 ... ... # doctest: +ELLIPSIS
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1451 ... [0, 1, ..., 9]
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1452 ... '''
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1453 >>> test = doctest.DocTestFinder().find(f)[0]
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1454 >>> doctest.DocTestRunner(verbose=False).run(test)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1455 TestResults(failed=0, attempted=1)
33088
9937d0e63ca3 - Added __docformat__
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33087
diff changeset
1456
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1457 For examples with multi-line source, the option directive may appear
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1458 at the end of any line:
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1459
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1460 >>> def f(x): r'''
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1461 ... >>> for x in range(10): # doctest: +ELLIPSIS
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1462 ... ... print x,
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1463 ... 0 1 2 ... 9
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1464 ...
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1465 ... >>> for x in range(10):
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1466 ... ... print x, # doctest: +ELLIPSIS
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1467 ... 0 1 2 ... 9
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1468 ... '''
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1469 >>> test = doctest.DocTestFinder().find(f)[0]
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1470 >>> doctest.DocTestRunner(verbose=False).run(test)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1471 TestResults(failed=0, attempted=2)
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1472
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1473 If more than one line of an example with multi-line source has an
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1474 option directive, then they are combined:
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1475
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1476 >>> def f(x): r'''
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1477 ... Should fail (option directive not on the last line):
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1478 ... >>> for x in range(10): # doctest: +ELLIPSIS
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1479 ... ... print x, # doctest: +NORMALIZE_WHITESPACE
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1480 ... 0 1 2...9
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1481 ... '''
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1482 >>> test = doctest.DocTestFinder().find(f)[0]
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1483 >>> doctest.DocTestRunner(verbose=False).run(test)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1484 TestResults(failed=0, attempted=1)
33087
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1485
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1486 It is an error to have a comment of the form ``# doctest:`` that is
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1487 *not* followed by words of the form ``+OPTION`` or ``-OPTION``, where
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1488 ``OPTION`` is an option that has been registered with
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1489 `register_option`:
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1490
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1491 >>> # Error: Option not registered
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1492 >>> s = '>>> print 12 #doctest: +BADOPTION'
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1493 >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0)
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1494 Traceback (most recent call last):
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1495 ValueError: line 1 of the doctest for s has an invalid option: '+BADOPTION'
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1496
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1497 >>> # Error: No + or - prefix
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1498 >>> s = '>>> print 12 #doctest: ELLIPSIS'
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1499 >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0)
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1500 Traceback (most recent call last):
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1501 ValueError: line 1 of the doctest for s has an invalid option: 'ELLIPSIS'
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1502
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1503 It is an error to use an option directive on a line that contains no
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1504 source:
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1505
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1506 >>> s = '>>> # doctest: +ELLIPSIS'
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1507 >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0)
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1508 Traceback (most recent call last):
5347da31f076 - Changed option directives to be example-specific. (i.e., they now
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33073
diff changeset
1509 ValueError: line 0 of the doctest for s has an option directive on a line with no example: '# doctest: +ELLIPSIS'
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1510 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1511
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1512 def test_testsource(): r"""
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1513 Unit tests for `testsource()`.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1514
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1515 The testsource() function takes a module and a name, finds the (first)
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1516 test with that name in that module, and converts it to a script. The
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1517 example code is converted to regular Python code. The surrounding
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1518 words and expected output are converted to comments:
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1519
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1520 >>> import test.test_doctest
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1521 >>> name = 'test.test_doctest.sample_func'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1522 >>> print doctest.testsource(test.test_doctest, name)
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
1523 # Blah blah
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1524 #
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1525 print sample_func(22)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1526 # Expected:
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
1527 ## 44
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1528 #
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
1529 # Yee ha!
35167
021a4abf7f27 Fix test cases for doctest.
Georg Brandl <georg@python.org>
parents: 34215
diff changeset
1530 <BLANKLINE>
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1531
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1532 >>> name = 'test.test_doctest.SampleNewStyleClass'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1533 >>> print doctest.testsource(test.test_doctest, name)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1534 print '1\n2\n3'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1535 # Expected:
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
1536 ## 1
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
1537 ## 2
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
1538 ## 3
35167
021a4abf7f27 Fix test cases for doctest.
Georg Brandl <georg@python.org>
parents: 34215
diff changeset
1539 <BLANKLINE>
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1540
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1541 >>> name = 'test.test_doctest.SampleClass.a_classmethod'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1542 >>> print doctest.testsource(test.test_doctest, name)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1543 print SampleClass.a_classmethod(10)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1544 # Expected:
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
1545 ## 12
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1546 print SampleClass(0).a_classmethod(10)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1547 # Expected:
33089
4fd18505fc35 - Changed output of DocTestParser.get_program() to make it easier to
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33088
diff changeset
1548 ## 12
35167
021a4abf7f27 Fix test cases for doctest.
Georg Brandl <georg@python.org>
parents: 34215
diff changeset
1549 <BLANKLINE>
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1550 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1551
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1552 def test_debug(): r"""
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1553
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1554 Create a docstring that we want to debug:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1555
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1556 >>> s = '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1557 ... >>> x = 12
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1558 ... >>> print x
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1559 ... 12
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1560 ... '''
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1561
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1562 Create some fake stdin input, to feed to the debugger:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1563
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1564 >>> import tempfile
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1565 >>> real_stdin = sys.stdin
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1566 >>> sys.stdin = _FakeInput(['next', 'print x', 'continue'])
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1567
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1568 Run the debugger on the docstring, and then restore sys.stdin.
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1569
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1570 >>> try: doctest.debug_src(s)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1571 ... finally: sys.stdin = real_stdin
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35167
diff changeset
1572 > <string>(1)<module>()
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1573 (Pdb) next
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1574 12
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1575 --Return--
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35167
diff changeset
1576 > <string>(1)<module>()->None
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1577 (Pdb) print x
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1578 12
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1579 (Pdb) continue
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1580
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1581 """
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
1582
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1583 def test_pdb_set_trace():
33944
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1584 """Using pdb.set_trace from a doctest.
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1585
33071
04d9fde11330 This started as a spelling and whitespace cleanup. The comment for
Tim Peters <tim.peters@gmail.com>
parents: 33065
diff changeset
1586 You can use pdb.set_trace from a doctest. To do so, you must
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1587 retrieve the set_trace function from the pdb module at the time
33071
04d9fde11330 This started as a spelling and whitespace cleanup. The comment for
Tim Peters <tim.peters@gmail.com>
parents: 33065
diff changeset
1588 you use it. The doctest module changes sys.stdout so that it can
04d9fde11330 This started as a spelling and whitespace cleanup. The comment for
Tim Peters <tim.peters@gmail.com>
parents: 33065
diff changeset
1589 capture program output. It also temporarily replaces pdb.set_trace
04d9fde11330 This started as a spelling and whitespace cleanup. The comment for
Tim Peters <tim.peters@gmail.com>
parents: 33065
diff changeset
1590 with a version that restores stdout. This is necessary for you to
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1591 see debugger output.
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1592
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1593 >>> doc = '''
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1594 ... >>> x = 42
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1595 ... >>> import pdb; pdb.set_trace()
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1596 ... '''
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
1597 >>> parser = doctest.DocTestParser()
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
1598 >>> test = parser.get_doctest(doc, {}, "foo", "foo.py", 0)
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1599 >>> runner = doctest.DocTestRunner(verbose=False)
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1600
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1601 To demonstrate this, we'll create a fake standard input that
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1602 captures our debugger input:
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1603
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1604 >>> import tempfile
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1605 >>> real_stdin = sys.stdin
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1606 >>> sys.stdin = _FakeInput([
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1607 ... 'print x', # print data defined by the example
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1608 ... 'continue', # stop debugging
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1609 ... ''])
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1610
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1611 >>> try: runner.run(test)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1612 ... finally: sys.stdin = real_stdin
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1613 --Return--
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35167
diff changeset
1614 > <doctest foo[1]>(1)<module>()->None
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1615 -> import pdb; pdb.set_trace()
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1616 (Pdb) print x
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1617 42
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1618 (Pdb) continue
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1619 TestResults(failed=0, attempted=2)
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1620
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1621 You can also put pdb.set_trace in a function called from a test:
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1622
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1623 >>> def calls_set_trace():
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1624 ... y=2
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1625 ... import pdb; pdb.set_trace()
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1626
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1627 >>> doc = '''
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1628 ... >>> x=1
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1629 ... >>> calls_set_trace()
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1630 ... '''
33072
4dd1815b44ef - DocTest is now a simple container class; its constructor is no longer
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33071
diff changeset
1631 >>> test = parser.get_doctest(doc, globals(), "foo", "foo.py", 0)
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1632 >>> real_stdin = sys.stdin
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1633 >>> sys.stdin = _FakeInput([
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1634 ... 'print y', # print data defined in the function
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1635 ... 'up', # out of function
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1636 ... 'print x', # print data defined by the example
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1637 ... 'continue', # stop debugging
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1638 ... ''])
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1639
33944
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1640 >>> try:
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1641 ... runner.run(test)
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1642 ... finally:
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1643 ... sys.stdin = real_stdin
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1644 --Return--
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1645 > <doctest test.test_doctest.test_pdb_set_trace[8]>(3)calls_set_trace()->None
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1646 -> import pdb; pdb.set_trace()
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1647 (Pdb) print y
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1648 2
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1649 (Pdb) up
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35167
diff changeset
1650 > <doctest foo[1]>(1)<module>()
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1651 -> calls_set_trace()
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1652 (Pdb) print x
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1653 1
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1654 (Pdb) continue
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1655 TestResults(failed=0, attempted=2)
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1656
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1657 During interactive debugging, source code is shown, even for
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1658 doctest examples:
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1659
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1660 >>> doc = '''
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1661 ... >>> def f(x):
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1662 ... ... g(x*2)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1663 ... >>> def g(x):
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1664 ... ... print x+3
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1665 ... ... import pdb; pdb.set_trace()
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1666 ... >>> f(3)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1667 ... '''
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1668 >>> test = parser.get_doctest(doc, globals(), "foo", "foo.py", 0)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1669 >>> real_stdin = sys.stdin
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1670 >>> sys.stdin = _FakeInput([
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1671 ... 'list', # list source from example 2
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1672 ... 'next', # return from g()
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1673 ... 'list', # list source from example 1
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1674 ... 'next', # return from f()
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1675 ... 'list', # list source from example 3
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1676 ... 'continue', # stop debugging
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1677 ... ''])
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1678 >>> try: runner.run(test)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1679 ... finally: sys.stdin = real_stdin
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1680 ... # doctest: +NORMALIZE_WHITESPACE
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1681 --Return--
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1682 > <doctest foo[1]>(3)g()->None
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1683 -> import pdb; pdb.set_trace()
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1684 (Pdb) list
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1685 1 def g(x):
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1686 2 print x+3
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1687 3 -> import pdb; pdb.set_trace()
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1688 [EOF]
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1689 (Pdb) next
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1690 --Return--
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1691 > <doctest foo[0]>(2)f()->None
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1692 -> g(x*2)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1693 (Pdb) list
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1694 1 def f(x):
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1695 2 -> g(x*2)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1696 [EOF]
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1697 (Pdb) next
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1698 --Return--
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35167
diff changeset
1699 > <doctest foo[2]>(1)<module>()->None
33301
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1700 -> f(3)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1701 (Pdb) list
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1702 1 -> f(3)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1703 [EOF]
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1704 (Pdb) continue
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1705 **********************************************************************
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1706 File "foo.py", line 7, in foo
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1707 Failed example:
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1708 f(3)
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1709 Expected nothing
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1710 Got:
b1c26e89c464 - Removed redundant call to expandtabs in DocTestParesr.
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33299
diff changeset
1711 9
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1712 TestResults(failed=1, attempted=3)
33065
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1713 """
45329d9fdb8d Added support for pdb.set_trace.
Jim Fulton <jim@zope.com>
parents: 33060
diff changeset
1714
33944
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1715 def test_pdb_set_trace_nested():
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1716 """This illustrates more-demanding use of set_trace with nested functions.
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1717
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1718 >>> class C(object):
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1719 ... def calls_set_trace(self):
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1720 ... y = 1
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1721 ... import pdb; pdb.set_trace()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1722 ... self.f1()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1723 ... y = 2
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1724 ... def f1(self):
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1725 ... x = 1
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1726 ... self.f2()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1727 ... x = 2
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1728 ... def f2(self):
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1729 ... z = 1
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1730 ... z = 2
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1731
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1732 >>> calls_set_trace = C().calls_set_trace
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1733
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1734 >>> doc = '''
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1735 ... >>> a = 1
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1736 ... >>> calls_set_trace()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1737 ... '''
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1738 >>> parser = doctest.DocTestParser()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1739 >>> runner = doctest.DocTestRunner(verbose=False)
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1740 >>> test = parser.get_doctest(doc, globals(), "foo", "foo.py", 0)
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1741 >>> real_stdin = sys.stdin
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1742 >>> sys.stdin = _FakeInput([
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1743 ... 'print y', # print data defined in the function
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1744 ... 'step', 'step', 'step', 'step', 'step', 'step', 'print z',
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1745 ... 'up', 'print x',
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1746 ... 'up', 'print y',
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1747 ... 'up', 'print foo',
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1748 ... 'continue', # stop debugging
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1749 ... ''])
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1750
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1751 >>> try:
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1752 ... runner.run(test)
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1753 ... finally:
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1754 ... sys.stdin = real_stdin
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1755 > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(5)calls_set_trace()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1756 -> self.f1()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1757 (Pdb) print y
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1758 1
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1759 (Pdb) step
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1760 --Call--
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1761 > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(7)f1()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1762 -> def f1(self):
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1763 (Pdb) step
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1764 > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(8)f1()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1765 -> x = 1
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1766 (Pdb) step
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1767 > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(9)f1()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1768 -> self.f2()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1769 (Pdb) step
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1770 --Call--
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1771 > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(11)f2()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1772 -> def f2(self):
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1773 (Pdb) step
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1774 > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(12)f2()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1775 -> z = 1
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1776 (Pdb) step
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1777 > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(13)f2()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1778 -> z = 2
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1779 (Pdb) print z
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1780 1
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1781 (Pdb) up
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1782 > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(9)f1()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1783 -> self.f2()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1784 (Pdb) print x
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1785 1
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1786 (Pdb) up
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1787 > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(5)calls_set_trace()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1788 -> self.f1()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1789 (Pdb) print y
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1790 1
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1791 (Pdb) up
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35167
diff changeset
1792 > <doctest foo[1]>(1)<module>()
33944
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1793 -> calls_set_trace()
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1794 (Pdb) print foo
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1795 *** NameError: name 'foo' is not defined
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1796 (Pdb) continue
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
1797 TestResults(failed=0, attempted=2)
33944
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1798 """
96179c096d65 test_doctest.py test_pdb_set_trace_nested(): A new test from Jim Fulton
Tim Peters <tim.peters@gmail.com>
parents: 33618
diff changeset
1799
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1800 def test_DocTestSuite():
32982
ae8772d15a5f Bug 772091: doctest.DocTestSuite does not support __test__
Tim Peters <tim.peters@gmail.com>
parents: 32979
diff changeset
1801 """DocTestSuite creates a unittest test suite from a doctest.
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1802
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1803 We create a Suite by providing a module. A module can be provided
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1804 by passing a module object:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1805
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1806 >>> import unittest
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1807 >>> import test.sample_doctest
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1808 >>> suite = doctest.DocTestSuite(test.sample_doctest)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1809 >>> suite.run(unittest.TestResult())
32982
ae8772d15a5f Bug 772091: doctest.DocTestSuite does not support __test__
Tim Peters <tim.peters@gmail.com>
parents: 32979
diff changeset
1810 <unittest.TestResult run=9 errors=0 failures=4>
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1811
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1812 We can also supply the module by name:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1813
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1814 >>> suite = doctest.DocTestSuite('test.sample_doctest')
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1815 >>> suite.run(unittest.TestResult())
32982
ae8772d15a5f Bug 772091: doctest.DocTestSuite does not support __test__
Tim Peters <tim.peters@gmail.com>
parents: 32979
diff changeset
1816 <unittest.TestResult run=9 errors=0 failures=4>
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1817
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1818 We can use the current module:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1819
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1820 >>> suite = test.sample_doctest.test_suite()
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1821 >>> suite.run(unittest.TestResult())
32982
ae8772d15a5f Bug 772091: doctest.DocTestSuite does not support __test__
Tim Peters <tim.peters@gmail.com>
parents: 32979
diff changeset
1822 <unittest.TestResult run=9 errors=0 failures=4>
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1823
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1824 We can supply global variables. If we pass globs, they will be
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1825 used instead of the module globals. Here we'll pass an empty
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1826 globals, triggering an extra error:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1827
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1828 >>> suite = doctest.DocTestSuite('test.sample_doctest', globs={})
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1829 >>> suite.run(unittest.TestResult())
32982
ae8772d15a5f Bug 772091: doctest.DocTestSuite does not support __test__
Tim Peters <tim.peters@gmail.com>
parents: 32979
diff changeset
1830 <unittest.TestResult run=9 errors=0 failures=5>
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1831
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1832 Alternatively, we can provide extra globals. Here we'll make an
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1833 error go away by providing an extra global variable:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1834
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1835 >>> suite = doctest.DocTestSuite('test.sample_doctest',
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1836 ... extraglobs={'y': 1})
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1837 >>> suite.run(unittest.TestResult())
32982
ae8772d15a5f Bug 772091: doctest.DocTestSuite does not support __test__
Tim Peters <tim.peters@gmail.com>
parents: 32979
diff changeset
1838 <unittest.TestResult run=9 errors=0 failures=3>
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1839
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1840 You can pass option flags. Here we'll cause an extra error
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1841 by disabling the blank-line feature:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1842
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1843 >>> suite = doctest.DocTestSuite('test.sample_doctest',
32982
ae8772d15a5f Bug 772091: doctest.DocTestSuite does not support __test__
Tim Peters <tim.peters@gmail.com>
parents: 32979
diff changeset
1844 ... optionflags=doctest.DONT_ACCEPT_BLANKLINE)
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1845 >>> suite.run(unittest.TestResult())
32982
ae8772d15a5f Bug 772091: doctest.DocTestSuite does not support __test__
Tim Peters <tim.peters@gmail.com>
parents: 32979
diff changeset
1846 <unittest.TestResult run=9 errors=0 failures=5>
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1847
32982
ae8772d15a5f Bug 772091: doctest.DocTestSuite does not support __test__
Tim Peters <tim.peters@gmail.com>
parents: 32979
diff changeset
1848 You can supply setUp and tearDown functions:
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1849
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1850 >>> def setUp(t):
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1851 ... import test.test_doctest
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1852 ... test.test_doctest.sillySetup = True
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1853
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1854 >>> def tearDown(t):
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1855 ... import test.test_doctest
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1856 ... del test.test_doctest.sillySetup
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1857
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1858 Here, we installed a silly variable that the test expects:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1859
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1860 >>> suite = doctest.DocTestSuite('test.sample_doctest',
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1861 ... setUp=setUp, tearDown=tearDown)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1862 >>> suite.run(unittest.TestResult())
32982
ae8772d15a5f Bug 772091: doctest.DocTestSuite does not support __test__
Tim Peters <tim.peters@gmail.com>
parents: 32979
diff changeset
1863 <unittest.TestResult run=9 errors=0 failures=3>
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1864
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1865 But the tearDown restores sanity:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1866
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1867 >>> import test.test_doctest
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1868 >>> test.test_doctest.sillySetup
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1869 Traceback (most recent call last):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1870 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1871 AttributeError: 'module' object has no attribute 'sillySetup'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1872
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1873 The setUp and tearDown funtions are passed test objects. Here
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1874 we'll use the setUp function to supply the missing variable y:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1875
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1876 >>> def setUp(test):
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1877 ... test.globs['y'] = 1
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1878
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1879 >>> suite = doctest.DocTestSuite('test.sample_doctest', setUp=setUp)
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1880 >>> suite.run(unittest.TestResult())
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1881 <unittest.TestResult run=9 errors=0 failures=3>
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1882
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1883 Here, we didn't need to use a tearDown function because we
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1884 modified the test globals, which are a copy of the
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1885 sample_doctest module dictionary. The test globals are
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1886 automatically cleared for us after a test.
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1887 """
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1888
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1889 def test_DocFileSuite():
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1890 """We can test tests found in text files using a DocFileSuite.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1891
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1892 We create a suite by providing the names of one or more text
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1893 files that include examples:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1894
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1895 >>> import unittest
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1896 >>> suite = doctest.DocFileSuite('test_doctest.txt',
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
1897 ... 'test_doctest2.txt',
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
1898 ... 'test_doctest4.txt')
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1899 >>> suite.run(unittest.TestResult())
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
1900 <unittest.TestResult run=3 errors=0 failures=3>
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1901
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1902 The test files are looked for in the directory containing the
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1903 calling module. A package keyword argument can be provided to
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1904 specify a different relative location.
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1905
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1906 >>> import unittest
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1907 >>> suite = doctest.DocFileSuite('test_doctest.txt',
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1908 ... 'test_doctest2.txt',
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
1909 ... 'test_doctest4.txt',
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1910 ... package='test')
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1911 >>> suite.run(unittest.TestResult())
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
1912 <unittest.TestResult run=3 errors=0 failures=3>
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1913
43938
8636dcb801ce doctest assumed that a package's __loader__.get_data() method used universal
Brett Cannon <bcannon@gmail.com>
parents: 43934
diff changeset
1914 Support for using a package's __loader__.get_data() is also
8636dcb801ce doctest assumed that a package's __loader__.get_data() method used universal
Brett Cannon <bcannon@gmail.com>
parents: 43934
diff changeset
1915 provided.
8636dcb801ce doctest assumed that a package's __loader__.get_data() method used universal
Brett Cannon <bcannon@gmail.com>
parents: 43934
diff changeset
1916
8636dcb801ce doctest assumed that a package's __loader__.get_data() method used universal
Brett Cannon <bcannon@gmail.com>
parents: 43934
diff changeset
1917 >>> import unittest, pkgutil, test
43977
3c725a43ac8c Fix a bug in the test for using __loader__.get_data().
Brett Cannon <bcannon@gmail.com>
parents: 43940
diff changeset
1918 >>> added_loader = False
43938
8636dcb801ce doctest assumed that a package's __loader__.get_data() method used universal
Brett Cannon <bcannon@gmail.com>
parents: 43934
diff changeset
1919 >>> if not hasattr(test, '__loader__'):
8636dcb801ce doctest assumed that a package's __loader__.get_data() method used universal
Brett Cannon <bcannon@gmail.com>
parents: 43934
diff changeset
1920 ... test.__loader__ = pkgutil.get_loader(test)
8636dcb801ce doctest assumed that a package's __loader__.get_data() method used universal
Brett Cannon <bcannon@gmail.com>
parents: 43934
diff changeset
1921 ... added_loader = True
8636dcb801ce doctest assumed that a package's __loader__.get_data() method used universal
Brett Cannon <bcannon@gmail.com>
parents: 43934
diff changeset
1922 >>> try:
8636dcb801ce doctest assumed that a package's __loader__.get_data() method used universal
Brett Cannon <bcannon@gmail.com>
parents: 43934
diff changeset
1923 ... suite = doctest.DocFileSuite('test_doctest.txt',
8636dcb801ce doctest assumed that a package's __loader__.get_data() method used universal
Brett Cannon <bcannon@gmail.com>
parents: 43934
diff changeset
1924 ... 'test_doctest2.txt',
8636dcb801ce doctest assumed that a package's __loader__.get_data() method used universal
Brett Cannon <bcannon@gmail.com>
parents: 43934
diff changeset
1925 ... 'test_doctest4.txt',
8636dcb801ce doctest assumed that a package's __loader__.get_data() method used universal
Brett Cannon <bcannon@gmail.com>
parents: 43934
diff changeset
1926 ... package='test')
8636dcb801ce doctest assumed that a package's __loader__.get_data() method used universal
Brett Cannon <bcannon@gmail.com>
parents: 43934
diff changeset
1927 ... suite.run(unittest.TestResult())
8636dcb801ce doctest assumed that a package's __loader__.get_data() method used universal
Brett Cannon <bcannon@gmail.com>
parents: 43934
diff changeset
1928 ... finally:
43940
3fb7ed5c23dd Add a missing check before deleting a package's __loader__.
Brett Cannon <bcannon@gmail.com>
parents: 43938
diff changeset
1929 ... if added_loader:
3fb7ed5c23dd Add a missing check before deleting a package's __loader__.
Brett Cannon <bcannon@gmail.com>
parents: 43938
diff changeset
1930 ... del test.__loader__
43938
8636dcb801ce doctest assumed that a package's __loader__.get_data() method used universal
Brett Cannon <bcannon@gmail.com>
parents: 43934
diff changeset
1931 <unittest.TestResult run=3 errors=0 failures=3>
8636dcb801ce doctest assumed that a package's __loader__.get_data() method used universal
Brett Cannon <bcannon@gmail.com>
parents: 43934
diff changeset
1932
33516
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1933 '/' should be used as a path separator. It will be converted
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1934 to a native separator at run time:
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1935
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1936 >>> suite = doctest.DocFileSuite('../test/test_doctest.txt')
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1937 >>> suite.run(unittest.TestResult())
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1938 <unittest.TestResult run=1 errors=0 failures=1>
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1939
33516
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1940 If DocFileSuite is used from an interactive session, then files
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1941 are resolved relative to the directory of sys.argv[0]:
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1942
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 43977
diff changeset
1943 >>> import types, os.path, test.test_doctest
33516
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1944 >>> save_argv = sys.argv
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1945 >>> sys.argv = [test.test_doctest.__file__]
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1946 >>> suite = doctest.DocFileSuite('test_doctest.txt',
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 43977
diff changeset
1947 ... package=types.ModuleType('__main__'))
33516
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1948 >>> sys.argv = save_argv
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1949
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1950 By setting `module_relative=False`, os-specific paths may be
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1951 used (including absolute paths and paths relative to the
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1952 working directory):
33516
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1953
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1954 >>> # Get the absolute path of the test package.
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1955 >>> test_doctest_path = os.path.abspath(test.test_doctest.__file__)
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1956 >>> test_pkg_path = os.path.split(test_doctest_path)[0]
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1957
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1958 >>> # Use it to find the absolute path of test_doctest.txt.
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1959 >>> test_file = os.path.join(test_pkg_path, 'test_doctest.txt')
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1960
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1961 >>> suite = doctest.DocFileSuite(test_file, module_relative=False)
33516
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1962 >>> suite.run(unittest.TestResult())
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1963 <unittest.TestResult run=1 errors=0 failures=1>
e9e65ef946ec In DocFileTest:
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33473
diff changeset
1964
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1965 It is an error to specify `package` when `module_relative=False`:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1966
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1967 >>> suite = doctest.DocFileSuite(test_file, module_relative=False,
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1968 ... package='test')
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1969 Traceback (most recent call last):
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1970 ValueError: Package may only be specified for module-relative paths.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
1971
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1972 You can specify initial global variables:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1973
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1974 >>> suite = doctest.DocFileSuite('test_doctest.txt',
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1975 ... 'test_doctest2.txt',
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
1976 ... 'test_doctest4.txt',
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1977 ... globs={'favorite_color': 'blue'})
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1978 >>> suite.run(unittest.TestResult())
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
1979 <unittest.TestResult run=3 errors=0 failures=2>
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1980
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1981 In this case, we supplied a missing favorite color. You can
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1982 provide doctest options:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1983
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1984 >>> suite = doctest.DocFileSuite('test_doctest.txt',
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1985 ... 'test_doctest2.txt',
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
1986 ... 'test_doctest4.txt',
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1987 ... optionflags=doctest.DONT_ACCEPT_BLANKLINE,
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1988 ... globs={'favorite_color': 'blue'})
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1989 >>> suite.run(unittest.TestResult())
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
1990 <unittest.TestResult run=3 errors=0 failures=3>
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1991
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1992 And, you can provide setUp and tearDown functions:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1993
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1994 >>> def setUp(t):
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1995 ... import test.test_doctest
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1996 ... test.test_doctest.sillySetup = True
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1997
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
1998 >>> def tearDown(t):
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
1999 ... import test.test_doctest
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2000 ... del test.test_doctest.sillySetup
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2001
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2002 Here, we installed a silly variable that the test expects:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2003
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2004 >>> suite = doctest.DocFileSuite('test_doctest.txt',
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2005 ... 'test_doctest2.txt',
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2006 ... 'test_doctest4.txt',
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2007 ... setUp=setUp, tearDown=tearDown)
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2008 >>> suite.run(unittest.TestResult())
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2009 <unittest.TestResult run=3 errors=0 failures=2>
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2010
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2011 But the tearDown restores sanity:
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2012
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2013 >>> import test.test_doctest
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2014 >>> test.test_doctest.sillySetup
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2015 Traceback (most recent call last):
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2016 ...
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2017 AttributeError: 'module' object has no attribute 'sillySetup'
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2018
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2019 The setUp and tearDown funtions are passed test objects.
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2020 Here, we'll use a setUp function to set the favorite color in
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2021 test_doctest.txt:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2022
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2023 >>> def setUp(test):
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2024 ... test.globs['favorite_color'] = 'blue'
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2025
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2026 >>> suite = doctest.DocFileSuite('test_doctest.txt', setUp=setUp)
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2027 >>> suite.run(unittest.TestResult())
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2028 <unittest.TestResult run=1 errors=0 failures=0>
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2029
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2030 Here, we didn't need to use a tearDown function because we
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2031 modified the test globals. The test globals are
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2032 automatically cleared for us after a test.
33315
96bb46691e86 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33312
diff changeset
2033
34215
b6619423c196 add __file__ to the globals available for tests loaded via DocFileSuite;
Fred Drake <fdrake@acm.org>
parents: 33944
diff changeset
2034 Tests in a file run using `DocFileSuite` can also access the
b6619423c196 add __file__ to the globals available for tests loaded via DocFileSuite;
Fred Drake <fdrake@acm.org>
parents: 33944
diff changeset
2035 `__file__` global, which is set to the name of the file
b6619423c196 add __file__ to the globals available for tests loaded via DocFileSuite;
Fred Drake <fdrake@acm.org>
parents: 33944
diff changeset
2036 containing the tests:
b6619423c196 add __file__ to the globals available for tests loaded via DocFileSuite;
Fred Drake <fdrake@acm.org>
parents: 33944
diff changeset
2037
b6619423c196 add __file__ to the globals available for tests loaded via DocFileSuite;
Fred Drake <fdrake@acm.org>
parents: 33944
diff changeset
2038 >>> suite = doctest.DocFileSuite('test_doctest3.txt')
b6619423c196 add __file__ to the globals available for tests loaded via DocFileSuite;
Fred Drake <fdrake@acm.org>
parents: 33944
diff changeset
2039 >>> suite.run(unittest.TestResult())
b6619423c196 add __file__ to the globals available for tests loaded via DocFileSuite;
Fred Drake <fdrake@acm.org>
parents: 33944
diff changeset
2040 <unittest.TestResult run=1 errors=0 failures=0>
b6619423c196 add __file__ to the globals available for tests loaded via DocFileSuite;
Fred Drake <fdrake@acm.org>
parents: 33944
diff changeset
2041
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2042 If the tests contain non-ASCII characters, we have to specify which
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2043 encoding the file is encoded with. We do so by using the `encoding`
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2044 parameter:
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2045
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2046 >>> suite = doctest.DocFileSuite('test_doctest.txt',
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2047 ... 'test_doctest2.txt',
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2048 ... 'test_doctest4.txt',
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2049 ... encoding='utf-8')
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2050 >>> suite.run(unittest.TestResult())
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2051 <unittest.TestResult run=3 errors=0 failures=2>
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2052
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2053 """
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2054
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
2055 def test_trailing_space_in_test():
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
2056 """
33243
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2057 Trailing spaces in expected output are significant:
33231
dea98eed8cf0 Added NDIFF_DIFF option.
Tim Peters <tim.peters@gmail.com>
parents: 33220
diff changeset
2058
33220
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
2059 >>> x, y = 'foo', ''
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
2060 >>> print x, y
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
2061 foo \n
3235e739bda1 Bugs fixed:
Jim Fulton <jim@zope.com>
parents: 33217
diff changeset
2062 """
32979
5cc55d36bb1d Merging from tim-doctest-branch, which is now closed.
Tim Peters <tim.peters@gmail.com>
parents: 32964
diff changeset
2063
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2064
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2065 def test_unittest_reportflags():
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2066 """Default unittest reporting flags can be set to control reporting
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2067
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2068 Here, we'll set the REPORT_ONLY_FIRST_FAILURE option so we see
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2069 only the first failure of each test. First, we'll look at the
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2070 output without the flag. The file test_doctest.txt file has two
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2071 tests. They both fail if blank lines are disabled:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2072
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2073 >>> suite = doctest.DocFileSuite('test_doctest.txt',
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2074 ... optionflags=doctest.DONT_ACCEPT_BLANKLINE)
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2075 >>> import unittest
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2076 >>> result = suite.run(unittest.TestResult())
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2077 >>> print result.failures[0][1] # doctest: +ELLIPSIS
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2078 Traceback ...
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2079 Failed example:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2080 favorite_color
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2081 ...
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2082 Failed example:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2083 if 1:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2084 ...
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2085
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2086 Note that we see both failures displayed.
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2087
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2088 >>> old = doctest.set_unittest_reportflags(
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2089 ... doctest.REPORT_ONLY_FIRST_FAILURE)
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2090
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2091 Now, when we run the test:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2092
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2093 >>> result = suite.run(unittest.TestResult())
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2094 >>> print result.failures[0][1] # doctest: +ELLIPSIS
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2095 Traceback ...
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2096 Failed example:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2097 favorite_color
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2098 Exception raised:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2099 ...
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2100 NameError: name 'favorite_color' is not defined
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2101 <BLANKLINE>
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2102 <BLANKLINE>
33315
96bb46691e86 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33312
diff changeset
2103
33312
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2104 We get only the first failure.
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2105
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2106 If we give any reporting options when we set up the tests,
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2107 however:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2108
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2109 >>> suite = doctest.DocFileSuite('test_doctest.txt',
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2110 ... optionflags=doctest.DONT_ACCEPT_BLANKLINE | doctest.REPORT_NDIFF)
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2111
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2112 Then the default eporting options are ignored:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2113
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2114 >>> result = suite.run(unittest.TestResult())
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2115 >>> print result.failures[0][1] # doctest: +ELLIPSIS
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2116 Traceback ...
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2117 Failed example:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2118 favorite_color
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2119 ...
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2120 Failed example:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2121 if 1:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2122 print 'a'
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2123 print
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2124 print 'b'
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2125 Differences (ndiff with -expected +actual):
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2126 a
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2127 - <BLANKLINE>
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2128 +
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2129 b
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2130 <BLANKLINE>
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2131 <BLANKLINE>
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2132
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2133
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2134 Test runners can restore the formatting flags after they run:
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2135
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2136 >>> ignored = doctest.set_unittest_reportflags(old)
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2137
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2138 """
bf0ef5fc49f1 - setUp and tearDown functions are now passed the test object
Jim Fulton <jim@zope.com>
parents: 33307
diff changeset
2139
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2140 def test_testfile(): r"""
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2141 Tests for the `testfile()` function. This function runs all the
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2142 doctest examples in a given file. In its simple invokation, it is
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2143 called with the name of a file, which is taken to be relative to the
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2144 calling module. The return value is (#failures, #tests).
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2145
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2146 >>> doctest.testfile('test_doctest.txt') # doctest: +ELLIPSIS
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2147 **********************************************************************
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2148 File "...", line 6, in test_doctest.txt
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2149 Failed example:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2150 favorite_color
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2151 Exception raised:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2152 ...
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2153 NameError: name 'favorite_color' is not defined
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2154 **********************************************************************
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2155 1 items had failures:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2156 1 of 2 in test_doctest.txt
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2157 ***Test Failed*** 1 failures.
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2158 TestResults(failed=1, attempted=2)
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2159 >>> doctest.master = None # Reset master.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2160
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2161 (Note: we'll be clearing doctest.master after each call to
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2162 `doctest.testfile`, to supress warnings about multiple tests with the
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2163 same name.)
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2164
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2165 Globals may be specified with the `globs` and `extraglobs` parameters:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2166
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2167 >>> globs = {'favorite_color': 'blue'}
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2168 >>> doctest.testfile('test_doctest.txt', globs=globs)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2169 TestResults(failed=0, attempted=2)
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2170 >>> doctest.master = None # Reset master.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2171
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2172 >>> extraglobs = {'favorite_color': 'red'}
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2173 >>> doctest.testfile('test_doctest.txt', globs=globs,
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2174 ... extraglobs=extraglobs) # doctest: +ELLIPSIS
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2175 **********************************************************************
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2176 File "...", line 6, in test_doctest.txt
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2177 Failed example:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2178 favorite_color
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2179 Expected:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2180 'blue'
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2181 Got:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2182 'red'
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2183 **********************************************************************
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2184 1 items had failures:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2185 1 of 2 in test_doctest.txt
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2186 ***Test Failed*** 1 failures.
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2187 TestResults(failed=1, attempted=2)
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2188 >>> doctest.master = None # Reset master.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2189
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2190 The file may be made relative to a given module or package, using the
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2191 optional `module_relative` parameter:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2192
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2193 >>> doctest.testfile('test_doctest.txt', globs=globs,
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2194 ... module_relative='test')
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2195 TestResults(failed=0, attempted=2)
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2196 >>> doctest.master = None # Reset master.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2197
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2198 Verbosity can be increased with the optional `verbose` paremter:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2199
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2200 >>> doctest.testfile('test_doctest.txt', globs=globs, verbose=True)
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2201 Trying:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2202 favorite_color
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2203 Expecting:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2204 'blue'
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2205 ok
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2206 Trying:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2207 if 1:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2208 print 'a'
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2209 print
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2210 print 'b'
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2211 Expecting:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2212 a
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2213 <BLANKLINE>
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2214 b
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2215 ok
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2216 1 items passed all tests:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2217 2 tests in test_doctest.txt
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2218 2 tests in 1 items.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2219 2 passed and 0 failed.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2220 Test passed.
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2221 TestResults(failed=0, attempted=2)
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2222 >>> doctest.master = None # Reset master.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2223
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2224 The name of the test may be specified with the optional `name`
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2225 parameter:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2226
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2227 >>> doctest.testfile('test_doctest.txt', name='newname')
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2228 ... # doctest: +ELLIPSIS
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2229 **********************************************************************
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2230 File "...", line 6, in newname
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2231 ...
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2232 TestResults(failed=1, attempted=2)
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2233 >>> doctest.master = None # Reset master.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2234
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2235 The summary report may be supressed with the optional `report`
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2236 parameter:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2237
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2238 >>> doctest.testfile('test_doctest.txt', report=False)
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2239 ... # doctest: +ELLIPSIS
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2240 **********************************************************************
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2241 File "...", line 6, in test_doctest.txt
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2242 Failed example:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2243 favorite_color
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2244 Exception raised:
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2245 ...
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2246 NameError: name 'favorite_color' is not defined
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2247 TestResults(failed=1, attempted=2)
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2248 >>> doctest.master = None # Reset master.
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2249
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2250 The optional keyword argument `raise_on_error` can be used to raise an
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2251 exception on the first error (which may be useful for postmortem
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2252 debugging):
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2253
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2254 >>> doctest.testfile('test_doctest.txt', raise_on_error=True)
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2255 ... # doctest: +ELLIPSIS
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2256 Traceback (most recent call last):
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2257 UnexpectedException: ...
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2258 >>> doctest.master = None # Reset master.
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2259
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2260 If the tests contain non-ASCII characters, the tests might fail, since
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2261 it's unknown which encoding is used. The encoding can be specified
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2262 using the optional keyword argument `encoding`:
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2263
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2264 >>> doctest.testfile('test_doctest4.txt') # doctest: +ELLIPSIS
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2265 **********************************************************************
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2266 File "...", line 7, in test_doctest4.txt
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2267 Failed example:
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2268 u'...'
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2269 Expected:
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2270 u'f\xf6\xf6'
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2271 Got:
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2272 u'f\xc3\xb6\xc3\xb6'
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2273 **********************************************************************
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2274 ...
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2275 **********************************************************************
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2276 1 items had failures:
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2277 2 of 4 in test_doctest4.txt
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2278 ***Test Failed*** 2 failures.
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2279 TestResults(failed=2, attempted=4)
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2280 >>> doctest.master = None # Reset master.
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2281
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2282 >>> doctest.testfile('test_doctest4.txt', encoding='utf-8')
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2283 TestResults(failed=0, attempted=4)
38459
7fa7cb6d8be8 Patch #1080727: add "encoding" parameter to doctest.DocFileSuite
George Yoshida <dynkin@gmail.com>
parents: 38136
diff changeset
2284 >>> doctest.master = None # Reset master.
33522
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2285 """
06f28238df31 - Added "testfile" function, a simple function for running & verifying
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33516
diff changeset
2286
33243
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2287 # old_test1, ... used to live in doctest.py, but cluttered it. Note
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2288 # that these use the deprecated doctest.Tester, so should go away (or
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2289 # be rewritten) someday.
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2290
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2291 # Ignore all warnings about the use of class Tester in this module.
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2292 # Note that the name of this module may differ depending on how it's
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2293 # imported, so the use of __name__ is important.
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2294 warnings.filterwarnings("ignore", "class Tester", DeprecationWarning,
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2295 __name__, 0)
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2296
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2297 def old_test1(): r"""
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2298 >>> from doctest import Tester
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2299 >>> t = Tester(globs={'x': 42}, verbose=0)
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2300 >>> t.runstring(r'''
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2301 ... >>> x = x * 2
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2302 ... >>> print x
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2303 ... 42
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2304 ... ''', 'XYZ')
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2305 **********************************************************************
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2306 Line 3, in XYZ
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2307 Failed example:
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2308 print x
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2309 Expected:
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2310 42
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2311 Got:
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2312 84
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2313 TestResults(failed=1, attempted=2)
33243
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2314 >>> t.runstring(">>> x = x * 2\n>>> print x\n84\n", 'example2')
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2315 TestResults(failed=0, attempted=2)
33243
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2316 >>> t.summarize()
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2317 **********************************************************************
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2318 1 items had failures:
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2319 1 of 2 in XYZ
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2320 ***Test Failed*** 1 failures.
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2321 TestResults(failed=1, attempted=4)
33243
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2322 >>> t.summarize(verbose=1)
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2323 1 items passed all tests:
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2324 2 tests in example2
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2325 **********************************************************************
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2326 1 items had failures:
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2327 1 of 2 in XYZ
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2328 4 tests in 2 items.
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2329 3 passed and 1 failed.
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2330 ***Test Failed*** 1 failures.
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2331 TestResults(failed=1, attempted=4)
33243
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2332 """
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2333
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2334 def old_test2(): r"""
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2335 >>> from doctest import Tester
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2336 >>> t = Tester(globs={}, verbose=1)
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2337 >>> test = r'''
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2338 ... # just an example
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2339 ... >>> x = 1 + 2
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2340 ... >>> x
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2341 ... 3
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2342 ... '''
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2343 >>> t.runstring(test, "Example")
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2344 Running string Example
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
2345 Trying:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
2346 x = 1 + 2
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
2347 Expecting nothing
33243
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2348 ok
33282
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
2349 Trying:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
2350 x
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
2351 Expecting:
eff766e29c55 - Changed the output of report_start() and report_unexpected_exception()
Edward Loper <edloper@gradient.cis.upenn.edu>
parents: 33277
diff changeset
2352 3
33243
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2353 ok
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2354 0 of 2 examples failed in string Example
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2355 TestResults(failed=0, attempted=2)
33243
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2356 """
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2357
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2358 def old_test3(): r"""
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2359 >>> from doctest import Tester
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2360 >>> t = Tester(globs={}, verbose=0)
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2361 >>> def _f():
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2362 ... '''Trivial docstring example.
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2363 ... >>> assert 2 == 2
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2364 ... '''
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2365 ... return 32
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2366 ...
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2367 >>> t.rundoc(_f) # expect 0 failures in 1 example
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2368 TestResults(failed=0, attempted=1)
33243
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2369 """
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2370
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2371 def old_test4(): """
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 43977
diff changeset
2372 >>> import types
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 43977
diff changeset
2373 >>> m1 = types.ModuleType('_m1')
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 43977
diff changeset
2374 >>> m2 = types.ModuleType('_m2')
33243
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2375 >>> test_data = \"""
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2376 ... def _f():
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2377 ... '''>>> assert 1 == 1
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2378 ... '''
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2379 ... def g():
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2380 ... '''>>> assert 2 != 1
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2381 ... '''
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2382 ... class H:
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2383 ... '''>>> assert 2 > 1
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2384 ... '''
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2385 ... def bar(self):
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2386 ... '''>>> assert 1 < 2
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2387 ... '''
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2388 ... \"""
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2389 >>> exec test_data in m1.__dict__
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2390 >>> exec test_data in m2.__dict__
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2391 >>> m1.__dict__.update({"f2": m2._f, "g2": m2.g, "h2": m2.H})
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2392
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2393 Tests that objects outside m1 are excluded:
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2394
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2395 >>> from doctest import Tester
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2396 >>> t = Tester(globs={}, verbose=0)
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2397 >>> t.rundict(m1.__dict__, "rundict_test", m1) # f2 and g2 and h2 skipped
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2398 TestResults(failed=0, attempted=4)
33243
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2399
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2400 Once more, not excluding stuff outside m1:
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2401
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2402 >>> t = Tester(globs={}, verbose=0)
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2403 >>> t.rundict(m1.__dict__, "rundict_test_pvt") # None are skipped.
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2404 TestResults(failed=0, attempted=8)
33243
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2405
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2406 The exclusion of objects from outside the designated module is
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2407 meant to be invoked automagically by testmod.
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2408
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2409 >>> doctest.testmod(m1, verbose=False)
44629
8058e16820ef Doctest results return a named tuple for readability
Raymond Hettinger <python@rcn.com>
parents: 44497
diff changeset
2410 TestResults(failed=0, attempted=4)
33243
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2411 """
cd2a95cfc7eb Moved some test cases from doctest to test_doctest.
Tim Peters <tim.peters@gmail.com>
parents: 33231
diff changeset
2412
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2413 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2414 ## Main
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2415 ######################################################################
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2416
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2417 def test_main():
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2418 # Check the doctest cases in doctest itself:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2419 test_support.run_doctest(doctest, verbosity=True)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2420 # Check the doctest cases defined here:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2421 from test import test_doctest
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2422 test_support.run_doctest(test_doctest, verbosity=True)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2423
45468
1d84bf5f9478 Patch #2167 from calvin: Remove unused imports
Christian Heimes <christian@cheimes.de>
parents: 44629
diff changeset
2424 import trace, sys
32963
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2425 def test_coverage(coverdir):
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2426 tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,],
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2427 trace=0, count=1)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2428 tracer.run('reload(doctest); test_main()')
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2429 r = tracer.results()
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2430 print 'Writing coverage results...'
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2431 r.write_results(show_missing=True, summary=True,
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2432 coverdir=coverdir)
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2433
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2434 if __name__ == '__main__':
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2435 if '-c' in sys.argv:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2436 test_coverage('/tmp/doctest.cover')
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2437 else:
9f66259aece3 Edward Loper's cool and massive refactoring of doctest.py, merged from
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
2438 test_main()