| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | \section{\module{doctest} --- | 
					
						
							|  |  |  |          Test docstrings represent reality} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \declaremodule{standard}{doctest} | 
					
						
							|  |  |  | \moduleauthor{Tim Peters}{tim_one@users.sourceforge.net} | 
					
						
							|  |  |  | \sectionauthor{Tim Peters}{tim_one@users.sourceforge.net} | 
					
						
							|  |  |  | \sectionauthor{Moshe Zadka}{moshez@debian.org} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \modulesynopsis{A framework for verifying examples in docstrings.} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The \module{doctest} module searches a module's docstrings for text that looks | 
					
						
							|  |  |  | like an interactive Python session, then executes all such sessions to verify | 
					
						
							|  |  |  | they still work exactly as shown.  Here's a complete but small example: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | """ | 
					
						
							|  |  |  | This is module example. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Example supplies one function, factorial.  For example, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | >>> factorial(5) | 
					
						
							|  |  |  | 120 | 
					
						
							|  |  |  | """ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def factorial(n): | 
					
						
							|  |  |  |     """Return the factorial of n, an exact integer >= 0. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If the result is small enough to fit in an int, return an int. | 
					
						
							|  |  |  |     Else return a long. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> [factorial(n) for n in range(6)] | 
					
						
							|  |  |  |     [1, 1, 2, 6, 24, 120] | 
					
						
							|  |  |  |     >>> [factorial(long(n)) for n in range(6)] | 
					
						
							|  |  |  |     [1, 1, 2, 6, 24, 120] | 
					
						
							|  |  |  |     >>> factorial(30) | 
					
						
							|  |  |  |     265252859812191058636308480000000L | 
					
						
							|  |  |  |     >>> factorial(30L) | 
					
						
							|  |  |  |     265252859812191058636308480000000L | 
					
						
							|  |  |  |     >>> factorial(-1) | 
					
						
							|  |  |  |     Traceback (most recent call last): | 
					
						
							|  |  |  |         ... | 
					
						
							|  |  |  |     ValueError: n must be >= 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Factorials of floats are OK, but the float must be an exact integer: | 
					
						
							|  |  |  |     >>> factorial(30.1) | 
					
						
							|  |  |  |     Traceback (most recent call last): | 
					
						
							|  |  |  |         ... | 
					
						
							|  |  |  |     ValueError: n must be exact integer | 
					
						
							|  |  |  |     >>> factorial(30.0) | 
					
						
							|  |  |  |     265252859812191058636308480000000L | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     It must also not be ridiculously large: | 
					
						
							|  |  |  |     >>> factorial(1e100) | 
					
						
							|  |  |  |     Traceback (most recent call last): | 
					
						
							|  |  |  |         ... | 
					
						
							|  |  |  |     OverflowError: n too large | 
					
						
							|  |  |  |     """ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							|  |  |  | % allow LaTeX to break here.
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     import math | 
					
						
							|  |  |  |     if not n >= 0: | 
					
						
							|  |  |  |         raise ValueError("n must be >= 0") | 
					
						
							|  |  |  |     if math.floor(n) != n: | 
					
						
							|  |  |  |         raise ValueError("n must be exact integer") | 
					
						
							| 
									
										
										
										
											2003-07-11 22:32:18 +00:00
										 |  |  |     if n+1 == n:  # catch a value like 1e300 | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  |         raise OverflowError("n too large") | 
					
						
							|  |  |  |     result = 1 | 
					
						
							|  |  |  |     factor = 2 | 
					
						
							|  |  |  |     while factor <= n: | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             result *= factor | 
					
						
							|  |  |  |         except OverflowError: | 
					
						
							|  |  |  |             result *= long(factor) | 
					
						
							|  |  |  |         factor += 1 | 
					
						
							|  |  |  |     return result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _test(): | 
					
						
							| 
									
										
										
										
											2004-08-10 01:41:28 +00:00
										 |  |  |     import doctest | 
					
						
							|  |  |  |     return doctest.testmod() | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     _test() | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-07-17 16:00:01 +00:00
										 |  |  | If you run \file{example.py} directly from the command line, | 
					
						
							|  |  |  | \module{doctest} works its magic: | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | $ python example.py
 | 
					
						
							|  |  |  | $
 | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-07-17 16:00:01 +00:00
										 |  |  | There's no output!  That's normal, and it means all the examples | 
					
						
							|  |  |  | worked.  Pass \programopt{-v} to the script, and \module{doctest} | 
					
						
							|  |  |  | prints a detailed log of what it's trying, and prints a summary at the | 
					
						
							|  |  |  | end: | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | $ python example.py -v
 | 
					
						
							|  |  |  | Trying: factorial(5) | 
					
						
							|  |  |  | Expecting: 120 | 
					
						
							|  |  |  | ok | 
					
						
							|  |  |  | Trying: [factorial(n) for n in range(6)] | 
					
						
							|  |  |  | Expecting: [1, 1, 2, 6, 24, 120] | 
					
						
							|  |  |  | ok | 
					
						
							|  |  |  | Trying: [factorial(long(n)) for n in range(6)] | 
					
						
							|  |  |  | Expecting: [1, 1, 2, 6, 24, 120] | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | ok | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | And so on, eventually ending with: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | Trying: factorial(1e100) | 
					
						
							|  |  |  | Expecting: | 
					
						
							| 
									
										
										
										
											2004-08-10 01:41:28 +00:00
										 |  |  |     Traceback (most recent call last): | 
					
						
							|  |  |  |         ... | 
					
						
							|  |  |  |     OverflowError: n too large | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | ok | 
					
						
							|  |  |  | 2 items passed all tests: | 
					
						
							|  |  |  |    1 tests in example | 
					
						
							|  |  |  |    8 tests in example.factorial | 
					
						
							|  |  |  | 9 tests in 2 items. | 
					
						
							|  |  |  | 9 passed and 0 failed. | 
					
						
							|  |  |  | Test passed. | 
					
						
							|  |  |  | $
 | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-07-17 16:00:01 +00:00
										 |  |  | That's all you need to know to start making productive use of | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | \module{doctest}!  Jump in.  The following sections provide full | 
					
						
							|  |  |  | details.  Note that there are many examples of doctests in | 
					
						
							|  |  |  | the standard Python test suite and libraries. | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-10 01:41:28 +00:00
										 |  |  | \subsection{Simple Usage} | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | The simplest way to start using doctest (but not necessarily the way | 
					
						
							|  |  |  | you'll continue to do it) is to end each module \module{M} with: | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | def _test(): | 
					
						
							| 
									
										
										
										
											2004-08-10 01:41:28 +00:00
										 |  |  |     import doctest | 
					
						
							|  |  |  |     return doctest.testmod() | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     _test() | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-10 01:41:28 +00:00
										 |  |  | \module{doctest} then examines docstrings in the module calling | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | \function{testmod()}. | 
					
						
							| 
									
										
										
										
											2002-11-22 08:23:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-10 01:41:28 +00:00
										 |  |  | Running the module as a script causes the examples in the docstrings | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | to get executed and verified: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | python M.py | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This won't display anything unless an example fails, in which case the | 
					
						
							|  |  |  | failing example(s) and the cause(s) of the failure(s) are printed to stdout, | 
					
						
							| 
									
										
										
										
											2004-08-10 01:41:28 +00:00
										 |  |  | and the final line of output is | 
					
						
							| 
									
										
										
										
											2004-08-13 01:49:12 +00:00
										 |  |  | \samp{'***Test Failed*** \var{N} failures.'}, where \var{N} is the | 
					
						
							| 
									
										
										
										
											2004-08-10 01:41:28 +00:00
										 |  |  | number of examples that failed. | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-17 17:32:41 +00:00
										 |  |  | Run it with the \programopt{-v} switch instead: | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | python M.py -v | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-07-17 15:22:47 +00:00
										 |  |  | and a detailed report of all examples tried is printed to standard | 
					
						
							|  |  |  | output, along with assorted summaries at the end. | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-10 01:41:28 +00:00
										 |  |  | You can force verbose mode by passing \code{verbose=True} to | 
					
						
							| 
									
										
										
										
											2003-06-28 03:09:06 +00:00
										 |  |  | \function{testmod()}, or | 
					
						
							| 
									
										
										
										
											2004-08-10 01:41:28 +00:00
										 |  |  | prohibit it by passing \code{verbose=False}.  In either of those cases, | 
					
						
							| 
									
										
										
										
											2003-06-28 03:09:06 +00:00
										 |  |  | \code{sys.argv} is not examined by \function{testmod()}. | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-28 03:09:06 +00:00
										 |  |  | In any case, \function{testmod()} returns a 2-tuple of ints \code{(\var{f}, | 
					
						
							| 
									
										
										
										
											2001-02-17 17:32:41 +00:00
										 |  |  | \var{t})}, where \var{f} is the number of docstring examples that | 
					
						
							|  |  |  | failed and \var{t} is the total number of docstring examples | 
					
						
							|  |  |  | attempted. | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \subsection{Which Docstrings Are Examined?} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-12 22:31:25 +00:00
										 |  |  | The module docstring, and all function, class and method docstrings are | 
					
						
							|  |  |  | searched.  Objects imported into the module are not searched. | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-17 17:32:41 +00:00
										 |  |  | In addition, if \code{M.__test__} exists and "is true", it must be a | 
					
						
							|  |  |  | dict, and each entry maps a (string) name to a function object, class | 
					
						
							|  |  |  | object, or string.  Function and class object docstrings found from | 
					
						
							| 
									
										
										
										
											2004-08-12 22:31:25 +00:00
										 |  |  | \code{M.__test__} are searched, and strings are treated as if they | 
					
						
							|  |  |  | were docstrings.  In output, a key \code{K} in \code{M.__test__} appears | 
					
						
							|  |  |  | with name | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											2003-07-17 15:22:47 +00:00
										 |  |  | <name of M>.__test__.K | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Any classes found are recursively searched similarly, to test docstrings in | 
					
						
							| 
									
										
										
										
											2004-08-12 22:31:25 +00:00
										 |  |  | their contained methods and nested classes. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \versionchanged[A "private name" concept is deprecated and no longer | 
					
						
							| 
									
										
										
										
											2004-08-13 01:49:12 +00:00
										 |  |  |                 documented]{2.4} | 
					
						
							| 
									
										
										
										
											2004-08-12 22:31:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \subsection{What's the Execution Context?} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | By default, each time \function{testmod()} finds a docstring to test, it | 
					
						
							|  |  |  | uses a \emph{shallow copy} of \module{M}'s globals, so that running tests | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | doesn't change the module's real globals, and so that one test in | 
					
						
							|  |  |  | \module{M} can't leave behind crumbs that accidentally allow another test | 
					
						
							|  |  |  | to work.  This means examples can freely use any names defined at top-level | 
					
						
							| 
									
										
										
										
											2001-10-02 21:01:22 +00:00
										 |  |  | in \module{M}, and names defined earlier in the docstring being run. | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | Examples cannot see names defined in other docstrings. | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | You can force use of your own dict as the execution context by passing | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | \code{globs=your_dict} to \function{testmod()} instead. | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \subsection{What About Exceptions?} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | No problem:  just paste in the expected traceback.  Since | 
					
						
							|  |  |  | tracebacks contain details that are likely to change | 
					
						
							|  |  |  | rapidly (for example, exact file paths and line numbers), this is one | 
					
						
							|  |  |  | case where doctest works hard to be flexible in what it accepts. | 
					
						
							|  |  |  | This makes the full story involved, but you really don't have | 
					
						
							|  |  |  | to remember much.  Simple example: | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											2001-02-22 23:15:05 +00:00
										 |  |  | >>> [1, 2, 3].remove(42) | 
					
						
							|  |  |  | Traceback (most recent call last): | 
					
						
							|  |  |  |   File "<stdin>", line 1, in ? | 
					
						
							|  |  |  | ValueError: list.remove(x): x not in list | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-25 23:07:03 +00:00
										 |  |  | That doctest succeeds if \exception{ValueError} is raised, with the | 
					
						
							|  |  |  | \samp{list.remove(x): x not in list} detail as shown.\footnote{The | 
					
						
							|  |  |  |   doctest also succeeds if it prints the exact text of the traceback | 
					
						
							|  |  |  |   message; otherwise, it fails.} | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-25 23:07:03 +00:00
										 |  |  | The expected output for an exception must start with a traceback | 
					
						
							|  |  |  | header, which may be either of the following two lines, indented the | 
					
						
							|  |  |  | same as the first line of the example: | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | Traceback (most recent call last): | 
					
						
							|  |  |  | Traceback (innermost last): | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-25 23:07:03 +00:00
										 |  |  | The traceback header is followed by an optional traceback stack, whose | 
					
						
							|  |  |  | contents are ignored by doctest.  Each line of the traceback stack | 
					
						
							|  |  |  | must be indented further than the first line of the example, \emph{or} | 
					
						
							|  |  |  | start with a non-alphanumeric character.  Typically, the traceback | 
					
						
							|  |  |  | stack is either omitted or copied verbatim from an interactive | 
					
						
							|  |  |  | session. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The traceback stack is followed by the most interesting part: the | 
					
						
							|  |  |  | line(s) containing the exception type and detail.  This is usually the | 
					
						
							|  |  |  | last line of a traceback, but can extend across multiple lines if the | 
					
						
							|  |  |  | exception has a multi-line detail, as illustrated in the following | 
					
						
							|  |  |  | example: | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											2004-08-25 23:07:03 +00:00
										 |  |  | >>> raise ValueError('multi\n   line\ndetail') | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | Traceback (most recent call last): | 
					
						
							| 
									
										
										
										
											2004-08-25 23:07:03 +00:00
										 |  |  |   File "<stdin>", line 1, in ? | 
					
						
							|  |  |  | ValueError: multi | 
					
						
							|  |  |  |     line | 
					
						
							|  |  |  | detail | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-25 23:07:03 +00:00
										 |  |  | The last three (starting with \exception{ValueError}) lines are | 
					
						
							|  |  |  | compared against the exception's type and detail, and the rest are | 
					
						
							|  |  |  | ignored. | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-25 23:07:03 +00:00
										 |  |  | Best practice is to omit the traceback stack, unless it adds | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | significant documentation value to the example.  So the example above | 
					
						
							|  |  |  | is probably better as: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											2004-08-25 23:07:03 +00:00
										 |  |  | >>> raise ValueError('multi\n   line\ndetail') | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | Traceback (most recent call last): | 
					
						
							| 
									
										
										
										
											2004-08-25 23:07:03 +00:00
										 |  |  |     ... | 
					
						
							|  |  |  | ValueError: multi | 
					
						
							|  |  |  |     line | 
					
						
							|  |  |  | detail | 
					
						
							| 
									
										
										
										
											2004-08-13 03:55:05 +00:00
										 |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Note the tracebacks are treated very specially.  In particular, in the | 
					
						
							|  |  |  | rewritten example, the use of \samp{...} is independent of doctest's | 
					
						
							|  |  |  | \constant{ELLIPSIS} option.  The ellipsis in that example could | 
					
						
							|  |  |  | be left out, or could just as well be three (or three hundred) commas. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-26 01:02:08 +00:00
										 |  |  | \versionchanged[The ability to handle a multi-line exception detail | 
					
						
							|  |  |  |                 was added]{2.4} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-19 16:38:58 +00:00
										 |  |  | \subsection{Option Flags and Directives\label{doctest-options}} | 
					
						
							| 
									
										
										
										
											2004-08-12 22:31:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-13 21:55:21 +00:00
										 |  |  | A number of option flags control various aspects of doctest's comparison | 
					
						
							| 
									
										
										
										
											2004-08-19 16:38:58 +00:00
										 |  |  | behavior.  Symbolic names for the flags are supplied as module constants, | 
					
						
							| 
									
										
										
										
											2004-08-13 21:55:21 +00:00
										 |  |  | which can be or'ed together and passed to various functions.  The names | 
					
						
							| 
									
										
										
										
											2004-08-19 16:38:58 +00:00
										 |  |  | can also be used in doctest directives (see below). | 
					
						
							| 
									
										
										
										
											2004-08-12 22:31:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{datadesc}{DONT_ACCEPT_TRUE_FOR_1} | 
					
						
							|  |  |  |     By default, if an expected output block contains just \code{1}, | 
					
						
							|  |  |  |     an actual output block containing just \code{1} or just | 
					
						
							|  |  |  |     \code{True} is considered to be a match, and similarly for \code{0} | 
					
						
							|  |  |  |     versus \code{False}.  When \constant{DONT_ACCEPT_TRUE_FOR_1} is | 
					
						
							|  |  |  |     specified, neither substitution is allowed.  The default behavior | 
					
						
							|  |  |  |     caters to that Python changed the return type of many functions | 
					
						
							|  |  |  |     from integer to boolean; doctests expecting "little integer" | 
					
						
							|  |  |  |     output still work in these cases.  This option will probably go | 
					
						
							|  |  |  |     away, but not for several years. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{datadesc}{DONT_ACCEPT_BLANKLINE} | 
					
						
							|  |  |  |     By default, if an expected output block contains a line | 
					
						
							|  |  |  |     containing only the string \code{<BLANKLINE>}, then that line | 
					
						
							|  |  |  |     will match a blank line in the actual output.  Because a | 
					
						
							|  |  |  |     genuinely blank line delimits the expected output, this is | 
					
						
							|  |  |  |     the only way to communicate that a blank line is expected.  When | 
					
						
							|  |  |  |     \constant{DONT_ACCEPT_BLANKLINE} is specified, this substitution | 
					
						
							|  |  |  |     is not allowed. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{datadesc}{NORMALIZE_WHITESPACE} | 
					
						
							|  |  |  |     When specified, all sequences of whitespace (blanks and newlines) are | 
					
						
							|  |  |  |     treated as equal.  Any sequence of whitespace within the expected | 
					
						
							|  |  |  |     output will match any sequence of whitespace within the actual output. | 
					
						
							|  |  |  |     By default, whitespace must match exactly. | 
					
						
							|  |  |  |     \constant{NORMALIZE_WHITESPACE} is especially useful when a line | 
					
						
							|  |  |  |     of expected output is very long, and you want to wrap it across | 
					
						
							|  |  |  |     multiple lines in your source. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{datadesc}{ELLIPSIS} | 
					
						
							|  |  |  |     When specified, an ellipsis marker (\code{...}) in the expected output | 
					
						
							|  |  |  |     can match any substring in the actual output.  This includes | 
					
						
							| 
									
										
										
										
											2004-08-19 16:38:58 +00:00
										 |  |  |     substrings that span line boundaries, and empty substrings, so it's | 
					
						
							|  |  |  |     best to keep usage of this simple.  Complicated uses can lead to the | 
					
						
							|  |  |  |     same kinds of "oops, it matched too much!" surprises that \regexp{.*} | 
					
						
							|  |  |  |     is prone to in regular expressions. | 
					
						
							| 
									
										
										
										
											2004-08-12 22:31:25 +00:00
										 |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-26 01:41:51 +00:00
										 |  |  | \begin{datadesc}{REPORT_UDIFF} | 
					
						
							| 
									
										
										
										
											2004-08-12 22:31:25 +00:00
										 |  |  |     When specified, failures that involve multi-line expected and | 
					
						
							|  |  |  |     actual outputs are displayed using a unified diff. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-26 01:41:51 +00:00
										 |  |  | \begin{datadesc}{REPORT_CDIFF} | 
					
						
							| 
									
										
										
										
											2004-08-12 22:31:25 +00:00
										 |  |  |     When specified, failures that involve multi-line expected and | 
					
						
							|  |  |  |     actual outputs will be displayed using a context diff. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-26 01:41:51 +00:00
										 |  |  | \begin{datadesc}{REPORT_NDIFF} | 
					
						
							| 
									
										
										
										
											2004-08-22 19:43:28 +00:00
										 |  |  |     When specified, differences are computed by \code{difflib.Differ}, | 
					
						
							|  |  |  |     using the same algorithm as the popular \file{ndiff.py} utility. | 
					
						
							|  |  |  |     This is the only method that marks differences within lines as | 
					
						
							|  |  |  |     well as across lines.  For example, if a line of expected output | 
					
						
							|  |  |  |     contains digit \code{1} where actual output contains letter \code{l}, | 
					
						
							|  |  |  |     a line is inserted with a caret marking the mismatching column | 
					
						
							|  |  |  |     positions. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							| 
									
										
										
										
											2004-08-12 22:31:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-26 02:45:51 +00:00
										 |  |  | \begin{datadesc}{REPORT_ONLY_FIRST_FAILURE} | 
					
						
							|  |  |  |   When specified, display the first failing example in each doctest, | 
					
						
							|  |  |  |   but suppress output for all remaining examples.  This will prevent | 
					
						
							|  |  |  |   doctest from reporting correct examples that break because of | 
					
						
							|  |  |  |   earlier failures; but it might also hide incorrect examples that | 
					
						
							|  |  |  |   fail independently of the first failure.  When | 
					
						
							|  |  |  |   \constant{REPORT_ONLY_FIRST_FAILURE} is specified, the remaining | 
					
						
							|  |  |  |   examples are still run, and still count towards the total number of | 
					
						
							|  |  |  |   failures reported; only the output is suppressed. | 
					
						
							|  |  |  | \end{datadesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-19 16:38:58 +00:00
										 |  |  | A "doctest directive" is a trailing Python comment on a line of a doctest | 
					
						
							|  |  |  | example: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{productionlist}[doctest] | 
					
						
							|  |  |  |     \production{directive} | 
					
						
							| 
									
										
										
										
											2004-08-20 14:37:05 +00:00
										 |  |  |                {"\#" "doctest:" \token{on_or_off} \token{directive_name}} | 
					
						
							| 
									
										
										
										
											2004-08-19 16:38:58 +00:00
										 |  |  |     \production{on_or_off} | 
					
						
							|  |  |  |                {"+" | "-"} | 
					
						
							|  |  |  |     \production{directive_name} | 
					
						
							|  |  |  |                {"DONT_ACCEPT_BLANKLINE" | "NORMALIZE_WHITESPACE" | ...} | 
					
						
							|  |  |  | \end{productionlist} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Whitespace is not allowed between the \code{+} or \code{-} and the | 
					
						
							|  |  |  | directive name.  The directive name can be any of the option names | 
					
						
							|  |  |  | explained above. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The doctest directives appearing in a single example modify doctest's | 
					
						
							|  |  |  | behavior for that single example.  Use \code{+} to enable the named | 
					
						
							|  |  |  | behavior, or \code{-} to disable it. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | For example, this test passes: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | >>> print range(20) #doctest: +NORMALIZE_WHITESPACE | 
					
						
							|  |  |  | [0,   1,  2,  3,  4,  5,  6,  7,  8,  9, | 
					
						
							|  |  |  | 10,  11, 12, 13, 14, 15, 16, 17, 18, 19] | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Without the directive it would fail, both because the actual output | 
					
						
							|  |  |  | doesn't have two blanks before the single-digit list elements, and | 
					
						
							|  |  |  | because the actual output is on a single line.  This test also passes, | 
					
						
							|  |  |  | and requires a directive to do so: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | >>> print range(20) # doctest:+ELLIPSIS | 
					
						
							|  |  |  | [0, 1, ..., 18, 19] | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Only one directive per physical line is accepted.  If you want to | 
					
						
							|  |  |  | use multiple directives for a single example, you can add | 
					
						
							|  |  |  | \samp{...} lines to your example containing only directives: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | >>> print range(20) #doctest: +ELLIPSIS | 
					
						
							|  |  |  | ...                 #doctest: +NORMALIZE_WHITESPACE | 
					
						
							|  |  |  | [0,    1, ...,   18,    19] | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Note that since all options are disabled by default, and directives apply | 
					
						
							|  |  |  | only to the example they appear in, enabling options (via \code{+} in a | 
					
						
							|  |  |  | directive) is usually the only meaningful choice.  However, option flags | 
					
						
							|  |  |  | can also be passed to functions that run doctests, establishing different | 
					
						
							|  |  |  | defaults.  In such cases, disabling an option via \code{-} in a directive | 
					
						
							|  |  |  | can be useful. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-12 22:31:25 +00:00
										 |  |  | \versionchanged[Constants \constant{DONT_ACCEPT_BLANKLINE}, | 
					
						
							|  |  |  |     \constant{NORMALIZE_WHITESPACE}, \constant{ELLIPSIS}, | 
					
						
							| 
									
										
										
										
											2004-08-26 02:45:51 +00:00
										 |  |  |     \constant{REPORT_UDIFF}, \constant{REPORT_CDIFF}, | 
					
						
							|  |  |  |     \constant{REPORT_NDIFF}, and \constant{REPORT_ONLY_FIRST_FAILURE} | 
					
						
							| 
									
										
										
										
											2004-08-19 16:38:58 +00:00
										 |  |  |     were added; by default \code{<BLANKLINE>} in expected output | 
					
						
							|  |  |  |     matches an empty line in actual output; and doctest directives | 
					
						
							|  |  |  |     were added]{2.4} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-12 22:31:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | \subsection{Advanced Usage} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-07-11 22:32:18 +00:00
										 |  |  | Several module level functions are available for controlling how doctests | 
					
						
							|  |  |  | are run. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{debug}{module, name} | 
					
						
							|  |  |  |   Debug a single docstring containing doctests. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Provide the \var{module} (or dotted name of the module) containing the | 
					
						
							|  |  |  |   docstring to be debugged and the \var{name} (within the module) of the | 
					
						
							|  |  |  |   object with the docstring to be debugged. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   The doctest examples are extracted (see function \function{testsource()}), | 
					
						
							|  |  |  |   and written to a temporary file.  The Python debugger, \refmodule{pdb}, | 
					
						
							| 
									
										
										
										
											2003-07-17 15:22:47 +00:00
										 |  |  |   is then invoked on that file. | 
					
						
							| 
									
										
										
										
											2003-07-11 22:32:18 +00:00
										 |  |  |   \versionadded{2.3} | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-13 21:55:21 +00:00
										 |  |  | \begin{funcdesc}{testmod}{\optional{m}\optional{, name}\optional{, | 
					
						
							|  |  |  |                           globs}\optional{, verbose}\optional{, | 
					
						
							|  |  |  |                           isprivate}\optional{, report}\optional{, | 
					
						
							|  |  |  |                           optionflags}\optional{, extraglobs}\optional{, | 
					
						
							|  |  |  |                           raise_on_error}} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   All arguments are optional, and all except for \var{m} should be | 
					
						
							|  |  |  |   specified in keyword form. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Test examples in docstrings in functions and classes reachable | 
					
						
							|  |  |  |   from module \var{m} (or the current module if \var{m} is not supplied | 
					
						
							|  |  |  |   or is \code{None}), starting with \code{\var{m}.__doc__}. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Also test examples reachable from dict \code{\var{m}.__test__}, if it | 
					
						
							|  |  |  |   exists and is not \code{None}.  \code{\var{m}.__test__} maps | 
					
						
							|  |  |  |   names (strings) to functions, classes and strings; function and class | 
					
						
							|  |  |  |   docstrings are searched for examples; strings are searched directly, | 
					
						
							|  |  |  |   as if they were docstrings. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Only docstrings attached to objects belonging to module \var{m} are | 
					
						
							|  |  |  |   searched. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Return \samp{(\var{failure_count}, \var{test_count})}. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Optional argument \var{name} gives the name of the module; by default, | 
					
						
							|  |  |  |   or if \code{None}, \code{\var{m}.__name__} is used. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Optional argument \var{globs} gives a dict to be used as the globals | 
					
						
							|  |  |  |   when executing examples; by default, or if \code{None}, | 
					
						
							|  |  |  |   \code{\var{m}.__dict__} is used.  A new shallow copy of this dict is | 
					
						
							|  |  |  |   created for each docstring with examples, so that each docstring's | 
					
						
							|  |  |  |   examples start with a clean slate. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Optional argument \var{extraglobs} gives a dict merged into the | 
					
						
							|  |  |  |   globals used to execute examples.  This works like | 
					
						
							|  |  |  |   \method{dict.update()}:  if \var{globs} and \var{extraglobs} have a | 
					
						
							|  |  |  |   common key, the associated value in \var{extraglobs} appears in the | 
					
						
							|  |  |  |   combined dict.  By default, or if \code{None}, no extra globals are | 
					
						
							|  |  |  |   used.  This is an advanced feature that allows parameterization of | 
					
						
							|  |  |  |   doctests.  For example, a doctest can be written for a base class, using | 
					
						
							|  |  |  |   a generic name for the class, then reused to test any number of | 
					
						
							|  |  |  |   subclasses by passing an \var{extraglobs} dict mapping the generic | 
					
						
							|  |  |  |   name to the subclass to be tested. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Optional argument \var{verbose} prints lots of stuff if true, and prints | 
					
						
							|  |  |  |   only failures if false; by default, or if \code{None}, it's true | 
					
						
							|  |  |  |   if and only if \code{'-v'} is in \code{sys.argv}. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Optional argument \var{report} prints a summary at the end when true, | 
					
						
							|  |  |  |   else prints nothing at the end.  In verbose mode, the summary is | 
					
						
							|  |  |  |   detailed, else the summary is very brief (in fact, empty if all tests | 
					
						
							|  |  |  |   passed). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Optional argument \var{optionflags} or's together option flags.  See | 
					
						
							|  |  |  |   see section \ref{doctest-options}. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Optional argument \var{raise_on_error} defaults to false.  If true, | 
					
						
							|  |  |  |   an exception is raised upon the first failure or unexpected exception | 
					
						
							|  |  |  |   in an example.  This allows failures to be post-mortem debugged. | 
					
						
							|  |  |  |   Default behavior is to continue running examples. | 
					
						
							| 
									
										
										
										
											2003-07-11 22:32:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-13 21:55:21 +00:00
										 |  |  |   Optional argument \var{isprivate} specifies a function used to | 
					
						
							|  |  |  |   determine whether a name is private.  The default function treats | 
					
						
							|  |  |  |   all names as public.  \var{isprivate} can be set to | 
					
						
							|  |  |  |   \code{doctest.is_private} to skip over names that are | 
					
						
							|  |  |  |   private according to Python's underscore naming convention. | 
					
						
							|  |  |  |   \deprecated{2.4}{\var{isprivate} was a stupid idea -- don't use it. | 
					
						
							|  |  |  |   If you need to skip tests based on name, filter the list returned by | 
					
						
							|  |  |  |   \code{DocTestFinder.find()} instead.} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   \versionchanged[The parameter \var{optionflags} was added]{2.3} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   \versionchanged[The parameters \var{extraglobs} and \var{raise_on_error} | 
					
						
							|  |  |  |                   were added]{2.4} | 
					
						
							| 
									
										
										
										
											2003-07-11 22:32:18 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{testsource}{module, name} | 
					
						
							|  |  |  |   Extract the doctest examples from a docstring. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Provide the \var{module} (or dotted name of the module) containing the | 
					
						
							|  |  |  |   tests to be extracted and the \var{name} (within the module) of the object | 
					
						
							|  |  |  |   with the docstring containing the tests to be extracted. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   The doctest examples are returned as a string containing Python | 
					
						
							|  |  |  |   code.  The expected output blocks in the examples are converted | 
					
						
							|  |  |  |   to Python comments. | 
					
						
							|  |  |  |   \versionadded{2.3} | 
					
						
							|  |  |  | \end{funcdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{funcdesc}{DocTestSuite}{\optional{module}} | 
					
						
							| 
									
										
										
										
											2003-07-17 16:00:01 +00:00
										 |  |  |   Convert doctest tests for a module to a | 
					
						
							|  |  |  |   \class{\refmodule{unittest}.TestSuite}. | 
					
						
							| 
									
										
										
										
											2003-07-11 22:32:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   The returned \class{TestSuite} is to be run by the unittest framework | 
					
						
							|  |  |  |   and runs each doctest in the module.  If any of the doctests fail, | 
					
						
							|  |  |  |   then the synthesized unit test fails, and a \exception{DocTestTestFailure} | 
					
						
							|  |  |  |   exception is raised showing the name of the file containing the test and a | 
					
						
							|  |  |  |   (sometimes approximate) line number. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   The optional \var{module} argument provides the module to be tested.  It | 
					
						
							|  |  |  |   can be a module object or a (possibly dotted) module name.  If not | 
					
						
							| 
									
										
										
										
											2003-07-17 15:22:47 +00:00
										 |  |  |   specified, the module calling this function is used. | 
					
						
							| 
									
										
										
										
											2003-07-11 22:32:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Example using one of the many ways that the \refmodule{unittest} module | 
					
						
							|  |  |  |   can use a \class{TestSuite}: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   \begin{verbatim} | 
					
						
							|  |  |  |     import unittest | 
					
						
							|  |  |  |     import doctest | 
					
						
							|  |  |  |     import my_module_with_doctests | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     suite = doctest.DocTestSuite(my_module_with_doctests) | 
					
						
							|  |  |  |     runner = unittest.TextTestRunner() | 
					
						
							|  |  |  |     runner.run(suite) | 
					
						
							|  |  |  |   \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   \versionadded{2.3} | 
					
						
							| 
									
										
										
										
											2003-07-17 15:22:47 +00:00
										 |  |  |   \warning{This function does not currently search \code{M.__test__} | 
					
						
							| 
									
										
										
										
											2003-07-17 14:47:12 +00:00
										 |  |  |   and its search technique does not exactly match \function{testmod()} in | 
					
						
							|  |  |  |   every detail.  Future versions will bring the two into convergence.} | 
					
						
							| 
									
										
										
										
											2003-07-11 22:32:18 +00:00
										 |  |  | \end{funcdesc} | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \subsection{How are Docstring Examples Recognized?} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-07-17 16:00:01 +00:00
										 |  |  | In most cases a copy-and-paste of an interactive console session works | 
					
						
							| 
									
										
										
										
											2004-08-13 21:55:21 +00:00
										 |  |  | fine, but doctest isn't trying to do an exact emulation of any specific | 
					
						
							|  |  |  | Python shell.  All hard tab characters are expanded to spaces, using | 
					
						
							|  |  |  | 8-column tab stops.  If you don't believe tabs should mean that, too | 
					
						
							|  |  |  | bad:  don't use hard tabs, or write your own \class{DocTestParser} | 
					
						
							|  |  |  | class. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \versionchanged[Expanding tabs to spaces is new; previous versions | 
					
						
							|  |  |  |                 tried to preserve hard tabs, with confusing results]{2.4} | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											2001-02-22 23:15:05 +00:00
										 |  |  | >>> # comments are ignored | 
					
						
							|  |  |  | >>> x = 12 | 
					
						
							|  |  |  | >>> x | 
					
						
							|  |  |  | 12 | 
					
						
							|  |  |  | >>> if x == 13: | 
					
						
							|  |  |  | ...     print "yes" | 
					
						
							|  |  |  | ... else: | 
					
						
							|  |  |  | ...     print "no" | 
					
						
							|  |  |  | ...     print "NO" | 
					
						
							|  |  |  | ...     print "NO!!!" | 
					
						
							|  |  |  | ... | 
					
						
							|  |  |  | no | 
					
						
							|  |  |  | NO | 
					
						
							|  |  |  | NO!!! | 
					
						
							|  |  |  | >>> | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-22 23:15:05 +00:00
										 |  |  | Any expected output must immediately follow the final | 
					
						
							|  |  |  | \code{'>\code{>}>~'} or \code{'...~'} line containing the code, and | 
					
						
							|  |  |  | the expected output (if any) extends to the next \code{'>\code{>}>~'} | 
					
						
							|  |  |  | or all-whitespace line. | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | The fine print: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{itemize} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \item Expected output cannot contain an all-whitespace line, since such a | 
					
						
							| 
									
										
										
										
											2004-08-13 21:55:21 +00:00
										 |  |  |   line is taken to signal the end of expected output.  If expected | 
					
						
							|  |  |  |   output does contain a blank line, put \code{<BLANKLINE>} in your | 
					
						
							|  |  |  |   doctest example each place a blank line is expected. | 
					
						
							|  |  |  |   \versionchanged[\code{<BLANKLINE>} was added; there was no way to | 
					
						
							|  |  |  |                   use expected output containing empty lines in | 
					
						
							|  |  |  |                   previous versions]{2.4} | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \item Output to stdout is captured, but not output to stderr (exception | 
					
						
							|  |  |  |   tracebacks are captured via a different means). | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 19:01:00 +00:00
										 |  |  | \item If you continue a line via backslashing in an interactive session, | 
					
						
							|  |  |  |   or for any other reason use a backslash, you should use a raw | 
					
						
							|  |  |  |   docstring, which will preserve your backslahses exactly as you type | 
					
						
							|  |  |  |   them: | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											2004-07-23 02:48:24 +00:00
										 |  |  | >>> def f(x): | 
					
						
							| 
									
										
										
										
											2004-05-31 19:01:00 +00:00
										 |  |  | ...     r'''Backslashes in a raw docstring: m\n''' | 
					
						
							|  |  |  | >>> print f.__doc__ | 
					
						
							|  |  |  | Backslashes in a raw docstring: m\n | 
					
						
							|  |  |  | \end{verbatim} | 
					
						
							| 
									
										
										
										
											2004-07-23 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 19:01:00 +00:00
										 |  |  |   Otherwise, the backslash will be interpreted as part of the string. | 
					
						
							| 
									
										
										
										
											2004-08-25 23:07:03 +00:00
										 |  |  |   E.g., the "{\textbackslash}" above would be interpreted as a newline | 
					
						
							| 
									
										
										
										
											2004-05-31 19:01:00 +00:00
										 |  |  |   character.  Alternatively, you can double each backslash in the | 
					
						
							|  |  |  |   doctest version (and not use a raw string): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											2004-07-23 02:48:24 +00:00
										 |  |  | >>> def f(x): | 
					
						
							| 
									
										
										
										
											2004-05-31 19:01:00 +00:00
										 |  |  | ...     '''Backslashes in a raw docstring: m\\n''' | 
					
						
							|  |  |  | >>> print f.__doc__ | 
					
						
							|  |  |  | Backslashes in a raw docstring: m\n | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-20 10:57:30 +00:00
										 |  |  | \item The starting column doesn't matter: | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											2001-02-17 18:03:25 +00:00
										 |  |  |   >>> assert "Easy!" | 
					
						
							|  |  |  |         >>> import math | 
					
						
							|  |  |  |             >>> math.floor(1.9) | 
					
						
							|  |  |  |             1.0 | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-22 23:15:05 +00:00
										 |  |  | and as many leading whitespace characters are stripped from the | 
					
						
							|  |  |  | expected output as appeared in the initial \code{'>\code{>}>~'} line | 
					
						
							| 
									
										
										
										
											2004-08-13 21:55:21 +00:00
										 |  |  | that started the example. | 
					
						
							| 
									
										
										
										
											2001-02-17 17:32:41 +00:00
										 |  |  | \end{itemize} | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \subsection{Warnings} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{enumerate} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \item \module{doctest} is serious about requiring exact matches in expected | 
					
						
							|  |  |  |   output.  If even a single character doesn't match, the test fails.  This | 
					
						
							|  |  |  |   will probably surprise you a few times, as you learn exactly what Python | 
					
						
							|  |  |  |   does and doesn't guarantee about output.  For example, when printing a | 
					
						
							|  |  |  |   dict, Python doesn't guarantee that the key-value pairs will be printed | 
					
						
							|  |  |  |   in any particular order, so a test like | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | % Hey! What happened to Monty Python examples?
 | 
					
						
							| 
									
										
										
										
											2001-02-20 10:57:30 +00:00
										 |  |  | % Tim: ask Guido -- it's his example!
 | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											2001-02-22 23:15:05 +00:00
										 |  |  | >>> foo() | 
					
						
							|  |  |  | {"Hermione": "hippogryph", "Harry": "broomstick"} | 
					
						
							|  |  |  | >>> | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | is vulnerable!  One workaround is to do | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											2001-02-22 23:15:05 +00:00
										 |  |  | >>> foo() == {"Hermione": "hippogryph", "Harry": "broomstick"} | 
					
						
							| 
									
										
										
										
											2003-11-27 19:48:03 +00:00
										 |  |  | True | 
					
						
							| 
									
										
										
										
											2001-02-22 23:15:05 +00:00
										 |  |  | >>> | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | instead.  Another is to do | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											2001-02-22 23:15:05 +00:00
										 |  |  | >>> d = foo().items() | 
					
						
							|  |  |  | >>> d.sort() | 
					
						
							|  |  |  | >>> d | 
					
						
							|  |  |  | [('Harry', 'broomstick'), ('Hermione', 'hippogryph')] | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | There are others, but you get the idea. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Another bad idea is to print things that embed an object address, like | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											2001-02-22 23:15:05 +00:00
										 |  |  | >>> id(1.0) # certain to fail some of the time | 
					
						
							|  |  |  | 7948648 | 
					
						
							|  |  |  | >>> | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Floating-point numbers are also subject to small output variations across | 
					
						
							|  |  |  | platforms, because Python defers to the platform C library for float | 
					
						
							|  |  |  | formatting, and C libraries vary widely in quality here. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											2001-02-22 23:15:05 +00:00
										 |  |  | >>> 1./7  # risky | 
					
						
							|  |  |  | 0.14285714285714285 | 
					
						
							|  |  |  | >>> print 1./7 # safer | 
					
						
							|  |  |  | 0.142857142857 | 
					
						
							|  |  |  | >>> print round(1./7, 6) # much safer | 
					
						
							|  |  |  | 0.142857 | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Numbers of the form \code{I/2.**J} are safe across all platforms, and I | 
					
						
							|  |  |  | often contrive doctest examples to produce numbers of that form: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											2001-02-22 23:15:05 +00:00
										 |  |  | >>> 3./4  # utterly safe | 
					
						
							|  |  |  | 0.75 | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Simple fractions are also easier for people to understand, and that makes | 
					
						
							|  |  |  | for better documentation. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-06-08 14:40:28 +00:00
										 |  |  | \item Be careful if you have code that must only execute once. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If you have module-level code that must only execute once, a more foolproof | 
					
						
							| 
									
										
										
										
											2001-06-11 14:55:01 +00:00
										 |  |  | definition of \function{_test()} is | 
					
						
							| 
									
										
										
										
											2001-06-08 14:40:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							|  |  |  | def _test(): | 
					
						
							|  |  |  |     import doctest, sys | 
					
						
							| 
									
										
										
										
											2002-11-22 08:23:09 +00:00
										 |  |  |     doctest.testmod() | 
					
						
							| 
									
										
										
										
											2001-06-08 14:40:28 +00:00
										 |  |  | \end{verbatim} | 
					
						
							| 
									
										
										
											
												A hack to ease compatibility with pre-2.3 Pythons:  by default, doctest
now accepts "True" when a test expects "1", and similarly for "False"
versus "0".  This is un-doctest-like, but on balance makes it much
more pleasant to write doctests that pass under 2.2 and 2.3.  I expect
it to go away again, when 2.2 is forgotten.  In the meantime, there's
a new doctest module constant that can be passed to a new optional
argument, if you want to turn this behavior off.
Note that this substitution is very simple-minded:  the expected and
actual outputs have to consist of single tokens.  No attempt is made,
e.g., to accept [True, False] when a test expects [1, 0].  This is a
simple hack for simple tests, and I intend to keep it that way.
											
										 
											2003-06-27 20:48:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \item WYSIWYG isn't always the case, starting in Python 2.3.  The | 
					
						
							| 
									
										
										
										
											2003-06-28 03:09:06 +00:00
										 |  |  |   string form of boolean results changed from \code{'0'} and | 
					
						
							|  |  |  |   \code{'1'} to \code{'False'} and \code{'True'} in Python 2.3. | 
					
						
							| 
									
										
										
											
												A hack to ease compatibility with pre-2.3 Pythons:  by default, doctest
now accepts "True" when a test expects "1", and similarly for "False"
versus "0".  This is un-doctest-like, but on balance makes it much
more pleasant to write doctests that pass under 2.2 and 2.3.  I expect
it to go away again, when 2.2 is forgotten.  In the meantime, there's
a new doctest module constant that can be passed to a new optional
argument, if you want to turn this behavior off.
Note that this substitution is very simple-minded:  the expected and
actual outputs have to consist of single tokens.  No attempt is made,
e.g., to accept [True, False] when a test expects [1, 0].  This is a
simple hack for simple tests, and I intend to keep it that way.
											
										 
											2003-06-27 20:48:05 +00:00
										 |  |  |   This makes it clumsy to write a doctest showing boolean results that | 
					
						
							|  |  |  |   passes under multiple versions of Python.  In Python 2.3, by default, | 
					
						
							|  |  |  |   and as a special case, if an expected output block consists solely | 
					
						
							| 
									
										
										
										
											2003-06-28 03:09:06 +00:00
										 |  |  |   of \code{'0'} and the actual output block consists solely of | 
					
						
							|  |  |  |   \code{'False'}, that's accepted as an exact match, and similarly for | 
					
						
							|  |  |  |   \code{'1'} versus \code{'True'}.  This behavior can be turned off by | 
					
						
							| 
									
										
										
											
												A hack to ease compatibility with pre-2.3 Pythons:  by default, doctest
now accepts "True" when a test expects "1", and similarly for "False"
versus "0".  This is un-doctest-like, but on balance makes it much
more pleasant to write doctests that pass under 2.2 and 2.3.  I expect
it to go away again, when 2.2 is forgotten.  In the meantime, there's
a new doctest module constant that can be passed to a new optional
argument, if you want to turn this behavior off.
Note that this substitution is very simple-minded:  the expected and
actual outputs have to consist of single tokens.  No attempt is made,
e.g., to accept [True, False] when a test expects [1, 0].  This is a
simple hack for simple tests, and I intend to keep it that way.
											
										 
											2003-06-27 20:48:05 +00:00
										 |  |  |   passing the new (in 2.3) module constant | 
					
						
							|  |  |  |   \constant{DONT_ACCEPT_TRUE_FOR_1} as the value of \function{testmod()}'s | 
					
						
							|  |  |  |   new (in 2.3) optional \var{optionflags} argument.  Some years after | 
					
						
							|  |  |  |   the integer spellings of booleans are history, this hack will | 
					
						
							|  |  |  |   probably be removed again. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-06-11 14:55:01 +00:00
										 |  |  | \end{enumerate} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \subsection{Soapbox} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-07-17 16:00:01 +00:00
										 |  |  | The first word in ``doctest'' is ``doc,'' and that's why the author | 
					
						
							|  |  |  | wrote \refmodule{doctest}: to keep documentation up to date.  It so | 
					
						
							|  |  |  | happens that \refmodule{doctest} makes a pleasant unit testing | 
					
						
							|  |  |  | environment, but that's not its primary purpose. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Choose docstring examples with care.  There's an art to this that | 
					
						
							|  |  |  | needs to be learned---it may not be natural at first.  Examples should | 
					
						
							|  |  |  | add genuine value to the documentation.  A good example can often be | 
					
						
							|  |  |  | worth many words.  If possible, show just a few normal cases, show | 
					
						
							|  |  |  | endcases, show interesting subtle cases, and show an example of each | 
					
						
							|  |  |  | kind of exception that can be raised.  You're probably testing for | 
					
						
							|  |  |  | endcases and subtle cases anyway in an interactive shell: | 
					
						
							|  |  |  | \refmodule{doctest} wants to make it as easy as possible to capture | 
					
						
							|  |  |  | those sessions, and will verify they continue to work as designed | 
					
						
							|  |  |  | forever after. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If done with care, the examples will be invaluable for your users, and | 
					
						
							|  |  |  | will pay back the time it takes to collect them many times over as the | 
					
						
							|  |  |  | years go by and things change.  I'm still amazed at how often one of | 
					
						
							|  |  |  | my \refmodule{doctest} examples stops working after a ``harmless'' | 
					
						
							|  |  |  | change. | 
					
						
							| 
									
										
										
										
											2001-02-17 05:58:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | For exhaustive testing, or testing boring cases that add no value to the | 
					
						
							| 
									
										
										
										
											2001-02-17 17:32:41 +00:00
										 |  |  | docs, define a \code{__test__} dict instead.  That's what it's for. |