Skip to content

Commit a140a7f

Browse files
author
Jim DeLaHunt
committed
Address bitdancer review comments, Doc/library/doctest.rst
1 parent 158219b commit a140a7f

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

Doc/library/doctest.rst

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -301,20 +301,19 @@ their contained methods and nested classes.
301301
How are Docstring Examples Recognized?
302302
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
303303

304-
The :mod:`doctest` module treats any line beginning with ``>>>`` as an
305-
example to be tested.
306-
Following lines which begin with ``...`` continue the example.
307-
Subsequent non-blank lines, if any, form an expected output string.
308-
A blank (all-whitespace) line, or a line with ``>>>`` (beginning the
309-
next example), ends the expected output string.
304+
A doctest example is composed of one or more tests. An individual test
305+
starts with a line that starts with '>>>', has zero or more code
306+
continuation lines that start with '...', and ends with zero or more
307+
expected output lines. The expected output ends at the first line that
308+
starts with '>>>' or is blank. All lines in an example block must have
309+
the same indentation level.
310310

311311
In most cases a copy-and-paste of an interactive console session works fine,
312-
but doctest isn't trying to do an exact emulation of any specific Python shell.
312+
but doctest isn't trying to do an exact emulation of the Python shell.
313313

314314
::
315315

316316
>>> # comments are ignored
317-
...
318317
>>> import math
319318
>>> x = factorial(10); math.ceil(math.log10(x))
320319
7
@@ -329,24 +328,27 @@ but doctest isn't trying to do an exact emulation of any specific Python shell.
329328

330329
The fine print:
331330

332-
* The ``>>>`` string tells the module to look for an interactive statement:
333-
that is, a statement list ending with a newline, or a
334-
:ref:`compound statement <compound>`.
335-
The ``...`` string tells the module that the line continues a compound
336-
statement. (Actually, doctest gets these strings from the PS1 and PS2
337-
values of the :mod:`sys` module.)
331+
* The >>> marks the start of an interactive statement: that is, a
332+
statement list ending with a newline, or a :ref:`compound statement <compound>`.
333+
The ... string indicates that the line continues a compound statement.
338334

339-
* The expected output can be absent. This indicates that the example generates
340-
no output when run. If the example does generate output, the module reports
335+
* If the expected output is empty, it indicates that the test generates
336+
no output when run. If the test does generate output, the module reports
341337
it as a failure.
342338

343339
* The expected output can contain multiple lines. These lines become a
344-
string, which is compared to the string of actual output from
345-
testing the example.
340+
string containing newlines. The leading indentation of the example
341+
block is stripped when building the string. The resulting string is
342+
compared to the string of actual output from running the test.
343+
344+
* The last code continuation line of an example copied from the
345+
interactive shell (the line starting with "..." that is otherwise
346+
blank in the example above) may be omitted without changing the
347+
meaning of the test.
346348

347349
* Expected output cannot contain an all-whitespace line, since such a line is
348350
taken to signal the end of expected output. If expected output does contain a
349-
blank line, put ``<BLANKLINE>`` in your doctest example each place a blank line
351+
blank line, put ``<BLANKLINE>`` in your doctest test each place a blank line
350352
is expected.
351353

352354
* All hard tab characters are expanded to spaces, using 8-column tab stops.
@@ -390,29 +392,28 @@ The fine print:
390392
1
391393

392394
and as many leading whitespace characters are stripped from the expected output
393-
as appeared in the initial ``'>>> '`` line that started the example.
395+
as appeared in the initial ``'>>> '`` line that started the test.
394396

395397

396398
.. _doctest-execution-context:
397399

398400
What's the Execution Context?
399401
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
400402

401-
Within a docstring, later examples can use names defined by earlier
402-
examples. It's fine for an example to set up state, and
403+
Within a docstring, later tests can use names defined by earlier
404+
examples. It's fine for an test to set up state, and
403405
have no output.
404406

405407
For each docstring, :mod:`doctest` makes (by default) a
406-
*shallow copy* of :mod:`M`'s globals.
407-
This means examples can freely use any names defined at the top level of
408-
:mod:`M`.
409-
When doctest tests examples, it doesn't change the module's real globals.
410-
411-
This shallow copy of globals is discarded at the end of each docstring,
412-
and copied afresh for the next docstring. Thus, examples in one docstring
413-
in :mod:`M` can't leave behind crumbs that accidentally allow an example
414-
in another docstring to work. Examples cannot see names defined in other
415-
docstrings.
408+
*shallow copy* of :mod:`M`'s globals. This means tests can freely
409+
use any names defined at the top level of :mod:`M`.
410+
When doctest performs tests, it doesn't change the module's real globals.
411+
412+
This shallow copy of globals is discarded after the docstring has been
413+
processed, and copied afresh for the next docstring. Thus, tests in one
414+
docstring in :mod:`M` can't leave behind crumbs that accidentally allow an
415+
test in another docstring to work. Tests cannot see names defined in
416+
other docstrings.
416417

417418
You can force use of your own dict as the execution context by passing
418419
``globs=your_dict`` to :func:`testmod` or :func:`testfile` instead.

0 commit comments

Comments
 (0)