@@ -108,7 +108,8 @@ \section{\module{doctest} ---
108108ok
109109Trying: [factorial(long(n)) for n in range(6)]
110110Expecting: [1, 1, 2, 6, 24, 120]
111- ok \end {verbatim }
111+ ok
112+ \end {verbatim }
112113
113114And so on, eventually ending with:
114115
@@ -129,12 +130,14 @@ \section{\module{doctest} ---
129130\end {verbatim }
130131
131132That's all you need to know to start making productive use of
132- \module {doctest}! Jump in.
133+ \module {doctest}! Jump in. The following sections provide full
134+ details. Note that there are many examples of doctests in
135+ the standard Python test suite and libraries.
133136
134137\subsection {Simple Usage }
135138
136- The simplest (not necessarily the best) way to start using doctest is to
137- end each module \module {M} with:
139+ The simplest way to start using doctest (but not necessarily the way
140+ you'll continue to do it) is to end each module \module {M} with:
138141
139142\begin {verbatim }
140143def _test():
@@ -146,8 +149,7 @@ \subsection{Simple Usage}
146149\end {verbatim }
147150
148151\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()}.
152+ \function {testmod()}.
151153
152154Running the module as a script causes the examples in the docstrings
153155to get executed and verified:
@@ -292,35 +294,95 @@ \subsection{Which Docstrings Are Examined?}
292294
293295\subsection {What's the Execution Context? }
294296
295- By default, each time \function {testmod()} finds a docstring to test, it uses
296- a \emph {copy } of \module {M}'s globals, so that running tests on a module
297+ By default, each time \function {testmod()} finds a docstring to test, it
298+ uses a \emph {shallow copy } of \module {M}'s globals, so that running tests
297299doesn't change the module's real globals, and so that one test in
298300\module {M} can't leave behind crumbs that accidentally allow another test
299301to work. This means examples can freely use any names defined at top-level
300302in \module {M}, and names defined earlier in the docstring being run.
303+ Examples cannot see names defined in other docstrings.
301304
302305You can force use of your own dict as the execution context by passing
303- \code {globs=your_dict} to \function {testmod()} instead. Presumably this
304- would be a copy of \code {M.__dict__} merged with the globals from other
305- imported modules.
306+ \code {globs=your_dict} to \function {testmod()} instead.
306307
307308\subsection {What About Exceptions? }
308309
309- No problem, as long as the only output generated by the example is the
310- traceback itself. For example:
310+ No problem: just paste in the expected traceback. Since
311+ tracebacks contain details that are likely to change
312+ rapidly (for example, exact file paths and line numbers), this is one
313+ case where doctest works hard to be flexible in what it accepts.
314+ This makes the full story involved, but you really don't have
315+ to remember much. Simple example:
311316
312317\begin {verbatim }
313318>>> [1, 2, 3].remove(42)
314319Traceback (most recent call last):
315320 File "<stdin>", line 1, in ?
316321ValueError: list.remove(x): x not in list
317- >>>
318322\end {verbatim }
319323
320- Note that only the exception type and value are compared (specifically,
321- only the last line in the traceback). The various `` File'' lines in
322- between can be left out (unless they add significantly to the documentation
323- value of the example).
324+ That doctest succeeds if, and only if, \exception {ValueError} is raised,
325+ with the \samp {list.remove(x): x not in list} detail as shown.
326+
327+ The expected output for an exception is divided into four parts.
328+ First, an example may produce some normal output before an exception
329+ is raised, although that's unusual. The "normal output" is taken to
330+ be everything until the first "Traceback" line, and is usually an
331+ empty string. Next, the traceback line must be one of these two, and
332+ indented the same as the first line in the example:
333+
334+ \begin {verbatim }
335+ Traceback (most recent call last):
336+ Traceback (innermost last):
337+ \end {verbatim }
338+
339+ The most interesting part is the last part: the line(s) starting with the
340+ exception type and detail. This is usually the last line of a traceback,
341+ but can extend across any number of lines. After the "Traceback" line,
342+ doctest simply ignores everything until the first line indented the same as
343+ the first line of the example, \emph {and } starting with an alphanumeric
344+ character. This example illustrates the complexities that are possible:
345+
346+ \begin {verbatim }
347+ >>> print 1, 2; raise ValueError('printed 1\nand 2\n but not 3')
348+ 1 2
349+ Traceback (most recent call last):
350+ ... indented the same, but doesn't start with an alphanumeric
351+ not indented the same, so ignored too
352+ File "/Python23/lib/doctest.py", line 442, in _run_examples_inner
353+ compileflags, 1) in globs
354+ File "<string>", line 1, in ? # and all these are ignored
355+ ValueError: printed 1
356+ and 2
357+ but not 3
358+ \end {verbatim }
359+
360+ The first (\samp {1 2}) and last three (starting with
361+ \exception {ValueError}) lines are compared, and the rest are ignored.
362+
363+ Best practice is to omit the `` File'' lines, unless they add
364+ significant documentation value to the example. So the example above
365+ is probably better as:
366+
367+ \begin {verbatim }
368+ >>> print 1, 2; raise ValueError('printed 1\nand 2\n but not 3')
369+ 1 2
370+ Traceback (most recent call last):
371+ ...
372+ ValueError: printed 1
373+ and 2
374+ but not 3
375+ \end {verbatim }
376+
377+ Note the tracebacks are treated very specially. In particular, in the
378+ rewritten example, the use of \samp {...} is independent of doctest's
379+ \constant {ELLIPSIS} option. The ellipsis in that example could
380+ be left out, or could just as well be three (or three hundred) commas.
381+
382+ \versionchanged [The abilities to check both normal output and an
383+ exception in a single example, and to have a multi-line
384+ exception detail, were added]{2.4}
385+
324386
325387\subsection {Option Flags and Directive Names\label {doctest-options } }
326388
0 commit comments