Changeset 391 for python/trunk/Lib/test/test_doctest.py
- 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/Lib/test/test_doctest.py
r2 r391 4 4 """ 5 5 6 import sys 6 7 from test import test_support 7 8 import doctest 8 import warnings9 9 10 10 # NOTE: There are some additional tests relating to interaction with … … 259 259 >>> e.exc_msg 260 260 '\n' 261 262 Compare `Example`: 263 >>> example = doctest.Example('print 1', '1\n') 264 >>> same_example = doctest.Example('print 1', '1\n') 265 >>> other_example = doctest.Example('print 42', '42\n') 266 >>> example == same_example 267 True 268 >>> example != same_example 269 False 270 >>> hash(example) == hash(same_example) 271 True 272 >>> example == other_example 273 False 274 >>> example != other_example 275 True 261 276 """ 262 277 … … 348 363 ValueError: line 2 of the docstring for some_test lacks blank after ...: '...print 1' 349 364 365 Compare `DocTest`: 366 367 >>> docstring = ''' 368 ... >>> print 12 369 ... 12 370 ... ''' 371 >>> test = parser.get_doctest(docstring, globs, 'some_test', 372 ... 'some_test', 20) 373 >>> same_test = parser.get_doctest(docstring, globs, 'some_test', 374 ... 'some_test', 20) 375 >>> test == same_test 376 True 377 >>> test != same_test 378 False 379 >>> hash(test) == hash(same_test) 380 True 381 >>> docstring = ''' 382 ... >>> print 42 383 ... 42 384 ... ''' 385 >>> other_test = parser.get_doctest(docstring, globs, 'other_test', 386 ... 'other_file', 10) 387 >>> test == other_test 388 False 389 >>> test != other_test 390 True 391 392 Compare `DocTestCase`: 393 394 >>> DocTestCase = doctest.DocTestCase 395 >>> test_case = DocTestCase(test) 396 >>> same_test_case = DocTestCase(same_test) 397 >>> other_test_case = DocTestCase(other_test) 398 >>> test_case == same_test_case 399 True 400 >>> test_case != same_test_case 401 False 402 >>> hash(test_case) == hash(same_test_case) 403 True 404 >>> test == other_test_case 405 False 406 >>> test != other_test_case 407 True 408 350 409 """ 351 410 … … 500 559 501 560 >>> from test import doctest_aliases 561 >>> assert doctest_aliases.TwoNames.f 562 >>> assert doctest_aliases.TwoNames.g 502 563 >>> tests = excl_empty_finder.find(doctest_aliases) 503 564 >>> print len(tests) … … 864 925 TestResults(failed=0, attempted=1) 865 926 927 IGNORE_EXCEPTION_DETAIL also ignores difference in exception formatting 928 between Python versions. For example, in Python 3.x, the module path of 929 the exception is in the output, but this will fail under Python 2: 930 931 >>> def f(x): 932 ... r''' 933 ... >>> from httplib import HTTPException 934 ... >>> raise HTTPException('message') 935 ... Traceback (most recent call last): 936 ... httplib.HTTPException: message 937 ... ''' 938 >>> test = doctest.DocTestFinder().find(f)[0] 939 >>> doctest.DocTestRunner(verbose=False).run(test) 940 ... # doctest: +ELLIPSIS 941 ********************************************************************** 942 File ..., line 4, in f 943 Failed example: 944 raise HTTPException('message') 945 Expected: 946 Traceback (most recent call last): 947 httplib.HTTPException: message 948 Got: 949 Traceback (most recent call last): 950 ... 951 HTTPException: message 952 TestResults(failed=1, attempted=2) 953 954 But in Python 2 the module path is not included, an therefore a test must look 955 like the following test to succeed in Python 2. But that test will fail under 956 Python 3. 957 958 >>> def f(x): 959 ... r''' 960 ... >>> from httplib import HTTPException 961 ... >>> raise HTTPException('message') 962 ... Traceback (most recent call last): 963 ... HTTPException: message 964 ... ''' 965 >>> test = doctest.DocTestFinder().find(f)[0] 966 >>> doctest.DocTestRunner(verbose=False).run(test) 967 TestResults(failed=0, attempted=2) 968 969 However, with IGNORE_EXCEPTION_DETAIL, the module name of the exception 970 (if any) will be ignored: 971 972 >>> def f(x): 973 ... r''' 974 ... >>> from httplib import HTTPException 975 ... >>> raise HTTPException('message') #doctest: +IGNORE_EXCEPTION_DETAIL 976 ... Traceback (most recent call last): 977 ... HTTPException: message 978 ... ''' 979 >>> test = doctest.DocTestFinder().find(f)[0] 980 >>> doctest.DocTestRunner(verbose=False).run(test) 981 TestResults(failed=0, attempted=2) 982 983 The module path will be completely ignored, so two different module paths will 984 still pass if IGNORE_EXCEPTION_DETAIL is given. This is intentional, so it can 985 be used when exceptions have changed module. 986 987 >>> def f(x): 988 ... r''' 989 ... >>> from httplib import HTTPException 990 ... >>> raise HTTPException('message') #doctest: +IGNORE_EXCEPTION_DETAIL 991 ... Traceback (most recent call last): 992 ... foo.bar.HTTPException: message 993 ... ''' 994 >>> test = doctest.DocTestFinder().find(f)[0] 995 >>> doctest.DocTestRunner(verbose=False).run(test) 996 TestResults(failed=0, attempted=2) 997 866 998 But IGNORE_EXCEPTION_DETAIL does not allow a mismatch in the exception type: 867 999 … … 908 1040 ZeroDivisionError: integer division or modulo by zero 909 1041 TestResults(failed=1, attempted=1) 1042 """ 1043 def displayhook(): r""" 1044 Test that changing sys.displayhook doesn't matter for doctest. 1045 1046 >>> import sys 1047 >>> orig_displayhook = sys.displayhook 1048 >>> def my_displayhook(x): 1049 ... print('hi!') 1050 >>> sys.displayhook = my_displayhook 1051 >>> def f(): 1052 ... ''' 1053 ... >>> 3 1054 ... 3 1055 ... ''' 1056 >>> test = doctest.DocTestFinder().find(f)[0] 1057 >>> r = doctest.DocTestRunner(verbose=False).run(test) 1058 >>> post_displayhook = sys.displayhook 1059 1060 We need to restore sys.displayhook now, so that we'll be able to test 1061 results. 1062 1063 >>> sys.displayhook = orig_displayhook 1064 1065 Ok, now we can check that everything is ok. 1066 1067 >>> r 1068 TestResults(failed=0, attempted=1) 1069 >>> post_displayhook is my_displayhook 1070 True 910 1071 """ 911 1072 def optionflags(): r""" … … 1191 1352 TestResults(failed=1, attempted=1) 1192 1353 1193 The REPORT_ONLY_FIRST_FAILURE sup resses result output after the first1354 The REPORT_ONLY_FIRST_FAILURE suppresses result output after the first 1194 1355 failing example: 1195 1356 … … 1221 1382 TestResults(failed=3, attempted=5) 1222 1383 1223 However, output from `report_start` is not sup ressed:1384 However, output from `report_start` is not suppressed: 1224 1385 1225 1386 >>> doctest.DocTestRunner(verbose=True, optionflags=flags).run(test) … … 1509 1670 Traceback (most recent call last): 1510 1671 ValueError: line 0 of the doctest for s has an option directive on a line with no example: '# doctest: +ELLIPSIS' 1511 """ 1672 1673 """ 1674 1675 def test_unicode_output(self): r""" 1676 1677 Check that unicode output works: 1678 1679 >>> u'\xe9' 1680 u'\xe9' 1681 1682 If we return unicode, SpoofOut's buf variable becomes automagically 1683 converted to unicode. This means all subsequent output becomes converted 1684 to unicode, and if the output contains non-ascii characters that failed. 1685 It used to be that this state change carried on between tests, meaning 1686 tests would fail if unicode has been output previously in the testrun. 1687 This test tests that this is no longer so: 1688 1689 >>> print u'abc' 1690 abc 1691 1692 And then return a string with non-ascii characters: 1693 1694 >>> print u'\xe9'.encode('utf-8') 1695 é 1696 1697 """ 1698 1512 1699 1513 1700 def test_testsource(): r""" … … 1594 1781 >>> doc = ''' 1595 1782 ... >>> x = 42 1783 ... >>> raise Exception('clé') 1784 ... Traceback (most recent call last): 1785 ... Exception: clé 1596 1786 ... >>> import pdb; pdb.set_trace() 1597 1787 ... ''' 1598 1788 >>> parser = doctest.DocTestParser() 1599 >>> test = parser.get_doctest(doc, {}, "foo ", "foo.py", 0)1789 >>> test = parser.get_doctest(doc, {}, "foo-bÀr@baz", "foo-bÀr@baz.py", 0) 1600 1790 >>> runner = doctest.DocTestRunner(verbose=False) 1601 1791 … … 1613 1803 ... finally: sys.stdin = real_stdin 1614 1804 --Return-- 1615 > <doctest foo [1]>(1)<module>()->None1805 > <doctest foo-bÀr@baz[2]>(1)<module>()->None 1616 1806 -> import pdb; pdb.set_trace() 1617 1807 (Pdb) print x 1618 1808 42 1619 1809 (Pdb) continue 1620 TestResults(failed=0, attempted= 2)1810 TestResults(failed=0, attempted=3) 1621 1811 1622 1812 You can also put pdb.set_trace in a function called from a test: … … 1630 1820 ... >>> calls_set_trace() 1631 1821 ... ''' 1632 >>> test = parser.get_doctest(doc, globals(), "foo ", "foo.py", 0)1822 >>> test = parser.get_doctest(doc, globals(), "foo-bÀr@baz", "foo-bÀr@baz.py", 0) 1633 1823 >>> real_stdin = sys.stdin 1634 1824 >>> sys.stdin = _FakeInput([ … … 1649 1839 2 1650 1840 (Pdb) up 1651 > <doctest foo [1]>(1)<module>()1841 > <doctest foo-bÀr@baz[1]>(1)<module>() 1652 1842 -> calls_set_trace() 1653 1843 (Pdb) print x … … 1667 1857 ... >>> f(3) 1668 1858 ... ''' 1669 >>> test = parser.get_doctest(doc, globals(), "foo ", "foo.py", 0)1859 >>> test = parser.get_doctest(doc, globals(), "foo-bÀr@baz", "foo-bÀr@baz.py", 0) 1670 1860 >>> real_stdin = sys.stdin 1671 1861 >>> sys.stdin = _FakeInput([ … … 1681 1871 ... # doctest: +NORMALIZE_WHITESPACE 1682 1872 --Return-- 1683 > <doctest foo [1]>(3)g()->None1873 > <doctest foo-bÀr@baz[1]>(3)g()->None 1684 1874 -> import pdb; pdb.set_trace() 1685 1875 (Pdb) list … … 1690 1880 (Pdb) next 1691 1881 --Return-- 1692 > <doctest foo [0]>(2)f()->None1882 > <doctest foo-bÀr@baz[0]>(2)f()->None 1693 1883 -> g(x*2) 1694 1884 (Pdb) list … … 1698 1888 (Pdb) next 1699 1889 --Return-- 1700 > <doctest foo [2]>(1)<module>()->None1890 > <doctest foo-bÀr@baz[2]>(1)<module>()->None 1701 1891 -> f(3) 1702 1892 (Pdb) list … … 1705 1895 (Pdb) continue 1706 1896 ********************************************************************** 1707 File "foo .py", line 7, in foo1897 File "foo-bÀr@baz.py", line 7, in foo-bÀr@baz 1708 1898 Failed example: 1709 1899 f(3) … … 1739 1929 >>> parser = doctest.DocTestParser() 1740 1930 >>> runner = doctest.DocTestRunner(verbose=False) 1741 >>> test = parser.get_doctest(doc, globals(), "foo ", "foo.py", 0)1931 >>> test = parser.get_doctest(doc, globals(), "foo-bÀr@baz", "foo-bÀr@baz.py", 0) 1742 1932 >>> real_stdin = sys.stdin 1743 1933 >>> sys.stdin = _FakeInput([ … … 1791 1981 1 1792 1982 (Pdb) up 1793 > <doctest foo [1]>(1)<module>()1983 > <doctest foo-bÀr@baz[1]>(1)<module>() 1794 1984 -> calls_set_trace() 1795 1985 (Pdb) print foo … … 1809 1999 >>> suite = doctest.DocTestSuite(test.sample_doctest) 1810 2000 >>> suite.run(unittest.TestResult()) 1811 <unittest. TestResult run=9 errors=0 failures=4>2001 <unittest.result.TestResult run=9 errors=0 failures=4> 1812 2002 1813 2003 We can also supply the module by name: … … 1815 2005 >>> suite = doctest.DocTestSuite('test.sample_doctest') 1816 2006 >>> suite.run(unittest.TestResult()) 1817 <unittest.TestResult run=9 errors=0 failures=4> 2007 <unittest.result.TestResult run=9 errors=0 failures=4> 2008 2009 The module need not contain any doctest examples: 2010 2011 >>> suite = doctest.DocTestSuite('test.sample_doctest_no_doctests') 2012 >>> suite.run(unittest.TestResult()) 2013 <unittest.result.TestResult run=0 errors=0 failures=0> 2014 2015 However, if DocTestSuite finds no docstrings, it raises an error: 2016 2017 >>> try: 2018 ... doctest.DocTestSuite('test.sample_doctest_no_docstrings') 2019 ... except ValueError as e: 2020 ... error = e 2021 2022 >>> print(error.args[1]) 2023 has no docstrings 2024 2025 You can prevent this error by passing a DocTestFinder instance with 2026 the `exclude_empty` keyword argument set to False: 2027 2028 >>> finder = doctest.DocTestFinder(exclude_empty=False) 2029 >>> suite = doctest.DocTestSuite('test.sample_doctest_no_docstrings', 2030 ... test_finder=finder) 2031 >>> suite.run(unittest.TestResult()) 2032 <unittest.result.TestResult run=0 errors=0 failures=0> 1818 2033 1819 2034 We can use the current module: … … 1821 2036 >>> suite = test.sample_doctest.test_suite() 1822 2037 >>> suite.run(unittest.TestResult()) 1823 <unittest. TestResult run=9 errors=0 failures=4>2038 <unittest.result.TestResult run=9 errors=0 failures=4> 1824 2039 1825 2040 We can supply global variables. If we pass globs, they will be … … 1829 2044 >>> suite = doctest.DocTestSuite('test.sample_doctest', globs={}) 1830 2045 >>> suite.run(unittest.TestResult()) 1831 <unittest. TestResult run=9 errors=0 failures=5>2046 <unittest.result.TestResult run=9 errors=0 failures=5> 1832 2047 1833 2048 Alternatively, we can provide extra globals. Here we'll make an … … 1837 2052 ... extraglobs={'y': 1}) 1838 2053 >>> suite.run(unittest.TestResult()) 1839 <unittest. TestResult run=9 errors=0 failures=3>2054 <unittest.result.TestResult run=9 errors=0 failures=3> 1840 2055 1841 2056 You can pass option flags. Here we'll cause an extra error … … 1845 2060 ... optionflags=doctest.DONT_ACCEPT_BLANKLINE) 1846 2061 >>> suite.run(unittest.TestResult()) 1847 <unittest. TestResult run=9 errors=0 failures=5>2062 <unittest.result.TestResult run=9 errors=0 failures=5> 1848 2063 1849 2064 You can supply setUp and tearDown functions: … … 1862 2077 ... setUp=setUp, tearDown=tearDown) 1863 2078 >>> suite.run(unittest.TestResult()) 1864 <unittest. TestResult run=9 errors=0 failures=3>2079 <unittest.result.TestResult run=9 errors=0 failures=3> 1865 2080 1866 2081 But the tearDown restores sanity: … … 1880 2095 >>> suite = doctest.DocTestSuite('test.sample_doctest', setUp=setUp) 1881 2096 >>> suite.run(unittest.TestResult()) 1882 <unittest. TestResult run=9 errors=0 failures=3>2097 <unittest.result.TestResult run=9 errors=0 failures=3> 1883 2098 1884 2099 Here, we didn't need to use a tearDown function because we … … 1899 2114 ... 'test_doctest4.txt') 1900 2115 >>> suite.run(unittest.TestResult()) 1901 <unittest. TestResult run=3 errors=0 failures=3>2116 <unittest.result.TestResult run=3 errors=0 failures=3> 1902 2117 1903 2118 The test files are looked for in the directory containing the … … 1911 2126 ... package='test') 1912 2127 >>> suite.run(unittest.TestResult()) 1913 <unittest. TestResult run=3 errors=0 failures=3>2128 <unittest.result.TestResult run=3 errors=0 failures=3> 1914 2129 1915 2130 Support for using a package's __loader__.get_data() is also … … 1930 2145 ... if added_loader: 1931 2146 ... del test.__loader__ 1932 <unittest. TestResult run=3 errors=0 failures=3>2147 <unittest.result.TestResult run=3 errors=0 failures=3> 1933 2148 1934 2149 '/' should be used as a path separator. It will be converted … … 1937 2152 >>> suite = doctest.DocFileSuite('../test/test_doctest.txt') 1938 2153 >>> suite.run(unittest.TestResult()) 1939 <unittest. TestResult run=1 errors=0 failures=1>2154 <unittest.result.TestResult run=1 errors=0 failures=1> 1940 2155 1941 2156 If DocFileSuite is used from an interactive session, then files … … 1962 2177 >>> suite = doctest.DocFileSuite(test_file, module_relative=False) 1963 2178 >>> suite.run(unittest.TestResult()) 1964 <unittest. TestResult run=1 errors=0 failures=1>2179 <unittest.result.TestResult run=1 errors=0 failures=1> 1965 2180 1966 2181 It is an error to specify `package` when `module_relative=False`: … … 1978 2193 ... globs={'favorite_color': 'blue'}) 1979 2194 >>> suite.run(unittest.TestResult()) 1980 <unittest. TestResult run=3 errors=0 failures=2>2195 <unittest.result.TestResult run=3 errors=0 failures=2> 1981 2196 1982 2197 In this case, we supplied a missing favorite color. You can … … 1989 2204 ... globs={'favorite_color': 'blue'}) 1990 2205 >>> suite.run(unittest.TestResult()) 1991 <unittest. TestResult run=3 errors=0 failures=3>2206 <unittest.result.TestResult run=3 errors=0 failures=3> 1992 2207 1993 2208 And, you can provide setUp and tearDown functions: … … 2008 2223 ... setUp=setUp, tearDown=tearDown) 2009 2224 >>> suite.run(unittest.TestResult()) 2010 <unittest. TestResult run=3 errors=0 failures=2>2225 <unittest.result.TestResult run=3 errors=0 failures=2> 2011 2226 2012 2227 But the tearDown restores sanity: … … 2027 2242 >>> suite = doctest.DocFileSuite('test_doctest.txt', setUp=setUp) 2028 2243 >>> suite.run(unittest.TestResult()) 2029 <unittest. TestResult run=1 errors=0 failures=0>2244 <unittest.result.TestResult run=1 errors=0 failures=0> 2030 2245 2031 2246 Here, we didn't need to use a tearDown function because we … … 2039 2254 >>> suite = doctest.DocFileSuite('test_doctest3.txt') 2040 2255 >>> suite.run(unittest.TestResult()) 2041 <unittest. TestResult run=1 errors=0 failures=0>2256 <unittest.result.TestResult run=1 errors=0 failures=0> 2042 2257 2043 2258 If the tests contain non-ASCII characters, we have to specify which … … 2050 2265 ... encoding='utf-8') 2051 2266 >>> suite.run(unittest.TestResult()) 2052 <unittest. TestResult run=3 errors=0 failures=2>2267 <unittest.result.TestResult run=3 errors=0 failures=2> 2053 2268 2054 2269 """ … … 2168 2383 2169 2384 (Note: we'll be clearing doctest.master after each call to 2170 `doctest.testfile`, to sup ress warnings about multiple tests with the2385 `doctest.testfile`, to suppress warnings about multiple tests with the 2171 2386 same name.) 2172 2387 … … 2204 2419 >>> doctest.master = None # Reset master. 2205 2420 2206 Verbosity can be increased with the optional `verbose` par emter:2421 Verbosity can be increased with the optional `verbose` parameter: 2207 2422 2208 2423 >>> doctest.testfile('test_doctest.txt', globs=globs, verbose=True) … … 2241 2456 >>> doctest.master = None # Reset master. 2242 2457 2243 The summary report may be sup ressed with the optional `report`2458 The summary report may be suppressed with the optional `report` 2244 2459 parameter: 2245 2460 … … 2333 2548 # that these use the deprecated doctest.Tester, so should go away (or 2334 2549 # be rewritten) someday. 2335 2336 # Ignore all warnings about the use of class Tester in this module.2337 # Note that the name of this module may differ depending on how it's2338 # imported, so the use of __name__ is important.2339 warnings.filterwarnings("ignore", "class Tester", DeprecationWarning,2340 __name__, 0)2341 2550 2342 2551 def old_test1(): r""" … … 2463 2672 # Check the doctest cases in doctest itself: 2464 2673 test_support.run_doctest(doctest, verbosity=True) 2465 # Check the doctest cases defined here: 2674 2466 2675 from test import test_doctest 2467 test_support.run_doctest(test_doctest, verbosity=True) 2468 2469 import trace, sys 2676 2677 # Ignore all warnings about the use of class Tester in this module. 2678 deprecations = [] 2679 if __debug__: 2680 deprecations.append(("class Tester is deprecated", DeprecationWarning)) 2681 if sys.py3kwarning: 2682 deprecations += [("backquote not supported", SyntaxWarning), 2683 ("execfile.. not supported", DeprecationWarning)] 2684 with test_support.check_warnings(*deprecations): 2685 # Check the doctest cases defined here: 2686 test_support.run_doctest(test_doctest, verbosity=True) 2687 2688 import sys 2470 2689 def test_coverage(coverdir): 2690 trace = test_support.import_module('trace') 2471 2691 tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], 2472 2692 trace=0, count=1)
Note:
See TracChangeset
for help on using the changeset viewer.