Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2388a2

Browse files
committed
Start rewriting doctest's LaTeX docs. Damn, this is slow going!
1 parent 6a4e635 commit c2388a2

2 files changed

Lines changed: 148 additions & 29 deletions

File tree

Doc/lib/libdoctest.tex

Lines changed: 147 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ \section{\module{doctest} ---
7878
return result
7979
8080
def _test():
81-
import doctest, example
82-
return doctest.testmod(example)
81+
import doctest
82+
return doctest.testmod()
8383
8484
if __name__ == "__main__":
8585
_test()
@@ -100,33 +100,25 @@ \section{\module{doctest} ---
100100

101101
\begin{verbatim}
102102
$ python example.py -v
103-
Running example.__doc__
104103
Trying: factorial(5)
105104
Expecting: 120
106105
ok
107-
0 of 1 examples failed in example.__doc__
108-
Running example.factorial.__doc__
109106
Trying: [factorial(n) for n in range(6)]
110107
Expecting: [1, 1, 2, 6, 24, 120]
111108
ok
112109
Trying: [factorial(long(n)) for n in range(6)]
113110
Expecting: [1, 1, 2, 6, 24, 120]
114-
ok
115-
Trying: factorial(30)
116-
Expecting: 265252859812191058636308480000000L
117-
ok
118-
\end{verbatim}
111+
ok\end{verbatim}
119112

120113
And so on, eventually ending with:
121114

122115
\begin{verbatim}
123116
Trying: factorial(1e100)
124117
Expecting:
125-
Traceback (most recent call last):
126-
...
127-
OverflowError: n too large
118+
Traceback (most recent call last):
119+
...
120+
OverflowError: n too large
128121
ok
129-
0 of 8 examples failed in example.factorial.__doc__
130122
2 items passed all tests:
131123
1 tests in example
132124
8 tests in example.factorial
@@ -137,28 +129,27 @@ \section{\module{doctest} ---
137129
\end{verbatim}
138130

139131
That's all you need to know to start making productive use of
140-
\module{doctest}! Jump in. The docstrings in \file{doctest.py} contain
141-
detailed information about all aspects of \module{doctest}, and we'll
142-
just cover the more important points here.
132+
\module{doctest}! Jump in.
143133

144-
\subsection{Normal Usage}
134+
\subsection{Simple Usage}
145135

146-
In normal use, end each module \module{M} with:
136+
The simplest (not necessarily the best) way to start using doctest is to
137+
end each module \module{M} with:
147138

148139
\begin{verbatim}
149140
def _test():
150-
import doctest, M # replace M with your module's name
151-
return doctest.testmod(M) # ditto
141+
import doctest
142+
return doctest.testmod()
152143
153144
if __name__ == "__main__":
154145
_test()
155146
\end{verbatim}
156147

157-
If you want to test the current module as the main module, you don't need to
158-
pass M to \function{testmod()}; in this case, it will test the current
159-
module.
148+
\module{doctest} then examines docstrings in the module calling
149+
\function{testmod()}. If you want to test a different module, you can
150+
pass that module object to \function{testmod()}.
160151

161-
Then running the module as a script causes the examples in the docstrings
152+
Running the module as a script causes the examples in the docstrings
162153
to get executed and verified:
163154

164155
\begin{verbatim}
@@ -167,7 +158,9 @@ \subsection{Normal Usage}
167158

168159
This won't display anything unless an example fails, in which case the
169160
failing example(s) and the cause(s) of the failure(s) are printed to stdout,
170-
and the final line of output is \code{'Test failed.'}.
161+
and the final line of output is
162+
\\code{'***Test Failed*** \var{N} failures.'}, where \var{N} is the
163+
number of examples that failed.
171164

172165
Run it with the \programopt{-v} switch instead:
173166

@@ -178,16 +171,142 @@ \subsection{Normal Usage}
178171
and a detailed report of all examples tried is printed to standard
179172
output, along with assorted summaries at the end.
180173

181-
You can force verbose mode by passing \code{verbose=1} to
174+
You can force verbose mode by passing \code{verbose=True} to
182175
\function{testmod()}, or
183-
prohibit it by passing \code{verbose=0}. In either of those cases,
176+
prohibit it by passing \code{verbose=False}. In either of those cases,
184177
\code{sys.argv} is not examined by \function{testmod()}.
185178

186179
In any case, \function{testmod()} returns a 2-tuple of ints \code{(\var{f},
187180
\var{t})}, where \var{f} is the number of docstring examples that
188181
failed and \var{t} is the total number of docstring examples
189182
attempted.
190183

184+
\begin{funcdesc}{testmod}{\optional{m}\optional{, name}\optional{,
185+
globs}\optional{, verbose}\optional{,
186+
isprivate}\optional{, report}\optional{,
187+
optionflags}\optional{, extraglobs}\optional{,
188+
raise_on_error}}
189+
190+
All arguments are optional, and all except for \var{m} should be
191+
specified in keyword form.
192+
193+
Test examples in docstrings in functions and classes reachable
194+
from module \var{m} (or the current module if \var{m} is not supplied
195+
or is \code{None}), starting with \code{\var{m}.__doc__}.
196+
197+
Also test examples reachable from dict \code{\var{m}.__test__}, if it
198+
exists and is not \code{None}. \code{\var{m}.__test__} maps
199+
names (strings) to functions, classes and strings; function and class
200+
docstrings are searched for examples; strings are searched directly,
201+
as if they were docstrings.
202+
203+
Only docstrings attached to objects belonging to module \var{m} are
204+
searched.
205+
206+
Return \code{(#failures, #tests)}.
207+
208+
Optional argument \var{name} gives the name of the module; by default,
209+
or if \code{None}, \code{\var{m}.__name__} is used.
210+
211+
Optional argument \var{globs} gives a dict to be used as the globals
212+
when executing examples; by default, or if \code{None},
213+
\code{\var{m}.__dict__} is used. A new shallow copy of this dict is
214+
created for each docstring with examples, so that each docstring's
215+
examples start with a clean slate.
216+
217+
Optional argument \var{extraglobs} gives a dicti merged into the
218+
globals used to execute examples. This works like
219+
\method{dict.update()}: if \var{globs} and \var{extraglobs} have a
220+
common key, the associated value in \var{extraglobs} appears in the
221+
combined dict. By default, or if \code{None}, no extra globals are
222+
used. This is an advanced feature that allows parameterization of
223+
doctests. For example, a doctest can be written for a base class, using
224+
a generic name for the class, then reused to test any number of
225+
subclasses by passing an \var{extraglobs} dict mapping the generic
226+
name to the subclass to be tested.
227+
228+
Optional argument \var{verbose} prints lots of stuff if true, and prints
229+
only failures if false; by default, or if \code{None}, it's true
230+
if and only if \code{'-v'} is in \code{\module{sys}.argv}.
231+
232+
Optional argument \var{report} prints a summary at the end when true,
233+
else prints nothing at the end. In verbose mode, the summary is
234+
detailed, else the summary is very brief (in fact, empty if all tests
235+
passed).
236+
237+
Optional argument \var{optionflags} or's together module constants,
238+
and defaults to 0.
239+
240+
% Possible values:
241+
%
242+
% DONT_ACCEPT_TRUE_FOR_1
243+
% By default, if an expected output block contains just "1",
244+
% an actual output block containing just "True" is considered
245+
% to be a match, and similarly for "0" versus "False". When
246+
% DONT_ACCEPT_TRUE_FOR_1 is specified, neither substitution
247+
% is allowed.
248+
%
249+
% DONT_ACCEPT_BLANKLINE
250+
% By default, if an expected output block contains a line
251+
% containing only the string "<BLANKLINE>", then that line
252+
% will match a blank line in the actual output. When
253+
% DONT_ACCEPT_BLANKLINE is specified, this substitution is
254+
% not allowed.
255+
%
256+
% NORMALIZE_WHITESPACE
257+
% When NORMALIZE_WHITESPACE is specified, all sequences of
258+
% whitespace are treated as equal. I.e., any sequence of
259+
% whitespace within the expected output will match any
260+
% sequence of whitespace within the actual output.
261+
%
262+
% ELLIPSIS
263+
% When ELLIPSIS is specified, then an ellipsis marker
264+
% ("...") in the expected output can match any substring in
265+
% the actual output.
266+
%
267+
% UNIFIED_DIFF
268+
% When UNIFIED_DIFF is specified, failures that involve
269+
% multi-line expected and actual outputs will be displayed
270+
% using a unified diff.
271+
%
272+
% CONTEXT_DIFF
273+
% When CONTEXT_DIFF is specified, failures that involve
274+
% multi-line expected and actual outputs will be displayed
275+
% using a context diff.
276+
277+
Optional argument \var{raise_on_error} defaults to false. If true,
278+
an exception is raised upon the first failure or unexpected exception
279+
in an example. This allows failures to be post-mortem debugged.
280+
Default behavior is to continue running examples.
281+
282+
Optional argument \var{isprivate} specifies a function used to
283+
determine whether a name is private. The default function treats
284+
all names as public. \var{isprivate} can be set to
285+
\code{\module{doctest}.is_private} to skip over names that are
286+
private according to Python's underscore naming convention.
287+
\deprecated{2.4}{\var{isprivate} was a stupid idea -- don't use it.
288+
If you need to skip tests based on name, filter the list returned by
289+
\code{\class{DocTestFinder.find()} instead.}
290+
291+
% """ [XX] This is no longer true:
292+
% Advanced tomfoolery: testmod runs methods of a local instance of
293+
% class doctest.Tester, then merges the results into (or creates)
294+
% global Tester instance doctest.master. Methods of doctest.master
295+
% can be called directly too, if you want to do something unusual.
296+
% Passing report=0 to testmod is especially useful then, to delay
297+
% displaying a summary. Invoke doctest.master.summarize(verbose)
298+
% when you're done fiddling.
299+
300+
\versionchanged[The parameter \var{optionflags} was added]{2.3}
301+
302+
\versionchanged[Many new module constants for use with \var{optionflags}
303+
were added]{2.4}
304+
305+
\versionchanged[The parameters \var{extraglobs} and \var{raise_on_error}
306+
were added]{2.4}
307+
\end{funcdesc}
308+
309+
191310
\subsection{Which Docstrings Are Examined?}
192311

193312
See the docstrings in \file{doctest.py} for all the details. They're

Lib/doctest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ def testmod(m=None, name=None, globs=None, verbose=None, isprivate=None,
16101610
are not skipped.
16111611
16121612
Also test examples reachable from dict m.__test__ if it exists and is
1613-
not None. m.__dict__ maps names to functions, classes and strings;
1613+
not None. m.__test__ maps names to functions, classes and strings;
16141614
function and class docstrings are tested even if the name is private;
16151615
strings are tested directly, as if they were docstrings.
16161616

0 commit comments

Comments
 (0)