@@ -170,7 +170,7 @@ with assorted summaries at the end.
170
170
171
171
You can force verbose mode by passing ``verbose=True `` to :func: `testmod `, or
172
172
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
174
174
has no effect).
175
175
176
176
There is also a command line shortcut for running :func: `testmod `, see section
@@ -227,7 +227,7 @@ documentation::
227
227
As with :func: `testmod `, :func: `testfile ` won't display anything unless an
228
228
example fails. If an example does fail, then the failing example(s) and the
229
229
cause(s) of the failure(s) are printed to stdout, using the same format as
230
- :func: `testmod `.
230
+ :func: `! testmod `.
231
231
232
232
By default, :func: `testfile ` looks for files in the calling module's directory.
233
233
See section :ref: `doctest-basic-api ` for a description of the optional arguments
@@ -307,6 +307,9 @@ Which Docstrings Are Examined?
307
307
The module docstring, and all function, class and method docstrings are
308
308
searched. Objects imported into the module are not searched.
309
309
310
+ .. attribute :: module.__test__
311
+ :no-typesetting:
312
+
310
313
In addition, there are cases when you want tests to be part of a module but not part
311
314
of the help text, which requires that the tests not be included in the docstring.
312
315
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:
529
532
* The interactive shell omits the traceback header line for some
530
533
:exc: `SyntaxError `\ s. But doctest uses the traceback header line to
531
534
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
533
536
manually add the traceback header line to your test example.
534
537
535
538
.. index :: single: ^ (caret); marker
@@ -856,15 +859,15 @@ The :const:`ELLIPSIS` directive gives a nice approach for the last example:
856
859
<C object at 0x...>
857
860
858
861
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. ::
861
864
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
868
871
869
872
Numbers of the form ``I/2.**J `` are safe across all platforms, and I often
870
873
contrive doctest examples to produce numbers of that form::
@@ -934,13 +937,13 @@ and :ref:`doctest-simple-testfile`.
934
937
935
938
Optional argument *verbose * prints lots of stuff if true, and prints only
936
939
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 `.
938
941
939
942
Optional argument *report * prints a summary at the end when true, else prints
940
943
nothing at the end. In verbose mode, the summary is detailed, else the summary
941
944
is very brief (in fact, empty if all tests passed).
942
945
943
- Optional argument *optionflags * (default value 0 ) takes the
946
+ Optional argument *optionflags * (default value `` 0 `` ) takes the
944
947
:ref: `bitwise OR <bitwise >` of option flags.
945
948
See section :ref: `doctest-options `.
946
949
@@ -1041,7 +1044,7 @@ from text files and modules with doctests:
1041
1044
1042
1045
The returned :class: `unittest.TestSuite ` is to be run by the unittest framework
1043
1046
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 `
1045
1048
exception is raised showing the name of the file containing the test and a
1046
1049
(sometimes approximate) line number. If all the examples in a file are
1047
1050
skipped, then the synthesized unit test is also marked as skipped.
@@ -1074,13 +1077,14 @@ from text files and modules with doctests:
1074
1077
1075
1078
Optional argument *setUp * specifies a set-up function for the test suite.
1076
1079
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.
1079
1082
1080
1083
Optional argument *tearDown * specifies a tear-down function for the test
1081
1084
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.
1084
1088
1085
1089
Optional argument *globs * is a dictionary containing the initial global
1086
1090
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:
1107
1111
Convert doctest tests for a module to a :class: `unittest.TestSuite `.
1108
1112
1109
1113
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
1112
1118
showing the name of the file containing the test and a (sometimes approximate)
1113
1119
line number. If all the examples in a docstring are skipped, then the
1114
- synthesized unit test is also marked as skipped.
1115
1120
1116
1121
Optional argument *module * provides the module to be tested. It can be a module
1117
1122
object or a (possibly dotted) module name. If not specified, the module calling
1118
1123
this function is used.
1119
1124
1120
1125
Optional argument *globs * is a dictionary containing the initial global
1121
1126
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__ ` .
1123
1128
1124
1129
Optional argument *extraglobs * specifies an extra set of global variables, which
1125
1130
is merged into *globs *. By default, no extra globals are used.
@@ -1128,20 +1133,14 @@ from text files and modules with doctests:
1128
1133
drop-in replacement) that is used to extract doctests from the module.
1129
1134
1130
1135
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 .
1132
1137
1133
1138
This function uses the same search technique as :func: `testmod `.
1134
1139
1135
1140
.. versionchanged :: 3.5
1136
1141
:func: `DocTestSuite ` returns an empty :class: `unittest.TestSuite ` if *module *
1137
1142
contains no docstrings instead of raising :exc: `ValueError `.
1138
1143
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
-
1145
1144
Under the covers, :func: `DocTestSuite ` creates a :class: `unittest.TestSuite ` out
1146
1145
of :class: `!doctest.DocTestCase ` instances, and :class: `!DocTestCase ` is a
1147
1146
subclass of :class: `unittest.TestCase `. :class: `!DocTestCase ` isn't documented
@@ -1154,15 +1153,15 @@ of :class:`!DocTestCase`.
1154
1153
1155
1154
So both ways of creating a :class: `unittest.TestSuite ` run instances of
1156
1155
: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
1160
1159
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.
1164
1163
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 `
1166
1165
reporting flags specific to :mod: `unittest ` support, via this function:
1167
1166
1168
1167
@@ -1177,12 +1176,12 @@ reporting flags specific to :mod:`unittest` support, via this function:
1177
1176
:mod: `unittest `: the :meth: `!runTest ` method of :class: `!DocTestCase ` looks at
1178
1177
the option flags specified for the test case when the :class: `!DocTestCase `
1179
1178
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
1181
1180
:ref: `bitwise ORed <bitwise >` into the option flags, and the option flags
1182
1181
so augmented are passed to the :class: `DocTestRunner ` instance created to
1183
1182
run the doctest. If any reporting flags were specified when the
1184
1183
:class: `!DocTestCase ` instance was constructed, :mod: `!doctest `'s
1185
- :mod: `unittest ` reporting flags are ignored.
1184
+ :mod: `! unittest ` reporting flags are ignored.
1186
1185
1187
1186
The value of the :mod: `unittest ` reporting flags in effect before the function
1188
1187
was called is returned by the function.
@@ -1275,7 +1274,7 @@ DocTest Objects
1275
1274
.. attribute :: filename
1276
1275
1277
1276 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
1279
1278
extracted from a file.
1280
1279
1281
1280
@@ -1415,10 +1414,10 @@ DocTestFinder objects
1415
1414
1416
1415
The globals for each :class: `DocTest ` is formed by combining *globs * and
1417
1416
*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 ``{} ``.
1422
1421
1423
1422
1424
1423
.. _doctest-doctestparser :
@@ -1442,7 +1441,7 @@ DocTestParser objects
1442
1441
:class: `DocTest ` object.
1443
1442
1444
1443
*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
1446
1445
information.
1447
1446
1448
1447
@@ -1457,7 +1456,7 @@ DocTestParser objects
1457
1456
1458
1457
Divide the given string into examples and intervening text, and return them as
1459
1458
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
1461
1460
identifying this string, and is only used for error messages.
1462
1461
1463
1462
@@ -1497,7 +1496,7 @@ DocTestRunner objects
1497
1496
:class: `OutputChecker `. This comparison may be customized with a number of
1498
1497
option flags; see section :ref: `doctest-options ` for more information. If the
1499
1498
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.
1501
1500
1502
1501
The test runner's display output can be controlled in two ways. First, an output
1503
1502
function can be passed to :meth: `run `; this function will be called
@@ -1536,7 +1535,7 @@ DocTestRunner objects
1536
1535
output; it should not be called directly.
1537
1536
1538
1537
*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
1540
1539
:meth: `DocTestRunner.run `.
1541
1540
1542
1541
@@ -1936,7 +1935,7 @@ several options for organizing tests:
1936
1935
containing test cases for the named topics. These functions can be included in
1937
1936
the same file as the module, or separated out into a separate test file.
1938
1937
1939
- * Define a `` __test__ ` ` dictionary mapping from regression test topics to
1938
+ * Define a :attr: ` ~module. __test__ ` dictionary mapping from regression test topics to
1940
1939
docstrings containing test cases.
1941
1940
1942
1941
When you have placed your tests in a module, the module can itself be the test
0 commit comments