@@ -174,7 +174,7 @@ with assorted summaries at the end.
174
174
175
175
You can force verbose mode by passing ``verbose=True `` to :func: `testmod `, or
176
176
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
178
178
has no effect).
179
179
180
180
There is also a command line shortcut for running :func: `testmod `, see section
@@ -231,7 +231,7 @@ documentation::
231
231
As with :func: `testmod `, :func: `testfile ` won't display anything unless an
232
232
example fails. If an example does fail, then the failing example(s) and the
233
233
cause(s) of the failure(s) are printed to stdout, using the same format as
234
- :func: `testmod `.
234
+ :func: `! testmod `.
235
235
236
236
By default, :func: `testfile ` looks for files in the calling module's directory.
237
237
See section :ref: `doctest-basic-api ` for a description of the optional arguments
@@ -311,6 +311,9 @@ Which Docstrings Are Examined?
311
311
The module docstring, and all function, class and method docstrings are
312
312
searched. Objects imported into the module are not searched.
313
313
314
+ .. attribute :: module.__test__
315
+ :no-typesetting:
316
+
314
317
In addition, there are cases when you want tests to be part of a module but not part
315
318
of the help text, which requires that the tests not be included in the docstring.
316
319
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:
533
536
* The interactive shell omits the traceback header line for some
534
537
:exc: `SyntaxError `\ s. But doctest uses the traceback header line to
535
538
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
537
540
manually add the traceback header line to your test example.
538
541
539
542
.. index :: single: ^ (caret); marker
@@ -860,15 +863,15 @@ The :const:`ELLIPSIS` directive gives a nice approach for the last example:
860
863
<C object at 0x...>
861
864
862
865
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. ::
865
868
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
872
875
873
876
Numbers of the form ``I/2.**J `` are safe across all platforms, and I often
874
877
contrive doctest examples to produce numbers of that form::
@@ -938,13 +941,13 @@ and :ref:`doctest-simple-testfile`.
938
941
939
942
Optional argument *verbose * prints lots of stuff if true, and prints only
940
943
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 `.
942
945
943
946
Optional argument *report * prints a summary at the end when true, else prints
944
947
nothing at the end. In verbose mode, the summary is detailed, else the summary
945
948
is very brief (in fact, empty if all tests passed).
946
949
947
- Optional argument *optionflags * (default value 0 ) takes the
950
+ Optional argument *optionflags * (default value `` 0 `` ) takes the
948
951
:ref: `bitwise OR <bitwise >` of option flags.
949
952
See section :ref: `doctest-options `.
950
953
@@ -1045,7 +1048,7 @@ from text files and modules with doctests:
1045
1048
1046
1049
The returned :class: `unittest.TestSuite ` is to be run by the unittest framework
1047
1050
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 `
1049
1052
exception is raised showing the name of the file containing the test and a
1050
1053
(sometimes approximate) line number. If all the examples in a file are
1051
1054
skipped, then the synthesized unit test is also marked as skipped.
@@ -1078,13 +1081,14 @@ from text files and modules with doctests:
1078
1081
1079
1082
Optional argument *setUp * specifies a set-up function for the test suite.
1080
1083
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.
1083
1086
1084
1087
Optional argument *tearDown * specifies a tear-down function for the test
1085
1088
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.
1088
1092
1089
1093
Optional argument *globs * is a dictionary containing the initial global
1090
1094
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:
1111
1115
Convert doctest tests for a module to a :class: `unittest.TestSuite `.
1112
1116
1113
1117
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
1116
1122
showing the name of the file containing the test and a (sometimes approximate)
1117
1123
line number. If all the examples in a docstring are skipped, then the
1118
- synthesized unit test is also marked as skipped.
1119
1124
1120
1125
Optional argument *module * provides the module to be tested. It can be a module
1121
1126
object or a (possibly dotted) module name. If not specified, the module calling
1122
1127
this function is used.
1123
1128
1124
1129
Optional argument *globs * is a dictionary containing the initial global
1125
1130
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__ ` .
1127
1132
1128
1133
Optional argument *extraglobs * specifies an extra set of global variables, which
1129
1134
is merged into *globs *. By default, no extra globals are used.
@@ -1132,20 +1137,14 @@ from text files and modules with doctests:
1132
1137
drop-in replacement) that is used to extract doctests from the module.
1133
1138
1134
1139
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 .
1136
1141
1137
1142
This function uses the same search technique as :func: `testmod `.
1138
1143
1139
1144
.. versionchanged :: 3.5
1140
1145
:func: `DocTestSuite ` returns an empty :class: `unittest.TestSuite ` if *module *
1141
1146
contains no docstrings instead of raising :exc: `ValueError `.
1142
1147
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
-
1149
1148
Under the covers, :func: `DocTestSuite ` creates a :class: `unittest.TestSuite ` out
1150
1149
of :class: `!doctest.DocTestCase ` instances, and :class: `!DocTestCase ` is a
1151
1150
subclass of :class: `unittest.TestCase `. :class: `!DocTestCase ` isn't documented
@@ -1158,15 +1157,15 @@ of :class:`!DocTestCase`.
1158
1157
1159
1158
So both ways of creating a :class: `unittest.TestSuite ` run instances of
1160
1159
: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
1164
1163
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.
1168
1167
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 `
1170
1169
reporting flags specific to :mod: `unittest ` support, via this function:
1171
1170
1172
1171
@@ -1181,12 +1180,12 @@ reporting flags specific to :mod:`unittest` support, via this function:
1181
1180
:mod: `unittest `: the :meth: `!runTest ` method of :class: `!DocTestCase ` looks at
1182
1181
the option flags specified for the test case when the :class: `!DocTestCase `
1183
1182
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
1185
1184
:ref: `bitwise ORed <bitwise >` into the option flags, and the option flags
1186
1185
so augmented are passed to the :class: `DocTestRunner ` instance created to
1187
1186
run the doctest. If any reporting flags were specified when the
1188
1187
:class: `!DocTestCase ` instance was constructed, :mod: `!doctest `'s
1189
- :mod: `unittest ` reporting flags are ignored.
1188
+ :mod: `! unittest ` reporting flags are ignored.
1190
1189
1191
1190
The value of the :mod: `unittest ` reporting flags in effect before the function
1192
1191
was called is returned by the function.
@@ -1279,7 +1278,7 @@ DocTest Objects
1279
1278
.. attribute :: filename
1280
1279
1281
1280
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
1283
1282
extracted from a file.
1284
1283
1285
1284
@@ -1419,10 +1418,10 @@ DocTestFinder objects
1419
1418
1420
1419
The globals for each :class: `DocTest ` is formed by combining *globs * and
1421
1420
*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 ``{} ``.
1426
1425
1427
1426
1428
1427
.. _doctest-doctestparser :
@@ -1446,7 +1445,7 @@ DocTestParser objects
E41F
1446
1445
:class: `DocTest ` object.
1447
1446
1448
1447
*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
1450
1449
information.
1451
1450
1452
1451
@@ -1461,7 +1460,7 @@ DocTestParser objects
1461
1460
1462
1461
Divide the given string into examples and intervening text, and return them as
1463
1462
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
1465
1464
identifying this string, and is only used for error messages.
1466
1465
1467
1466
@@ -1501,14 +1500,14 @@ DocTestRunner objects
1501
1500
:class: `OutputChecker `. This comparison may be customized with a number of
1502
1501
option flags; see section :ref: `doctest-options ` for more information. If the
1503
1502
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.
1505
1504
1506
1505
The test runner's display output can be controlled in two ways. First, an output
1507
1506
function can be passed to :meth: `run `; this function will be called
1508
1507
with strings that should be displayed. It defaults to ``sys.stdout.write ``. If
1509
1508
capturing the output is not sufficient, then the display output can be also
1510
1509
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 `,
1512
1511
:meth: `report_unexpected_exception `, and :meth: `report_failure `.
1513
1512
1514
1513
The optional keyword argument *checker * specifies the :class: `OutputChecker `
@@ -1540,7 +1539,7 @@ DocTestRunner objects
1540
1539
output; it should not be called directly.
1541
1540
1542
1541
*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
1544
1543
:meth: `DocTestRunner.run `.
1545
1544
1546
1545
@@ -1940,7 +1939,7 @@ several options for organizing tests:
1940
1939
containing test cases for the named topics. These functions can be included in
1941
1940
the same file as the module, or separated out into a separate test file.
1942
1941
1943
- * Define a `` __test__ ` ` dictionary mapping from regression test topics to
1942
+ * Define a :attr: ` ~module. __test__ ` dictionary mapping from regression test topics to
1944
1943
docstrings containing test cases.
1945
1944
1946
1945
When you have placed your tests in a module, the module can itself be the test
0 commit comments