16
16
import tempfile
17
17
import types
18
18
import contextlib
19
+ import traceback
19
20
20
21
21
22
def doctest_skip_if (condition ):
@@ -470,7 +471,7 @@ def basics(): r"""
470
471
>>> tests = finder.find(sample_func)
471
472
472
473
>>> print(tests) # doctest: +ELLIPSIS
473
- [<DocTest sample_func from test_doctest.py:37 (1 example)>]
474
+ [<DocTest sample_func from test_doctest.py:38 (1 example)>]
474
475
475
476
The exact name depends on how test_doctest was invoked, so allow for
476
477
leading path components.
@@ -892,6 +893,9 @@ def basics(): r"""
892
893
DocTestRunner is used to run DocTest test cases, and to accumulate
893
894
statistics. Here's a simple DocTest case we can use:
894
895
896
+ >>> save_colorize = traceback._COLORIZE
897
+ >>> traceback._COLORIZE = False
898
+
895
899
>>> def f(x):
896
900
... '''
897
901
... >>> x = 12
@@ -946,6 +950,8 @@ def basics(): r"""
946
950
6
947
951
ok
948
952
TestResults(failed=1, attempted=3)
953
+
954
+ >>> traceback._COLORIZE = save_colorize
949
955
"""
950
956
def verbose_flag (): r"""
951
957
The `verbose` flag makes the test runner generate more detailed
@@ -1021,6 +1027,9 @@ def exceptions(): r"""
1021
1027
lines between the first line and the type/value may be omitted or
1022
1028
replaced with any other string:
1023
1029
1030
+ >>> save_colorize = traceback._COLORIZE
1031
+ >>> traceback._COLORIZE = False
1032
+
1024
1033
>>> def f(x):
1025
1034
... '''
1026
1035
... >>> x = 12
@@ -1251,6 +1260,8 @@ def exceptions(): r"""
1251
1260
...
1252
1261
ZeroDivisionError: integer division or modulo by zero
1253
1262
TestResults(failed=1, attempted=1)
1263
+
1264
+ >>> traceback._COLORIZE = save_colorize
1254
1265
"""
1255
1266
def displayhook (): r"""
1256
1267
Test that changing sys.displayhook doesn't matter for doctest.
@@ -1292,6 +1303,9 @@ def optionflags(): r"""
1292
1303
The DONT_ACCEPT_TRUE_FOR_1 flag disables matches between True/False
1293
1304
and 1/0:
1294
1305
1306
+ >>> save_colorize = traceback._COLORIZE
1307
+ >>> traceback._COLORIZE = False
1308
+
1295
1309
>>> def f(x):
1296
1310
... '>>> True\n1\n'
1297
1311
@@ -1711,6 +1725,7 @@ def optionflags(): r"""
1711
1725
1712
1726
Clean up.
1713
1727
>>> del doctest.OPTIONFLAGS_BY_NAME[unlikely]
1728
+ >>> traceback._COLORIZE = save_colorize
1714
1729
1715
1730
"""
1716
1731
@@ -1721,6 +1736,9 @@ def option_directives(): r"""
1721
1736
single example. To turn an option on for an example, follow that
1722
1737
example with a comment of the form ``# doctest: +OPTION``:
1723
1738
1739
+ >>> save_colorize = traceback._COLORIZE
1740
+ >>> traceback._COLORIZE = False
1741
+
1724
1742
>>> def f(x): r'''
1725
1743
... >>> print(list(range(10))) # should fail: no ellipsis
1726
1744
... [0, 1, ..., 9]
@@ -1928,6 +1946,8 @@ def option_directives(): r"""
1928
1946
>>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0)
1929
1947
Traceback (most recent call last):
1930
1948
ValueError: line 0 of the doctest for s has an option directive on a line with no example: '# doctest: +ELLIPSIS'
1949
+
1950
+ >>> traceback._COLORIZE = save_colorize
1931
1951
"""
1932
1952
1933
1953
def test_testsource (): r"""
@@ -2011,6 +2031,9 @@ def test_pdb_set_trace():
2011
2031
with a version that restores stdout. This is necessary for you to
2012
2032
see debugger output.
2013
2033
2034
+ >>> save_colorize = traceback._COLORIZE
2035
+ >>> traceback._COLORIZE = False
2036
+
2014
2037
>>> doc = '''
2015
2038
... >>> x = 42
2016
2039
... >>> raise Exception('clé')
@@ -2065,7 +2088,7 @@ def test_pdb_set_trace():
2065
2088
... finally:
2066
2089
... sys.stdin = real_stdin
2067
2090
--Return--
2068
- > <doctest test.test_doctest.test_doctest.test_pdb_set_trace[7 ]>(3)calls_set_trace()->None
2091
+ > <doctest test.test_doctest.test_doctest.test_pdb_set_trace[9 ]>(3)calls_set_trace()->None
2069
2092
-> import pdb; pdb.set_trace()
2070
2093
(Pdb) print(y)
2071
2094
2
@@ -2133,6 +2156,8 @@ def test_pdb_set_trace():
2133
2156
Got:
2134
2157
9
2135
2158
TestResults(failed=1, attempted=3)
2159
+
2160
+ >>> traceback._COLORIZE = save_colorize
2136
2161
"""
2137
2162
2138
2163
def test_pdb_set_trace_nested ():
@@ -2667,7 +2692,10 @@ def test_testfile(): r"""
2667
2692
called with the name of a file, which is taken to be relative to the
2668
2693
calling module. The return value is (#failures, #tests).
2669
2694
2670
- We don't want `-v` in sys.argv for these tests.
2695
+ We don't want color or `-v` in sys.argv for these tests.
2696
+
2697
+ >>> save_colorize = traceback._COLORIZE
2698
+ >>> traceback._COLORIZE = False
2671
2699
2672
2700
>>> save_argv = sys.argv
2673
2701
>>> if '-v' in sys.argv:
@@ -2835,6 +2863,7 @@ def test_testfile(): r"""
2835
2863
TestResults(failed=0, attempted=2)
2836
2864
>>> doctest.master = None # Reset master.
2837
2865
>>> sys.argv = save_argv
2866
+ >>> traceback._COLORIZE = save_colorize
2838
2867
"""
2839
2868
2840
2869
class TestImporter (importlib .abc .MetaPathFinder , importlib .abc .ResourceLoader ):
@@ -2972,6 +3001,9 @@ def test_testmod(): r"""
2972
3001
def test_unicode (): """
2973
3002
Check doctest with a non-ascii filename:
2974
3003
3004
+ >>> save_colorize = traceback._COLORIZE
3005
+ >>> traceback._COLORIZE = False
3006
+
2975
3007
>>> doc = '''
2976
3008
... >>> raise Exception('clé')
2977
3009
... '''
@@ -2997,8 +3029,11 @@ def test_unicode(): """
2997
3029
raise Exception('clé')
2998
3030
Exception: clé
2999
3031
TestResults(failed=1, attempted=1)
3032
+
3033
+ >>> traceback._COLORIZE = save_colorize
3000
3034
"""
3001
3035
3036
+
3002
3037
@doctest_skip_if (not support .has_subprocess_support )
3003
3038
def test_CLI (): r"""
3004
3039
The doctest module can be used to run doctests against an arbitrary file.
@@ -3290,6 +3325,9 @@ def test_run_doctestsuite_multiple_times():
3290
3325
3291
3326
def test_exception_with_note (note ):
3292
3327
"""
3328
+ >>> save_colorize = traceback._COLORIZE
3329
+ >>> traceback._COLORIZE = False
3330
+
3293
3331
>>> test_exception_with_note('Note')
3294
3332
Traceback (most recent call last):
3295
3333
...
@@ -3339,6 +3377,8 @@ def test_exception_with_note(note):
3339
3377
ValueError: message
3340
3378
note
3341
3379
TestResults(failed=1, attempted=...)
3380
+
3381
+ >>> traceback._COLORIZE = save_colorize
3342
3382
"""
3343
3383
exc = ValueError ('Text' )
3344
3384
exc .add_note (note )
@@ -3419,6 +3459,9 @@ def test_syntax_error_subclass_from_stdlib():
3419
3459
3420
3460
def test_syntax_error_with_incorrect_expected_note ():
3421
3461
"""
3462
+ >>> save_colorize = traceback._COLORIZE
3463
+ >>> traceback._COLORIZE = False
3464
+
3422
3465
>>> def f(x):
3423
3466
... r'''
3424
3467
... >>> exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))
@@ -3447,6 +3490,8 @@ def test_syntax_error_with_incorrect_expected_note():
3447
3490
note1
3448
3491
note2
3449
3492
TestResults(failed=1, attempted=...)
3493
+
3494
+ >>> traceback._COLORIZE = save_colorize
3450
3495
"""
3451
3496
3452
3497
0 commit comments