Since the LaTeX isn't doctest'ed, examples are always wrong <wink>.

This commit is contained in:
Tim Peters 2004-09-25 00:10:53 +00:00
parent 27ebcae450
commit 7a082142d8

View file

@ -2,8 +2,8 @@
Test interactive Python examples} Test interactive Python examples}
\declaremodule{standard}{doctest} \declaremodule{standard}{doctest}
\moduleauthor{Tim Peters}{tim_one@users.sourceforge.net} \moduleauthor{Tim Peters}{tim@python.org}
\sectionauthor{Tim Peters}{tim_one@users.sourceforge.net} \sectionauthor{Tim Peters}{tim@python.org}
\sectionauthor{Moshe Zadka}{moshez@debian.org} \sectionauthor{Moshe Zadka}{moshez@debian.org}
\sectionauthor{Edward Loper}{edloper@users.sourceforge.net} \sectionauthor{Edward Loper}{edloper@users.sourceforge.net}
@ -11,17 +11,21 @@
The \module{doctest} module searches for pieces of text that look like The \module{doctest} module searches for pieces of text that look like
interactive Python sessions, and then executes those sessions to interactive Python sessions, and then executes those sessions to
verify that they work exactly as shown. There are two common ways to verify that they work exactly as shown. There are several common ways to
use doctest: use doctest:
\begin{enumerate} \begin{itemize}
\item To check that a module's docstrings are up-to-date by verifying \item To check that a module's docstrings are up-to-date by verifying
that all interactive examples still work as documented. that all interactive examples still work as documented.
\item To perform regression testing by verifying that interactive \item To perform regression testing by verifying that interactive
examples from a test file or a test object work as expected. examples from a test file or a test object work as expected.
\end{enumerate} \item To write tutorial documentation for a package, liberally
illustrated with input-ouput examples. Depending on whether
the examples or the expository text are emphasized, this has
the flavor of "literate testing" or "executable documentation".
\end{itemize}
Here's a complete but small example: Here's a complete but small example module:
\begin{verbatim} \begin{verbatim}
""" """
@ -81,16 +85,13 @@ def factorial(n):
result = 1 result = 1
factor = 2 factor = 2
while factor <= n: while factor <= n:
try:
result *= factor result *= factor
except OverflowError:
result *= long(factor)
factor += 1 factor += 1
return result return result
def _test(): def _test():
import doctest import doctest
return doctest.testmod() doctest.testmod()
if __name__ == "__main__": if __name__ == "__main__":
_test() _test()
@ -138,10 +139,12 @@ Expecting:
... ...
OverflowError: n too large OverflowError: n too large
ok ok
1 items had no tests:
__main__._test
2 items passed all tests: 2 items passed all tests:
1 tests in example 1 tests in __main__
8 tests in example.factorial 8 tests in __main__.factorial
9 tests in 2 items. 9 tests in 3 items.
9 passed and 0 failed. 9 passed and 0 failed.
Test passed. Test passed.
$ $
@ -150,7 +153,8 @@ $
That's all you need to know to start making productive use of That's all you need to know to start making productive use of
\module{doctest}! Jump in. The following sections provide full \module{doctest}! Jump in. The following sections provide full
details. Note that there are many examples of doctests in details. Note that there are many examples of doctests in
the standard Python test suite and libraries. the standard Python test suite and libraries. Especially useful examples
can be found in the standard test file \file{Lib/test/test_doctest.py}.
\subsection{Simple Usage: Checking Examples in \subsection{Simple Usage: Checking Examples in
Docstrings\label{doctest-simple-testmod}} Docstrings\label{doctest-simple-testmod}}