44
instruct the Python interpreter to run the doctest module directly from the standard library
and pass the file name(s) on the command line:
python -m doctest -v example.txt
Because the file name does not end with
.py
,
doctest
infers that it must be run with
testfile()
, not
testmod()
.
For more information on
testfile()
, see section Basic API.
25.2.3. How It Works
This section examines in detail how doctest works: which docstrings it looks at, how it
finds interactive examples, what execution context it uses, how it handles exceptions, and
how option flags can be used to control its behavior. This is the information that you need
to know to write doctest examples; for information about actually running doctest on
these examples, see the following sections.
25.2.3.1. Which Docstrings Are Examined?
The module docstring, and all function, class and method docstrings are searched.
Objects imported into the module are not searched.
In addition, if
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
M.__test__
are searched, and strings are treated as if they were
docstrings. In output, a key
K
in
M.__test__
appears with name
<name of M>.__test__.K
Any classes found are recursively searched similarly, to test docstrings in their contained
methods and nested classes.
Changed in version 2.4: A “private name” concept is deprecated and no longer
documented.
25.2.3.2. How are Docstring Examples Recognized?
In most cases a copy-and-paste of an interactive console session works fine, but doctest
isn’t trying to do an exact emulation of any specific Python shell.