blob: 62ff5876a14aee06892622960609d31ac74bd365 [file] [log] [blame]
Tim Peters76882292001-02-17 05:58:44 +00001\section{\module{doctest} ---
2 Test docstrings represent reality}
3
4\declaremodule{standard}{doctest}
5\moduleauthor{Tim Peters}{tim_one@users.sourceforge.net}
6\sectionauthor{Tim Peters}{tim_one@users.sourceforge.net}
7\sectionauthor{Moshe Zadka}{moshez@debian.org}
8
9\modulesynopsis{A framework for verifying examples in docstrings.}
10
11The \module{doctest} module searches a module's docstrings for text that looks
12like an interactive Python session, then executes all such sessions to verify
13they still work exactly as shown. Here's a complete but small example:
14
15\begin{verbatim}
16"""
17This is module example.
18
19Example supplies one function, factorial. For example,
20
21>>> factorial(5)
22120
23"""
24
25def factorial(n):
26 """Return the factorial of n, an exact integer >= 0.
27
28 If the result is small enough to fit in an int, return an int.
29 Else return a long.
30
31 >>> [factorial(n) for n in range(6)]
32 [1, 1, 2, 6, 24, 120]
33 >>> [factorial(long(n)) for n in range(6)]
34 [1, 1, 2, 6, 24, 120]
35 >>> factorial(30)
36 265252859812191058636308480000000L
37 >>> factorial(30L)
38 265252859812191058636308480000000L
39 >>> factorial(-1)
40 Traceback (most recent call last):
41 ...
42 ValueError: n must be >= 0
43
44 Factorials of floats are OK, but the float must be an exact integer:
45 >>> factorial(30.1)
46 Traceback (most recent call last):
47 ...
48 ValueError: n must be exact integer
49 >>> factorial(30.0)
50 265252859812191058636308480000000L
51
52 It must also not be ridiculously large:
53 >>> factorial(1e100)
54 Traceback (most recent call last):
55 ...
56 OverflowError: n too large
57 """
58
59\end{verbatim}
60% allow LaTeX to break here.
61\begin{verbatim}
62
63 import math
64 if not n >= 0:
65 raise ValueError("n must be >= 0")
66 if math.floor(n) != n:
67 raise ValueError("n must be exact integer")
Raymond Hettinger92f21b12003-07-11 22:32:18 +000068 if n+1 == n: # catch a value like 1e300
Tim Peters76882292001-02-17 05:58:44 +000069 raise OverflowError("n too large")
70 result = 1
71 factor = 2
72 while factor <= n:
73 try:
74 result *= factor
75 except OverflowError:
76 result *= long(factor)
77 factor += 1
78 return result
79
80def _test():
81 import doctest, example
82 return doctest.testmod(example)
83
84if __name__ == "__main__":
85 _test()
86\end{verbatim}
87
Fred Drake7a6b4f02003-07-17 16:00:01 +000088If you run \file{example.py} directly from the command line,
89\module{doctest} works its magic:
Tim Peters76882292001-02-17 05:58:44 +000090
91\begin{verbatim}
92$ python example.py
93$
94\end{verbatim}
95
Fred Drake7a6b4f02003-07-17 16:00:01 +000096There's no output! That's normal, and it means all the examples
97worked. Pass \programopt{-v} to the script, and \module{doctest}
98prints a detailed log of what it's trying, and prints a summary at the
99end:
Tim Peters76882292001-02-17 05:58:44 +0000100
101\begin{verbatim}
102$ python example.py -v
103Running example.__doc__
104Trying: factorial(5)
105Expecting: 120
106ok
1070 of 1 examples failed in example.__doc__
108Running example.factorial.__doc__
109Trying: [factorial(n) for n in range(6)]
110Expecting: [1, 1, 2, 6, 24, 120]
111ok
112Trying: [factorial(long(n)) for n in range(6)]
113Expecting: [1, 1, 2, 6, 24, 120]
114ok
115Trying: factorial(30)
116Expecting: 265252859812191058636308480000000L
117ok
118\end{verbatim}
119
120And so on, eventually ending with:
121
122\begin{verbatim}
123Trying: factorial(1e100)
124Expecting:
125Traceback (most recent call last):
126 ...
127OverflowError: n too large
128ok
1290 of 8 examples failed in example.factorial.__doc__
1302 items passed all tests:
131 1 tests in example
132 8 tests in example.factorial
1339 tests in 2 items.
1349 passed and 0 failed.
135Test passed.
136$
137\end{verbatim}
138
Fred Drake7a6b4f02003-07-17 16:00:01 +0000139That's all you need to know to start making productive use of
140\module{doctest}! Jump in. The docstrings in \file{doctest.py} contain
141detailed information about all aspects of \module{doctest}, and we'll
142just cover the more important points here.
Tim Peters76882292001-02-17 05:58:44 +0000143
144\subsection{Normal Usage}
145
146In normal use, end each module \module{M} with:
147
148\begin{verbatim}
149def _test():
150 import doctest, M # replace M with your module's name
151 return doctest.testmod(M) # ditto
152
153if __name__ == "__main__":
154 _test()
155\end{verbatim}
156
Martin v. Löwis4581cfa2002-11-22 08:23:09 +0000157If you want to test the module as the main module, you don't need to
Fred Drake5d2f5152003-06-28 03:09:06 +0000158pass M to \function{testmod()}; in this case, it will test the current
Martin v. Löwis4581cfa2002-11-22 08:23:09 +0000159module.
160
Tim Peters76882292001-02-17 05:58:44 +0000161Then running the module as a script causes the examples in the docstrings
162to get executed and verified:
163
164\begin{verbatim}
165python M.py
166\end{verbatim}
167
168This won't display anything unless an example fails, in which case the
169failing example(s) and the cause(s) of the failure(s) are printed to stdout,
Fred Drake7eb14632001-02-17 17:32:41 +0000170and the final line of output is \code{'Test failed.'}.
Tim Peters76882292001-02-17 05:58:44 +0000171
Fred Drake7eb14632001-02-17 17:32:41 +0000172Run it with the \programopt{-v} switch instead:
Tim Peters76882292001-02-17 05:58:44 +0000173
174\begin{verbatim}
175python M.py -v
176\end{verbatim}
177
Fred Drake8836e562003-07-17 15:22:47 +0000178and a detailed report of all examples tried is printed to standard
179output, along with assorted summaries at the end.
Tim Peters76882292001-02-17 05:58:44 +0000180
Fred Drake5d2f5152003-06-28 03:09:06 +0000181You can force verbose mode by passing \code{verbose=1} to
182\function{testmod()}, or
Tim Peters76882292001-02-17 05:58:44 +0000183prohibit it by passing \code{verbose=0}. In either of those cases,
Fred Drake5d2f5152003-06-28 03:09:06 +0000184\code{sys.argv} is not examined by \function{testmod()}.
Tim Peters76882292001-02-17 05:58:44 +0000185
Fred Drake5d2f5152003-06-28 03:09:06 +0000186In any case, \function{testmod()} returns a 2-tuple of ints \code{(\var{f},
Fred Drake7eb14632001-02-17 17:32:41 +0000187\var{t})}, where \var{f} is the number of docstring examples that
188failed and \var{t} is the total number of docstring examples
189attempted.
Tim Peters76882292001-02-17 05:58:44 +0000190
191\subsection{Which Docstrings Are Examined?}
192
Fred Drake8836e562003-07-17 15:22:47 +0000193See the docstrings in \file{doctest.py} for all the details. They're
194unsurprising: the module docstring, and all function, class and method
195docstrings are searched. Optionally, the tester can be directed to
196exclude docstrings attached to objects with private names. Objects
197imported into the module are not searched.
Tim Peters76882292001-02-17 05:58:44 +0000198
Fred Drake7eb14632001-02-17 17:32:41 +0000199In addition, if \code{M.__test__} exists and "is true", it must be a
200dict, and each entry maps a (string) name to a function object, class
201object, or string. Function and class object docstrings found from
Raymond Hettinger71adf7e2003-07-16 19:25:22 +0000202\code{M.__test__} are searched even if the the tester has been
Raymond Hettinger943277e2003-07-17 14:47:12 +0000203directed to skip over private names in the rest of the module.
Raymond Hettinger71adf7e2003-07-16 19:25:22 +0000204In output, a key \code{K} in \code{M.__test__} appears with name
Tim Peters76882292001-02-17 05:58:44 +0000205
206\begin{verbatim}
Fred Drake8836e562003-07-17 15:22:47 +0000207<name of M>.__test__.K
Tim Peters76882292001-02-17 05:58:44 +0000208\end{verbatim}
209
210Any classes found are recursively searched similarly, to test docstrings in
211their contained methods and nested classes. While private names reached
Raymond Hettinger71adf7e2003-07-16 19:25:22 +0000212from \module{M}'s globals can be optionally skipped, all names reached from
Fred Drake7eb14632001-02-17 17:32:41 +0000213\code{M.__test__} are searched.
Tim Peters76882292001-02-17 05:58:44 +0000214
215\subsection{What's the Execution Context?}
216
Fred Drake5d2f5152003-06-28 03:09:06 +0000217By default, each time \function{testmod()} finds a docstring to test, it uses
218a \emph{copy} of \module{M}'s globals, so that running tests on a module
Tim Peters76882292001-02-17 05:58:44 +0000219doesn't change the module's real globals, and so that one test in
220\module{M} can't leave behind crumbs that accidentally allow another test
221to work. This means examples can freely use any names defined at top-level
Tim Peters0481d242001-10-02 21:01:22 +0000222in \module{M}, and names defined earlier in the docstring being run.
Tim Peters76882292001-02-17 05:58:44 +0000223
224You can force use of your own dict as the execution context by passing
225\code{globs=your_dict} to \function{testmod()} instead. Presumably this
Fred Drake7eb14632001-02-17 17:32:41 +0000226would be a copy of \code{M.__dict__} merged with the globals from other
Tim Peters76882292001-02-17 05:58:44 +0000227imported modules.
228
229\subsection{What About Exceptions?}
230
231No problem, as long as the only output generated by the example is the
232traceback itself. For example:
233
234\begin{verbatim}
Fred Drake19f3c522001-02-22 23:15:05 +0000235>>> [1, 2, 3].remove(42)
236Traceback (most recent call last):
237 File "<stdin>", line 1, in ?
238ValueError: list.remove(x): x not in list
239>>>
Tim Peters76882292001-02-17 05:58:44 +0000240\end{verbatim}
241
242Note that only the exception type and value are compared (specifically,
Fred Drake7eb14632001-02-17 17:32:41 +0000243only the last line in the traceback). The various ``File'' lines in
Tim Peters76882292001-02-17 05:58:44 +0000244between can be left out (unless they add significantly to the documentation
245value of the example).
246
247\subsection{Advanced Usage}
248
Raymond Hettinger92f21b12003-07-11 22:32:18 +0000249Several module level functions are available for controlling how doctests
250are run.
Tim Peters76882292001-02-17 05:58:44 +0000251
Raymond Hettinger92f21b12003-07-11 22:32:18 +0000252\begin{funcdesc}{debug}{module, name}
253 Debug a single docstring containing doctests.
254
255 Provide the \var{module} (or dotted name of the module) containing the
256 docstring to be debugged and the \var{name} (within the module) of the
257 object with the docstring to be debugged.
258
259 The doctest examples are extracted (see function \function{testsource()}),
260 and written to a temporary file. The Python debugger, \refmodule{pdb},
Fred Drake8836e562003-07-17 15:22:47 +0000261 is then invoked on that file.
Raymond Hettinger92f21b12003-07-11 22:32:18 +0000262 \versionadded{2.3}
263\end{funcdesc}
264
265\begin{funcdesc}{testmod}{}
266 This function provides the most basic interface to the doctests.
267 It creates a local instance of class \class{Tester}, runs appropriate
268 methods of that class, and merges the results into the global \class{Tester}
269 instance, \code{master}.
270
271 To get finer control than \function{testmod()} offers, create an instance
Fred Drake8836e562003-07-17 15:22:47 +0000272 of \class{Tester} with custom policies, or run methods of \code{master}
Raymond Hettinger92f21b12003-07-11 22:32:18 +0000273 directly. See \code{Tester.__doc__} for details.
274\end{funcdesc}
275
276\begin{funcdesc}{testsource}{module, name}
277 Extract the doctest examples from a docstring.
278
279 Provide the \var{module} (or dotted name of the module) containing the
280 tests to be extracted and the \var{name} (within the module) of the object
281 with the docstring containing the tests to be extracted.
282
283 The doctest examples are returned as a string containing Python
284 code. The expected output blocks in the examples are converted
285 to Python comments.
286 \versionadded{2.3}
287\end{funcdesc}
288
289\begin{funcdesc}{DocTestSuite}{\optional{module}}
Fred Drake7a6b4f02003-07-17 16:00:01 +0000290 Convert doctest tests for a module to a
291 \class{\refmodule{unittest}.TestSuite}.
Raymond Hettinger92f21b12003-07-11 22:32:18 +0000292
293 The returned \class{TestSuite} is to be run by the unittest framework
294 and runs each doctest in the module. If any of the doctests fail,
295 then the synthesized unit test fails, and a \exception{DocTestTestFailure}
296 exception is raised showing the name of the file containing the test and a
297 (sometimes approximate) line number.
298
299 The optional \var{module} argument provides the module to be tested. It
300 can be a module object or a (possibly dotted) module name. If not
Fred Drake8836e562003-07-17 15:22:47 +0000301 specified, the module calling this function is used.
Raymond Hettinger92f21b12003-07-11 22:32:18 +0000302
303 Example using one of the many ways that the \refmodule{unittest} module
304 can use a \class{TestSuite}:
305
306 \begin{verbatim}
307 import unittest
308 import doctest
309 import my_module_with_doctests
310
311 suite = doctest.DocTestSuite(my_module_with_doctests)
312 runner = unittest.TextTestRunner()
313 runner.run(suite)
314 \end{verbatim}
315
316 \versionadded{2.3}
Fred Drake8836e562003-07-17 15:22:47 +0000317 \warning{This function does not currently search \code{M.__test__}
Raymond Hettinger943277e2003-07-17 14:47:12 +0000318 and its search technique does not exactly match \function{testmod()} in
319 every detail. Future versions will bring the two into convergence.}
Raymond Hettinger92f21b12003-07-11 22:32:18 +0000320\end{funcdesc}
Tim Peters76882292001-02-17 05:58:44 +0000321
322
323\subsection{How are Docstring Examples Recognized?}
324
Fred Drake7a6b4f02003-07-17 16:00:01 +0000325In most cases a copy-and-paste of an interactive console session works
326fine---just make sure the leading whitespace is rigidly consistent
327(you can mix tabs and spaces if you're too lazy to do it right, but
328\module{doctest} is not in the business of guessing what you think a tab
329means).
Tim Peters76882292001-02-17 05:58:44 +0000330
331\begin{verbatim}
Fred Drake19f3c522001-02-22 23:15:05 +0000332>>> # comments are ignored
333>>> x = 12
334>>> x
33512
336>>> if x == 13:
337... print "yes"
338... else:
339... print "no"
340... print "NO"
341... print "NO!!!"
342...
343no
344NO
345NO!!!
346>>>
Tim Peters76882292001-02-17 05:58:44 +0000347\end{verbatim}
348
Fred Drake19f3c522001-02-22 23:15:05 +0000349Any expected output must immediately follow the final
350\code{'>\code{>}>~'} or \code{'...~'} line containing the code, and
351the expected output (if any) extends to the next \code{'>\code{>}>~'}
352or all-whitespace line.
Tim Peters76882292001-02-17 05:58:44 +0000353
354The fine print:
355
356\begin{itemize}
357
358\item Expected output cannot contain an all-whitespace line, since such a
359 line is taken to signal the end of expected output.
360
361\item Output to stdout is captured, but not output to stderr (exception
362 tracebacks are captured via a different means).
363
364\item If you continue a line via backslashing in an interactive session, or
365 for any other reason use a backslash, you need to double the backslash in
366 the docstring version. This is simply because you're in a string, and so
367 the backslash must be escaped for it to survive intact. Like:
368
369\begin{verbatim}
370>>> if "yes" == \\
371... "y" + \\
372... "es":
373... print 'yes'
374yes
375\end{verbatim}
376
Tim Petersf0768c82001-02-20 10:57:30 +0000377\item The starting column doesn't matter:
Tim Peters76882292001-02-17 05:58:44 +0000378
379\begin{verbatim}
Tim Petersc4089d82001-02-17 18:03:25 +0000380 >>> assert "Easy!"
381 >>> import math
382 >>> math.floor(1.9)
383 1.0
Tim Peters76882292001-02-17 05:58:44 +0000384\end{verbatim}
385
Fred Drake19f3c522001-02-22 23:15:05 +0000386and as many leading whitespace characters are stripped from the
387expected output as appeared in the initial \code{'>\code{>}>~'} line
388that triggered it.
Fred Drake7eb14632001-02-17 17:32:41 +0000389\end{itemize}
Tim Peters76882292001-02-17 05:58:44 +0000390
391\subsection{Warnings}
392
393\begin{enumerate}
394
Tim Peters76882292001-02-17 05:58:44 +0000395\item \module{doctest} is serious about requiring exact matches in expected
396 output. If even a single character doesn't match, the test fails. This
397 will probably surprise you a few times, as you learn exactly what Python
398 does and doesn't guarantee about output. For example, when printing a
399 dict, Python doesn't guarantee that the key-value pairs will be printed
400 in any particular order, so a test like
401
402% Hey! What happened to Monty Python examples?
Tim Petersf0768c82001-02-20 10:57:30 +0000403% Tim: ask Guido -- it's his example!
Tim Peters76882292001-02-17 05:58:44 +0000404\begin{verbatim}
Fred Drake19f3c522001-02-22 23:15:05 +0000405>>> foo()
406{"Hermione": "hippogryph", "Harry": "broomstick"}
407>>>
Tim Peters76882292001-02-17 05:58:44 +0000408\end{verbatim}
409
410is vulnerable! One workaround is to do
411
412\begin{verbatim}
Fred Drake19f3c522001-02-22 23:15:05 +0000413>>> foo() == {"Hermione": "hippogryph", "Harry": "broomstick"}
4141
415>>>
Tim Peters76882292001-02-17 05:58:44 +0000416\end{verbatim}
417
418instead. Another is to do
419
420\begin{verbatim}
Fred Drake19f3c522001-02-22 23:15:05 +0000421>>> d = foo().items()
422>>> d.sort()
423>>> d
424[('Harry', 'broomstick'), ('Hermione', 'hippogryph')]
Tim Peters76882292001-02-17 05:58:44 +0000425\end{verbatim}
426
427There are others, but you get the idea.
428
429Another bad idea is to print things that embed an object address, like
430
431\begin{verbatim}
Fred Drake19f3c522001-02-22 23:15:05 +0000432>>> id(1.0) # certain to fail some of the time
4337948648
434>>>
Tim Peters76882292001-02-17 05:58:44 +0000435\end{verbatim}
436
437Floating-point numbers are also subject to small output variations across
438platforms, because Python defers to the platform C library for float
439formatting, and C libraries vary widely in quality here.
440
441\begin{verbatim}
Fred Drake19f3c522001-02-22 23:15:05 +0000442>>> 1./7 # risky
4430.14285714285714285
444>>> print 1./7 # safer
4450.142857142857
446>>> print round(1./7, 6) # much safer
4470.142857
Tim Peters76882292001-02-17 05:58:44 +0000448\end{verbatim}
449
450Numbers of the form \code{I/2.**J} are safe across all platforms, and I
451often contrive doctest examples to produce numbers of that form:
452
453\begin{verbatim}
Fred Drake19f3c522001-02-22 23:15:05 +0000454>>> 3./4 # utterly safe
4550.75
Tim Peters76882292001-02-17 05:58:44 +0000456\end{verbatim}
457
458Simple fractions are also easier for people to understand, and that makes
459for better documentation.
460
Skip Montanaro1dc98c42001-06-08 14:40:28 +0000461\item Be careful if you have code that must only execute once.
462
463If you have module-level code that must only execute once, a more foolproof
Fred Drakec1158352001-06-11 14:55:01 +0000464definition of \function{_test()} is
Skip Montanaro1dc98c42001-06-08 14:40:28 +0000465
466\begin{verbatim}
467def _test():
468 import doctest, sys
Martin v. Löwis4581cfa2002-11-22 08:23:09 +0000469 doctest.testmod()
Skip Montanaro1dc98c42001-06-08 14:40:28 +0000470\end{verbatim}
Tim Peters6ebe61f2003-06-27 20:48:05 +0000471
472\item WYSIWYG isn't always the case, starting in Python 2.3. The
Fred Drake5d2f5152003-06-28 03:09:06 +0000473 string form of boolean results changed from \code{'0'} and
474 \code{'1'} to \code{'False'} and \code{'True'} in Python 2.3.
Tim Peters6ebe61f2003-06-27 20:48:05 +0000475 This makes it clumsy to write a doctest showing boolean results that
476 passes under multiple versions of Python. In Python 2.3, by default,
477 and as a special case, if an expected output block consists solely
Fred Drake5d2f5152003-06-28 03:09:06 +0000478 of \code{'0'} and the actual output block consists solely of
479 \code{'False'}, that's accepted as an exact match, and similarly for
480 \code{'1'} versus \code{'True'}. This behavior can be turned off by
Tim Peters6ebe61f2003-06-27 20:48:05 +0000481 passing the new (in 2.3) module constant
482 \constant{DONT_ACCEPT_TRUE_FOR_1} as the value of \function{testmod()}'s
483 new (in 2.3) optional \var{optionflags} argument. Some years after
484 the integer spellings of booleans are history, this hack will
485 probably be removed again.
486
Fred Drakec1158352001-06-11 14:55:01 +0000487\end{enumerate}
488
Tim Peters76882292001-02-17 05:58:44 +0000489
490\subsection{Soapbox}
491
Fred Drake7a6b4f02003-07-17 16:00:01 +0000492The first word in ``doctest'' is ``doc,'' and that's why the author
493wrote \refmodule{doctest}: to keep documentation up to date. It so
494happens that \refmodule{doctest} makes a pleasant unit testing
495environment, but that's not its primary purpose.
Tim Peters76882292001-02-17 05:58:44 +0000496
Fred Drake7a6b4f02003-07-17 16:00:01 +0000497Choose docstring examples with care. There's an art to this that
498needs to be learned---it may not be natural at first. Examples should
499add genuine value to the documentation. A good example can often be
500worth many words. If possible, show just a few normal cases, show
501endcases, show interesting subtle cases, and show an example of each
502kind of exception that can be raised. You're probably testing for
503endcases and subtle cases anyway in an interactive shell:
504\refmodule{doctest} wants to make it as easy as possible to capture
505those sessions, and will verify they continue to work as designed
506forever after.
Tim Peters76882292001-02-17 05:58:44 +0000507
Fred Drake7a6b4f02003-07-17 16:00:01 +0000508If done with care, the examples will be invaluable for your users, and
509will pay back the time it takes to collect them many times over as the
510years go by and things change. I'm still amazed at how often one of
511my \refmodule{doctest} examples stops working after a ``harmless''
512change.
Tim Peters76882292001-02-17 05:58:44 +0000513
514For exhaustive testing, or testing boring cases that add no value to the
Fred Drake7eb14632001-02-17 17:32:41 +0000515docs, define a \code{__test__} dict instead. That's what it's for.