Changeset 391 for python/trunk/Doc/library/doctest.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/library/doctest.rst
r2 r391 1 :keepdoctest: 2 1 3 :mod:`doctest` --- Test interactive Python examples 2 4 =================================================== … … 96 98 97 99 There's no output! That's normal, and it means all the examples worked. Pass 98 :option:`-v` to the script, and :mod:`doctest` prints a detailed log of what100 ``-v`` to the script, and :mod:`doctest` prints a detailed log of what 99 101 it's trying, and prints a summary at the end:: 100 102 … … 164 166 number of examples that failed. 165 167 166 Run it with the :option:`-v` switch instead::168 Run it with the ``-v`` switch instead:: 167 169 168 170 python M.py -v … … 173 175 You can force verbose mode by passing ``verbose=True`` to :func:`testmod`, or 174 176 prohibit it by passing ``verbose=False``. In either of those cases, 175 ``sys.argv`` is not examined by :func:`testmod` (so passing :option:`-v` or not177 ``sys.argv`` is not examined by :func:`testmod` (so passing ``-v`` or not 176 178 has no effect). 177 179 … … 243 245 244 246 Like :func:`testmod`, :func:`testfile`'s verbosity can be set with the 245 :option:`-v` command-line switch or with the optional keyword argument247 ``-v`` command-line switch or with the optional keyword argument 246 248 *verbose*. 247 249 … … 300 302 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 301 303 302 In most cases a copy-and-paste of an interactive console session works fine, but 303 doctest isn't trying to do an exact emulation of any specific Python shell. All 304 hard tab characters are expanded to spaces, using 8-column tab stops. If you 305 don't believe tabs should mean that, too bad: don't use hard tabs, or write 306 your own :class:`DocTestParser` class. 307 308 .. versionchanged:: 2.4 309 Expanding tabs to spaces is new; previous versions tried to preserve hard tabs, 310 with confusing results. 304 In most cases a copy-and-paste of an interactive console session works fine, 305 but doctest isn't trying to do an exact emulation of any specific Python shell. 311 306 312 307 :: … … 339 334 is expected. 340 335 341 .. version changed:: 2.4336 .. versionadded:: 2.4 342 337 ``<BLANKLINE>`` was added; there was no way to use expected output containing 343 338 empty lines in previous versions. 339 340 * All hard tab characters are expanded to spaces, using 8-column tab stops. 341 Tabs in output generated by the tested code are not modified. Because any 342 hard tabs in the sample output *are* expanded, this means that if the code 343 output includes hard tabs, the only way the doctest can pass is if the 344 :const:`NORMALIZE_WHITESPACE` option or :ref:`directive <doctest-directives>` 345 is in effect. 346 Alternatively, the test can be rewritten to capture the output and compare it 347 to an expected value as part of the test. This handling of tabs in the 348 source was arrived at through trial and error, and has proven to be the least 349 error prone way of handling them. It is possible to use a different 350 algorithm for handling tabs by writing a custom :class:`DocTestParser` class. 351 352 .. versionchanged:: 2.4 353 Expanding tabs to spaces is new; previous versions tried to preserve hard tabs, 354 with confusing results. 344 355 345 356 * Output to stdout is captured, but not output to stderr (exception tracebacks … … 356 367 357 368 Otherwise, the backslash will be interpreted as part of the string. For example, 358 the "\\"above would be interpreted as a newline character. Alternatively, you369 the ``\n`` above would be interpreted as a newline character. Alternatively, you 359 370 can double each backslash in the doctest version (and not use a raw string):: 360 371 … … 439 450 exception's type and detail, and the rest are ignored. 440 451 452 .. versionchanged:: 2.4 453 Previous versions were unable to handle multi-line exception details. 454 441 455 Best practice is to omit the traceback stack, unless it adds significant 442 456 documentation value to the example. So the last example is probably better as:: … … 470 484 course this does the right thing for genuine tracebacks. 471 485 472 * When the :const:`IGNORE_EXCEPTION_DETAIL` doctest option is is specified, 473 everything following the leftmost colon is ignored. 486 * When the :const:`IGNORE_EXCEPTION_DETAIL` doctest option is specified, 487 everything following the leftmost colon and any module information in the 488 exception name is ignored. 474 489 475 490 * The interactive shell omits the traceback header line for some … … 499 514 SyntaxError: invalid syntax 500 515 501 .. versionchanged:: 2.4 502 The ability to handle a multi-line exception detail, and the 503 :const:`IGNORE_EXCEPTION_DETAIL` doctest option, were added. 504 505 516 517 .. _option-flags-and-directives: 506 518 .. _doctest-options: 507 519 508 Option Flags and Directives509 ^^^^^^^^^^^^ ^^^^^^^^^^^^^^^520 Option Flags 521 ^^^^^^^^^^^^ 510 522 511 523 A number of option flags control various aspects of doctest's behavior. 512 524 Symbolic names for the flags are supplied as module constants, which can be 513 525 or'ed together and passed to various functions. The names can also be used in 514 doctest directives (see below).526 :ref:`doctest directives <doctest-directives>`. 515 527 516 528 The first group of options define test semantics, controlling aspects of how … … 565 577 :exc:`TypeError` is raised. 566 578 567 Note that a similar effect can be obtained using :const:`ELLIPSIS`, and 568 :const:`IGNORE_EXCEPTION_DETAIL` may go away when Python releases prior to 2.4 569 become uninteresting. Until then, :const:`IGNORE_EXCEPTION_DETAIL` is the only 570 clear way to write a doctest that doesn't care about the exception detail yet 571 continues to pass under Python releases prior to 2.4 (doctest directives appear 572 to be comments to them). For example, :: 573 574 >>> (1, 2)[3] = 'moo' #doctest: +IGNORE_EXCEPTION_DETAIL 579 It will also ignore the module name used in Python 3 doctest reports. Hence 580 both of these variations will work with the flag specified, regardless of 581 whether the test is run under Python 2.7 or Python 3.2 (or later versions):: 582 583 >>> raise CustomError('message') 584 Traceback (most recent call last): 585 CustomError: message 586 587 >>> raise CustomError('message') 588 Traceback (most recent call last): 589 my_module.CustomError: message 590 591 Note that :const:`ELLIPSIS` can also be used to ignore the 592 details of the exception message, but such a test may still fail based 593 on whether or not the module details are printed as part of the 594 exception name. Using :const:`IGNORE_EXCEPTION_DETAIL` and the details 595 from Python 2.3 is also the only clear way to write a doctest that doesn't 596 care about the exception detail yet continues to pass under Python 2.3 or 597 earlier (those releases do not support :ref:`doctest directives 598 <doctest-directives>` and ignore them as irrelevant comments). For example:: 599 600 >>> (1, 2)[3] = 'moo' 575 601 Traceback (most recent call last): 576 602 File "<stdin>", line 1, in ? 577 603 TypeError: object doesn't support item assignment 578 604 579 passes under Python 2.4 and Python 2.3. The detail changed in 2.4, to say "does 580 not" instead of "doesn't". 605 passes under Python 2.3 and later Python versions with the flag specified, 606 even though the detail 607 changed in Python 2.4 to say "does not" instead of "doesn't". 608 609 .. versionchanged:: 2.7 610 :const:`IGNORE_EXCEPTION_DETAIL` now also ignores any information 611 relating to the module containing the exception under test 581 612 582 613 … … 590 621 591 622 The SKIP flag can also be used for temporarily "commenting out" examples. 623 624 .. versionadded:: 2.5 592 625 593 626 … … 635 668 A bitmask or'ing together all the reporting flags above. 636 669 637 "Doctest directives" may be used to modify the option flags for individual 638 examples. Doctest directives are expressed as a special Python comment 639 following an example's source code: 670 671 .. versionadded:: 2.4 672 The constants 673 :const:`DONT_ACCEPT_BLANKLINE`, :const:`NORMALIZE_WHITESPACE`, 674 :const:`ELLIPSIS`, :const:`IGNORE_EXCEPTION_DETAIL`, :const:`REPORT_UDIFF`, 675 :const:`REPORT_CDIFF`, :const:`REPORT_NDIFF`, 676 :const:`REPORT_ONLY_FIRST_FAILURE`, :const:`COMPARISON_FLAGS` and 677 :const:`REPORTING_FLAGS` were added. 678 679 There's also a way to register new option flag names, although this isn't useful 680 unless you intend to extend :mod:`doctest` internals via subclassing: 681 682 683 .. function:: register_optionflag(name) 684 685 Create a new option flag with a given name, and return the new flag's integer 686 value. :func:`register_optionflag` can be used when subclassing 687 :class:`OutputChecker` or :class:`DocTestRunner` to create new options that are 688 supported by your subclasses. :func:`register_optionflag` should always be 689 called using the following idiom:: 690 691 MY_FLAG = register_optionflag('MY_FLAG') 692 693 .. versionadded:: 2.4 694 695 696 .. _doctest-directives: 697 698 Directives 699 ^^^^^^^^^^ 700 701 Doctest directives may be used to modify the :ref:`option flags 702 <doctest-options>` for an individual example. Doctest directives are 703 special Python comments following an example's source code: 640 704 641 705 .. productionlist:: doctest … … 655 719 For example, this test passes:: 656 720 657 >>> print range(20) # doctest: +NORMALIZE_WHITESPACE721 >>> print range(20) # doctest: +NORMALIZE_WHITESPACE 658 722 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 659 723 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] … … 664 728 so:: 665 729 666 >>> print range(20) # doctest: +ELLIPSIS730 >>> print range(20) # doctest: +ELLIPSIS 667 731 [0, 1, ..., 18, 19] 668 732 669 Multiple directives can be used on a single physical line, separated by commas:: 733 Multiple directives can be used on a single physical line, separated by 734 commas:: 670 735 671 736 >>> print range(20) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE … … 693 758 disabling an option via ``-`` in a directive can be useful. 694 759 695 .. versionchanged:: 2.4 696 Constants :const:`DONT_ACCEPT_BLANKLINE`, :const:`NORMALIZE_WHITESPACE`, 697 :const:`ELLIPSIS`, :const:`IGNORE_EXCEPTION_DETAIL`, :const:`REPORT_UDIFF`, 698 :const:`REPORT_CDIFF`, :const:`REPORT_NDIFF`, 699 :const:`REPORT_ONLY_FIRST_FAILURE`, :const:`COMPARISON_FLAGS` and 700 :const:`REPORTING_FLAGS` were added; by default ``<BLANKLINE>`` in expected 701 output matches an empty line in actual output; and doctest directives were 702 added. 703 704 .. versionchanged:: 2.5 705 Constant :const:`SKIP` was added. 706 707 There's also a way to register new option flag names, although this isn't useful 708 unless you intend to extend :mod:`doctest` internals via subclassing: 709 710 711 .. function:: register_optionflag(name) 712 713 Create a new option flag with a given name, and return the new flag's integer 714 value. :func:`register_optionflag` can be used when subclassing 715 :class:`OutputChecker` or :class:`DocTestRunner` to create new options that are 716 supported by your subclasses. :func:`register_optionflag` should always be 717 called using the following idiom:: 718 719 MY_FLAG = register_optionflag('MY_FLAG') 720 721 .. versionadded:: 2.4 760 .. versionadded:: 2.4 761 Support for doctest directives was added. 722 762 723 763 … … 949 989 Python 2.4, :mod:`doctest`'s :class:`Tester` class is deprecated, and 950 990 :mod:`doctest` provides two functions that can be used to create :mod:`unittest` 951 test suites from modules and text files containing doctests. These test suites 952 can then be run using :mod:`unittest` test runners:: 991 test suites from modules and text files containing doctests. To integrate with 992 :mod:`unittest` test discovery, include a :func:`load_tests` function in your 993 test module:: 953 994 954 995 import unittest 955 996 import doctest 956 import my_module_with_doctests, and_another 957 958 suite = unittest.TestSuite() 959 for mod in my_module_with_doctests, and_another: 960 suite.addTest(doctest.DocTestSuite(mod)) 961 runner = unittest.TextTestRunner() 962 runner.run(suite) 997 import my_module_with_doctests 998 999 def load_tests(loader, tests, ignore): 1000 tests.addTests(doctest.DocTestSuite(my_module_with_doctests)) 1001 return tests 963 1002 964 1003 There are two main functions for creating :class:`unittest.TestSuite` instances … … 1037 1076 .. versionchanged:: 2.5 1038 1077 The parameter *encoding* was added. 1078 1079 .. note:: 1080 Unlike :func:`testmod` and :class:`DocTestFinder`, this function raises 1081 a :exc:`ValueError` if *module* contains no docstrings. You can prevent 1082 this error by passing a :class:`DocTestFinder` instance as the 1083 *test_finder* argument with its *exclude_empty* keyword argument set 1084 to ``False``:: 1085 1086 >>> finder = doctest.DocTestFinder(exclude_empty=False) 1087 >>> suite = doctest.DocTestSuite(test_finder=finder) 1039 1088 1040 1089 … … 1177 1226 1178 1227 A collection of doctest examples that should be run in a single namespace. The 1179 constructor arguments are used to initialize the member variables of the same 1180 names. 1228 constructor arguments are used to initialize the attributes of the same names. 1181 1229 1182 1230 .. versionadded:: 2.4 1183 1231 1184 :class:`DocTest` defines the following member variables. They are initialized by1232 :class:`DocTest` defines the following attributes. They are initialized by 1185 1233 the constructor, and should not be modified directly. 1186 1234 … … 1235 1283 1236 1284 A single interactive example, consisting of a Python statement and its expected 1237 output. The constructor arguments are used to initialize the member variables1238 of thesame names.1285 output. The constructor arguments are used to initialize the attributes of the 1286 same names. 1239 1287 1240 1288 .. versionadded:: 2.4 1241 1289 1242 :class:`Example` defines the following member variables. They are initialized by1290 :class:`Example` defines the following attributes. They are initialized by 1243 1291 the constructor, and should not be modified directly. 1244 1292 … … 1431 1479 example, as it is run. If *verbose* is ``False``, then only failures are 1432 1480 printed. If *verbose* is unspecified, or ``None``, then verbose output is used 1433 iff the command-line switch :option:`-v` is used.1481 iff the command-line switch ``-v`` is used. 1434 1482 1435 1483 The optional keyword argument *optionflags* can be used to control how the test … … 1746 1794 .. exception:: DocTestFailure(test, example, got) 1747 1795 1748 An exception thrownby :class:`DocTestRunner` to signal that a doctest example's1796 An exception raised by :class:`DocTestRunner` to signal that a doctest example's 1749 1797 actual output did not match its expected output. The constructor arguments are 1750 used to initialize the member variables of the same names.1751 1752 :exc:`DocTestFailure` defines the following member variables:1798 used to initialize the attributes of the same names. 1799 1800 :exc:`DocTestFailure` defines the following attributes: 1753 1801 1754 1802 … … 1770 1818 .. exception:: UnexpectedException(test, example, exc_info) 1771 1819 1772 An exception thrown by :class:`DocTestRunner` to signal that a doctest example1773 raised an unexpected exception. The constructor arguments are used to1774 initialize the member variables of the same names.1775 1776 :exc:`UnexpectedException` defines the following member variables:1820 An exception raised by :class:`DocTestRunner` to signal that a doctest 1821 example raised an unexpected exception. The constructor arguments are used 1822 to initialize the attributes of the same names. 1823 1824 :exc:`UnexpectedException` defines the following attributes: 1777 1825 1778 1826 … … 1857 1905 Trying to guess where one ends and the other begins is too error-prone, and that 1858 1906 also makes for a confusing test. 1859
Note:
See TracChangeset
for help on using the changeset viewer.