8000 gh-134918: Fix and improve doctest's documentation (GH-134919) · miss-islington/cpython@d9c4ec0 · GitHub
[go: up one dir, main page]

Skip to content

Commit d9c4ec0

Browse files
serhiy-storchakamiss-islington
authored andcommitted
pythongh-134918: Fix and improve doctest's documentation (pythonGH-134919)
(cherry picked from commit 3c66e59) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 19b31d1 commit d9c4ec0

File tree

1 file changed

+47
-48
lines changed

1 file changed

+47
-48
lines changed

Doc/library/doctest.rst

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ with assorted summaries at the end.
170170

171171
You can force verbose mode by passing ``verbose=True`` to :func:`testmod`, or
172172
prohibit it by passing ``verbose=False``. In either of those cases,
173-
``sys.argv`` is not examined by :func:`testmod` (so passing ``-v`` or not
173+
:data:`sys.argv` is not examined by :func:`testmod` (so passing ``-v`` or not
174174
has no effect).
175175

176176
There is also a command line shortcut for running :func:`testmod`, see section
@@ -227,7 +227,7 @@ documentation::
227227
As with :func:`testmod`, :func:`testfile` won't display anything unless an
228228
example fails. If an example does fail, then the failing example(s) and the
229229
cause(s) of the failure(s) are printed to stdout, using the same format as
230-
:func:`testmod`.
230+
:func:`!testmod`.
231231

232232
By default, :func:`testfile` looks for files in the calling module's directory.
233233
See section :ref:`doctest-basic-api` for a description of the optional arguments
@@ -307,6 +307,9 @@ Which Docstrings Are Examined?
307307
The module docstring, and all function, class and method docstrings are
308308
searched. Objects imported into the module are not searched.
309309

310+
.. attribute:: module.__test__
311+
:no-typesetting:
312+
310313
In addition, there are cases when you want tests to be part of a module but not part
311314
of the help text, which requires that the tests not be included in the docstring.
312315
Doctest looks for a module-level variable called ``__test__`` and uses it to locate other
@@ -529,7 +532,7 @@ Some details you should read once, but won't need to remember:
529532
* The interactive shell omits the traceback header line for some
530533
:exc:`SyntaxError`\ s. But doctest uses the traceback header line to
531534
distinguish exceptions from non-exceptions. So in the rare case where you need
532-
to test a :exc:`SyntaxError` that omits the traceback header, you will need to
535+
to test a :exc:`!SyntaxError` that omits the traceback header, you will need to
533536
manually add the traceback header line to your test example.
534537

535538
.. index:: single: ^ (caret); marker
@@ -856,15 +859,15 @@ The :const:`ELLIPSIS` directive gives a nice approach for the last example:
856859
<C object at 0x...>
857860

858861
Floating-point numbers are also subject to small output variations across
859-
platforms, because Python defers to the platform C library for float formatting,
860-
and C libraries vary widely in quality here. ::
862+
platforms, because Python defers to the platform C library for some
863+
floating-point calculations, and C libraries vary widely in quality here. ::
861864

862-
>>> 1./7 # risky
863-
0.14285714285714285
864-
>>> print(1./7) # safer
865-
0.142857142857
866-
>>> print(round(1./7, 6)) # much safer
867-
0.142857
865+
>>> 1000**0.1 # risky
866+
1.9952623149688797
867+
>>> round(1000**0.1, 9) # safer
868+
1.995262315
869+
>>> print(f'{1000**0.1:.4f}') # much safer
870+
1.9953
868871

869872
Numbers of the form ``I/2.**J`` are safe across all platforms, and I often
870873
contrive doctest examples to produce numbers of that form::
@@ -934,13 +937,13 @@ and :ref:`doctest-simple-testfile`.
934937

935938
Optional argument *verbose* prints lots of stuff if true, and prints only
936939
failures if false; by default, or if ``None``, it's true if and only if ``'-v'``
937-
is in ``sys.argv``.
940+
is in :data:`sys.argv`.
938941

939942
Optional argument *report* prints a summary at the end when true, else prints
940943
nothing at the end. In verbose mode, the summary is detailed, else the summary
941944
is very brief (in fact, empty if all tests passed).
942945

943-
Optional argument *optionflags* (default value 0) takes the
946+
Optional argument *optionflags* (default value ``0``) takes the
944947
:ref:`bitwise OR <bitwise>` of option flags.
945948
See section :ref:`doctest-options`.
946949

@@ -1041,7 +1044,7 @@ from text files and modules with doctests:
10411044

10421045
The returned :class:`unittest.TestSuite` is to be run by the unittest framework
10431046
and runs the interactive examples in each file. If an example in any file
1044-
fails, then the synthesized unit test fails, and a :exc:`failureException`
1047+
fails, then the synthesized unit test fails, and a :exc:`~unittest.TestCase.failureException`
10451048
exception is raised showing the name of the file containing the test and a
10461049
(sometimes approximate) line number. If all the examples in a file are
10471050
skipped, then the synthesized unit test is also marked as skipped.
@@ -1074,13 +1077,14 @@ from text files and modules with doctests:
10741077

10751078
Optional argument *setUp* specifies a set-up function for the test suite.
10761079
This is called before running the tests in each file. The *setUp* function
1077-
will be passed a :class:`DocTest` object. The setUp function can access the
1078-
test globals as the *globs* attribute of the test passed.
1080+
will be passed a :class:`DocTest` object. The *setUp* function can access the
1081+
test globals as the :attr:`~DocTest.globs` attribute of the test passed.
10791082

10801083
Optional argument *tearDown* specifies a tear-down function for the test
10811084
suite. This is called after running the tests in each file. The *tearDown*
1082-
function will be passed a :class:`DocTest` object. The setUp function can
1083-
access the test globals as the *globs* attribute of the test passed.
1085+
function will be passed a :class:`DocTest` object. The *tearDown* function can
1086+
access the test globals as the :attr:`~DocTest.globs` attribute of the test
1087+
passed.
10841088

10851089
Optional argument *globs* is a dictionary containing the initial global
10861090
variables for the tests. A new copy of this dictionary is created for each
@@ -1107,19 +1111,20 @@ from text files and modules with doctests:
11071111
Convert doctest tests for a module to a :class:`unittest.TestSuite`.
11081112

11091113
The returned :class:`unittest.TestSuite` is to be run by the unittest framework
1110-
and runs each doctest in the module. If any of the doctests fail, then the
1111-
synthesized unit test fails, and a :exc:`failureException` exception is raised
1114+
and runs each doctest in the module.
1115+
Each docstring is run as a separate unit test.
1116+
If any of the doctests fail, then the synthesized unit test fails,
1117+
and a :exc:`unittest.TestCase.failureException` exception is raised
11121118
showing the name of the file containing the test and a (sometimes approximate)
11131119
line number. If all the examples in a docstring are skipped, then the
1114-
synthesized unit test is also marked as skipped.
11151120

11161121
Optional argument *module* provides the module to be tested. It can be a module
11171122
object or a (possibly dotted) module name. If not specified, the module calling
11181123
this function is used.
11191124

11201125
Optional argument *globs* is a dictionary containing the initial global
11211126
variables for the tests. A new copy of this dictionary is created for each
1122-
test. By default, *globs* is a new empty dictionary.
1127+
test. By default, *globs* is the module's :attr:`~module.__dict__`.
11231128

11241129
Optional argument *extraglobs* specifies an extra set of global variables, which
11251130
is merged into *globs*. By default, no extra globals are used.
@@ -1128,20 +1133,14 @@ from text files and modules with doctests:
11281133
drop-in replacement) that is used to extract doctests from the module.
11291134

11301135
Optional arguments *setUp*, *tearDown*, and *optionflags* are the same as for
1131-
function :func:`DocFileSuite` above.
1136+
function :func:`DocFileSuite` above, but they are called for each docstring.
11321137

11331138
This function uses the same search technique as :func:`testmod`.
11341139

11351140
.. versionchanged:: 3.5
11361141
:func:`DocTestSuite` returns an empty :class:`unittest.TestSuite` if *module*
11371142
contains no docstrings instead of raising :exc:`ValueError`.
11381143

1139-
.. exception:: failureException
1140-
1141-
When doctests which have been converted to unit tests by :func:`DocFileSuite`
1142-
or :func:`DocTestSuite` fail, this exception is raised showing the name of
1143-
the file containing the test and a (sometimes approximate) line number.
1144-
11451144
Under the covers, :func:`DocTestSuite` creates a :class:`unittest.TestSuite` out
11461145
of :class:`!doctest.DocTestCase` instances, and :class:`!DocTestCase` is a
11471146
subclass of :class:`unittest.TestCase`. :class:`!DocTestCase` isn't documented
@@ -1154,15 +1153,15 @@ of :class:`!DocTestCase`.
11541153

11551154
So both ways of creating a :class:`unittest.TestSuite` run instances of
11561155
:class:`!DocTestCase`. This is important for a subtle reason: when you run
1157-
:mod:`doctest` functions yourself, you can control the :mod:`doctest` options in
1158-
use directly, by passing option flags to :mod:`doctest` functions. However, if
1159-
you're writing a :mod:`unittest` framework, :mod:`unittest` ultimately controls
1156+
:mod:`doctest` functions yourself, you can control the :mod:`!doctest` options in
1157+
use directly, by passing option flags to :mod:`!doctest` functions. However, if
1158+
you're writing a :mod:`unittest` framework, :mod:`!unittest` ultimately controls
11601159
when and how tests get run. The framework author typically wants to control
1161-
:mod:`doctest` reporting options (perhaps, e.g., specified by command line
1162-
options), but there's no way to pass options through :mod:`unittest` to
1163-
:mod:`doctest` test runners.
1160+
:mod:`!doctest` reporting options (perhaps, e.g., specified by command line
1161+
options), but there's no way to pass options through :mod:`!unittest` to
1162+
:mod:`!doctest` test runners.
11641163

1165-
For this reason, :mod:`doctest` also supports a notion of :mod:`doctest`
1164+
For this reason, :mod:`doctest` also supports a notion of :mod:`!doctest`
11661165
reporting flags specific to :mod:`unittest` support, via this function:
11671166

11681167

@@ -1177,12 +1176,12 @@ reporting flags specific to :mod:`unittest` support, via this function:
11771176
:mod:`unittest`: the :meth:`!runTest` method of :class:`!DocTestCase` looks at
11781177
the option flags specified for the test case when the :class:`!DocTestCase`
11791178
instance was constructed. If no reporting flags were specified (which is the
1180-
typical and expected case), :mod:`!doctest`'s :mod:`unittest` reporting flags are
1179+
typical and expected case), :mod:`!doctest`'s :mod:`!unittest` reporting flags are
11811180
:ref:`bitwise ORed <bitwise>` into the option flags, and the option flags
11821181
so augmented are passed to the :class:`DocTestRunner` instance created to
11831182
run the doctest. If any reporting flags were specified when the
11841183
:class:`!DocTestCase` instance was constructed, :mod:`!doctest`'s
1185-
:mod:`unittest` reporting flags are ignored.
1184+
:mod:`!unittest` reporting flags are ignored.
11861185

11871186
The value of the :mod:`unittest` reporting flags in effect before the function
11881187
was called is returned by the function.
@@ -1275,7 +1274,7 @@ DocTest Objects
12751274
.. attribute:: filename
12761275

12771276
The name of the file that this :class:`DocTest` was extracted from; or
1278-
``None`` if the filename is unknown, or if the :class:`DocTest` was not
1277+
``None`` if the filename is unknown, or if the :class:`!DocTest` was not
12791278
extracted from a file.
12801279

12811280

@@ -1415,10 +1414,10 @@ DocTestFinder objects
14151414

14161415
The globals for each :class:`DocTest` is formed by combining *globs* and
14171416
*extraglobs* (bindings in *extraglobs* override bindings in *globs*). A new
1418-
shallow copy of the globals dictionary is created for each :class:`DocTest`.
1419-
If *globs* is not specified, then it defaults to the module's *__dict__*, if
1420-
specified, or ``{}`` otherwise. If *extraglobs* is not specified, then it
1421-
defaults to ``{}``.
1417+
shallow copy of the globals dictionary is created for each :class:`!DocTest`.
1418+
If *globs* is not specified, then it defaults to the module's
1419+
:attr:`~module.__dict__`, if specified, or ``{}`` otherwise.
1420+
If *extraglobs* is not specified, then it defaults to ``{}``.
14221421

14231422

14241423
.. _doctest-doctestparser:
@@ -1442,7 +1441,7 @@ DocTestParser objects
14421441
:class:`DocTest` object.
14431442

14441443
*globs*, *name*, *filename*, and *lineno* are attributes for the new
1445-
:class:`DocTest` object. See the documentation for :class:`DocTest` for more
1444+
:class:`!DocTest` object. See the documentation for :class:`DocTest` for more
14461445
information.
14471446

14481447

@@ -1457,7 +1456,7 @@ DocTestParser objects
14571456

14581457
Divide the given string into examples and intervening text, and return them as
14591458
a list of alternating :class:`Example`\ s and strings. Line numbers for the
1460-
:class:`Example`\ s are 0-based. The optional argument *name* is a name
1459+
:class:`!Example`\ s are 0-based. The optional argument *name* is a name
14611460
identifying this string, and is only used for error messages.
14621461

14631462

@@ -1497,7 +1496,7 @@ DocTestRunner objects
14971496
:class:`OutputChecker`. This comparison may be customized with a number of
14981497
option flags; see section :ref:`doctest-options` for more information. If the
14991498
option flags are insufficient, then the comparison may also be customized by
1500-
passing a subclass of :class:`OutputChecker` to the constructor.
1499+
passing a subclass of :class:`!OutputChecker` to the constructor.
15011500

15021501
The test runner's display output can be controlled in two ways. First, an output
15031502
function can be passed to :meth:`run`; this function will be called
@@ -1536,7 +1535,7 @@ DocTestRunner objects
15361535
output; it should not be called directly.
15371536

15381537
*example* is the example about to be processed. *test* is the test
1539-
*containing example*. *out* is the output function that was passed to
1538+
containing *example*. *out* is the output function that was passed to
15401539
:meth:`DocTestRunner.run`.
15411540

15421541

@@ -1936,7 +1935,7 @@ several options for organizing tests:
19361935
containing test cases for the named topics. These functions can be included in
19371936
the same file as the module, or separated out into a separate test file.
19381937

1939-
* Define a ``__test__`` dictionary mapping from regression test topics to
1938+
* Define a :attr:`~module.__test__` dictionary mapping from regression test topics to
19401939
docstrings containing test cases.
19411940

19421941
When you have placed your tests in a module, the module can itself be the test

0 commit comments

Comments
 (0)
0