| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 158219b commit a140a7f
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -301,20 +301,19 @@ their contained methods and nested classes. | |||
| 301 | 301 | How are Docstring Examples Recognized? | |
| 302 | 302 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
| 303 | 303 | ||
| 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. | ||
| 310 | 310 | ||
| 311 | 311 | 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. | ||
| 313 | 313 | ||
| 314 | 314 | :: | |
| 315 | 315 | ||
| 316 | 316 | >>> # comments are ignored | |
| 317 | - ... | ||
| 318 | 317 | >>> import math | |
| 319 | 318 | >>> x = factorial(10); math.ceil(math.log10(x)) | |
| 320 | 319 | 7 | |
@@ -329,24 +328,27 @@ but doctest isn't trying to do an exact emulation of any specific Python shell. | |||
| 329 | 328 | ||
| 330 | 329 | The fine print: | |
| 331 | 330 | ||
| 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. | ||
| 338 | 334 | ||
| 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 | ||
| 341 | 337 | it as a failure. | |
| 342 | 338 | ||
| 343 | 339 | * 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. | ||
| 346 | 348 | ||
| 347 | 349 | * Expected output cannot contain an all-whitespace line, since such a line is | |
| 348 | 350 | 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 | ||
| 350 | 352 | is expected. | |
| 351 | 353 | ||
| 352 | 354 | * All hard tab characters are expanded to spaces, using 8-column tab stops. | |
@@ -390,29 +392,28 @@ The fine print: | |||
| 390 | 392 | 1 | |
| 391 | 393 | ||
| 392 | 394 | 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. | ||
| 394 | 396 | ||
| 395 | 397 | ||
| 396 | 398 | .. _doctest-execution-context: | |
| 397 | 399 | ||
| 398 | 400 | What's the Execution Context? | |
| 399 | 401 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
| 400 | 402 | ||
| 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 | ||
| 403 | 405 | have no output. | |
| 404 | 406 | ||
| 405 | 407 | 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. | ||
| 416 | 417 | ||
| 417 | 418 | You can force use of your own dict as the execution context by passing | |
| 418 | 419 | ``globs=your_dict`` to :func:`testmod` or :func:`testfile` instead. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments