10000 gh-134918: Fix and improve doctest's documentation · python/cpython@bdcf54a · GitHub
[go: up one dir, main page]

Skip to content

Commit bdcf54a

Browse files
gh-134918: Fix and improve doctest's documentation
1 parent 2f2bee2 commit bdcf54a

File tree

1 file changed

+48
-49
lines changed

1 file changed

+48
-49
lines changed

Doc/library/doctest.rst

Lines changed: 48 additions & 49 deletions
E41F
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ with assorted summaries at the end.
174174

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

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

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

314+
.. attribute:: module.__test__
315+
:no-typesetting:
316+
314317
In addition, there are cases when you want tests to be part of a module but not part
315318
of the help text, which requires that the tests not be included in the docstring.
316319
Doctest looks for a module-level variable called ``__test__`` and uses it to locate other
@@ -533,7 +536,7 @@ Some details you should read once, but won't need to remember:
533536
* The interactive shell omits the traceback header line for some
534537
:exc:`SyntaxError`\ s. But doctest uses the traceback header line to
535538
distinguish exceptions from non-exceptions. So in the rare case where you need
536-
to test a :exc:`SyntaxError` that omits the traceback header, you will need to
539+
to test a :exc:`!SyntaxError` that omits the traceback header, you will need to
537540
manually add the traceback header line to your test example.
538541

539542
.. index:: single: ^ (caret); marker
@@ -860,15 +863,15 @@ The :const:`ELLIPSIS` directive gives a nice approach for the last example:
860863
<C object at 0x...>
861864

862865
Floating-point numbers are also subject to small output variations across
863-
platforms, because Python defers to the platform C library for float formatting,
864-
and C libraries vary widely in quality here. ::
866+
platforms, because Python defers to the platform C library for some
867+
floating-point calculations, and C libraries vary widely in quality here. ::
865868

866-
>>> 1./7 # risky
867-
0.14285714285714285
868-
>>> print(1./7) # safer
869-
0.142857142857
870-
>>> print(round(1./7, 6)) # much safer
871-
0.142857
869+
>>> 1000**0.1 # risky
870+
1.9952623149688797
871+
>>> round(1000**0.1, 9) # safer
872+
1.995262315
873+
>>> print(f'{1000**0.1:.4f}') # much safer
874+
1.9953
872875

873876
Numbers of the form ``I/2.**J`` are safe across all platforms, and I often
874877
contrive doctest examples to produce numbers of that form::
@@ -938,13 +941,13 @@ and :ref:`doctest-simple-testfile`.
938941

939942
Optional argument *verbose* prints lots of stuff if true, and prints only
940943
failures if false; by default, or if ``None``, it's true if and only if ``'-v'``
941-
is in ``sys.argv``.
944+
is in :data:`sys.argv`.
942945

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

947-
Optional argument *optionflags* (default value 0) takes the
950+
Optional argument *optionflags* (default value ``0``) takes the
948951
:ref:`bitwise OR <bitwise>` of option flags.
949952
See section :ref:`doctest-options`.
950953

@@ -1045,7 +1048,7 @@ from text files and modules with doctests:
10451048

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

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

10841087
Optional argument *tearDown* specifies a tear-down function for the test
10851088
suite. This is called after running the tests in each file. The *tearDown*
1086-
function will be passed a :class:`DocTest` object. The setUp function can
1087-
access the test globals as the *globs* attribute of the test passed.
1089+
function will be passed a :class:`DocTest` object. The *tearDown* function can
1090+
access the test globals as the :attr:`~DocTest.globs` attribute of the test
1091+
passed.
10881092

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

11131117
The returned :class:`unittest.TestSuite` is to be run by the unittest framework
1114-
and runs each doctest in the module. If any of the doctests fail, then the
1115-
synthesized unit test fails, and a :exc:`failureException` exception is raised
1118+
and runs each doctest in the module.
1119+
Each docstring is run as a separate unit test.
1120+
If any of the doctests fail, then the synthesized unit test fails,
1121+
and a :exc:`unittest.TestCase.failureException` exception is raised
11161122
showing the name of the file containing the test and a (sometimes approximate)
11171123
line number. If all the examples in a docstring are skipped, then the
1118-
synthesized unit test is also marked as skipped.
11191124

11201125
Optional argument *module* provides the module to be tested. It can be a module
11211126
object or a (possibly dotted) module name. If not specified, the module calling
11221127
this function is used.
11231128

11241129
Optional argument *globs* is a dictionary containing the initial global
11251130
variables for the tests. A new copy of this dictionary is created for each
1126-
test. By default, *globs* is a new empty dictionary.
1131+
test. By default, *globs* is the module's :attr:`~module.__dict__`.
11271132

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

11341139
Optional arguments *setUp*, *tearDown*, and *optionflags* are the same as for
1135-
function :func:`DocFileSuite` above.
1140+
function :func:`DocFileSuite` above, but they are called for each docstring.
11361141

11371142
This function uses the same search technique as :func:`testmod`.
11381143

11391144
.. versionchanged:: 3.5
11401145
:func:`DocTestSuite` returns an empty :class:`unittest.TestSuite` if *module*
11411146
contains no docstrings instead of raising :exc:`ValueError`.
11421147

1143-
.. exception:: failureException
1144-
1145-
When doctests which have been converted to unit tests by :func:`DocFileSuite`
1146-
or :func:`DocTestSuite` fail, this exception is raised showing the name of
1147-
the file containing the test and a (sometimes approximate) line number.
1148-
11491148
Under the covers, :func:`DocTestSuite` creates a :class:`unittest.TestSuite` out
11501149
of :class:`!doctest.DocTestCase` instances, and :class:`!DocTestCase` is a
11511150
subclass of :class:`unittest.TestCase`. :class:`!DocTestCase` isn't documented
@@ -1158,15 +1157,15 @@ of :class:`!DocTestCase`.
11581157

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

1169-
For this reason, :mod:`doctest` also supports a notion of :mod:`doctest`
1168+
For this reason, :mod:`doctest` also supports a notion of :mod:`!doctest`
11701169
reporting flags specific to :mod:`unittest` support, via this function:
11711170

11721171

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

11911190
The value of the :mod:`unittest` reporting flags in effect before the function
11921191
was called is returned by the function.
@@ -1279,7 +1278,7 @@ DocTest Objects
12791278
.. attribute:: filename
12801279

12811280
The name of the file that this :class:`DocTest` was extracted from; or
1282-
``None`` if the filename is unknown, or if the :class:`DocTest` was not
1281+
``None`` if the filename is unknown, or if the :class:`!DocTest` was not
12831282
extracted from a file.
12841283

12851284

@@ -1419,10 +1418,10 @@ DocTestFinder objects
14191418

14201419
The globals for each :class:`DocTest` is formed by combining *globs* and
14211420
*extraglobs* (bindings in *extraglobs* override bindings in *globs*). A new
1422-
shallow copy of the globals dictionary is created for each :class:`DocTest`.
1423-
If *globs* is not specified, then it defaults to the module's *__dict__*, if
1424-
specified, or ``{}`` otherwise. If *extraglobs* is not specified, then it
1425-
defaults to ``{}``.
1421+
shallow copy of the globals dictionary is created for each :class:`!DocTest`.
1422+
If *globs* is not specified, then it defaults to the module's
1423+
:attr:`~module.__dict__`, if specified, or ``{}`` otherwise.
1424+
If *extraglobs* is not specified, then it defaults to ``{}``.
14261425

14271426

14281427
.. _doctest-doctestparser:
@@ -1446,7 +1445,7 @@ DocTestParser objects
14461445
:class:`DocTest` object.
14471446

14481447
*globs*, *name*, *filename*, and *lineno* are attributes for the new
1449-
:class:`DocTest` object. See the documentation for :class:`DocTest` for more
1448+
:class:`!DocTest` object. See the documentation for :class:`DocTest` for more
14501449
information.
14511450

14521451

@@ -1461,7 +1460,7 @@ DocTestParser objects
14611460

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

14671466

@@ -1501,14 +1500,14 @@ DocTestRunner objects
15011500
:class:`OutputChecker`. This comparison may be customized with a number of
15021501
option flags; see section :ref:`doctest-options` for more information. If the
15031502
option flags are insufficient, then the comparison may also be customized by
1504-
passing a subclass of :class:`OutputChecker` to the constructor.
1503+
passing a subclass of :class:`!OutputChecker` to the constructor.
15051504

15061505
The test runner's display output can be controlled in two ways. First, an output
15071506
function can be passed to :meth:`run`; this function will be called
15081507
with strings that should be displayed. It defaults to ``sys.stdout.write``. If
15091508
capturing the output is not sufficient, then the display output can be also
15101509
customized by subclassing DocTestRunner, and overriding the methods
1511-
:meth:`report_start`, :meth:`report_success`,
1510+
:meth:`report_skip`, :meth:`report_start`, :meth:`report_success`,
15121511
:meth:`report_unexpected_exception`, and :meth:`report_failure`.
15131512

15141513
The optional keyword argument *checker* specifies the :class:`OutputChecker`
@@ -1540,7 +1539,7 @@ DocTestRunner objects
15401539
output; it should not be called directly.
15411540

15421541
*example* is the example about to be processed. *test* is the test
1543-
*containing example*. *out* is the output function that was passed to
1542+
containing *example*. *out* is the output function that was passed to
15441543
:meth:`DocTestRunner.run`.
15451544

15461545

@@ -1940,7 +1939,7 @@ several options for organizing tests:
19401939
containing test cases for the named topics. These functions can be included in
19411940
the same file as the module, or separated out into a separate test file.
19421941

1943-
* Define a ``__test__`` dictionary mapping from regression test topics to
1942+
* Define a :attr:`~module.__test__` dictionary mapping from regression test topics to
19441943
docstrings containing test cases.
19451944

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

0 commit comments

Comments
 (0)
0