Skip to content

Commit a856364

Browse files
bpo-45229: Use doctest.DocTestSuite instead of run_doctest (GH-28468)
Alo use load_tests() for adding tests.
1 parent 5e2c32e commit a856364

17 files changed

+122
-188
lines changed

Lib/test/test_cmd.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import cmd
88
import sys
9+
import doctest
910
import unittest
1011
import io
1112
from test import support
@@ -219,10 +220,9 @@ def test_input_reset_at_EOF(self):
219220
"(Cmd) *** Unknown syntax: EOF\n"))
220221

221222

222-
def test_main(verbose=None):
223-
from test import test_cmd
224-
support.run_doctest(test_cmd, verbose)
225-
support.run_unittest(TestAlternateInput)
223+
def load_tests(loader, tests, pattern):
224+
tests.addTest(doctest.DocTestSuite())
225+
return tests
226226

227227
def test_coverage(coverdir):
228228
trace = support.import_module('trace')
@@ -239,4 +239,4 @@ def test_coverage(coverdir):
239239
elif "-i" in sys.argv:
240240
samplecmdclass().cmdloop()
241241
else:
242-
test_main()
242+
unittest.main()

Lib/test/test_code.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
import inspect
129129
import sys
130130
import threading
131+
import doctest
131132
import unittest
132133
import textwrap
133134
import weakref
@@ -136,7 +137,7 @@
136137
import ctypes
137138
except ImportError:
138139
ctypes = None
139-
from test.support import (run_doctest, run_unittest, cpython_only,
140+
from test.support import (cpython_only,
140141
check_impl_detail, requires_debug_ranges,
141142
gc_collect)
142143
from test.support.script_helper import assert_python_ok
@@ -609,13 +610,10 @@ def run(self):
609610
self.assertEqual(LAST_FREED, 500)
610611

611612

612-
def test_main(verbose=None):
613-
from test import test_code
614-
run_doctest(test_code, verbose)
615-
tests = [CodeTest, CodeConstsTest, CodeWeakRefTest]
616-
if check_impl_detail(cpython=True) and ctypes is not None:
617-
tests.append(CoExtra)
618-
run_unittest(*tests)
613+
def load_tests(loader, tests, pattern):
614+
tests.addTest(doctest.DocTestSuite())
615+
return tests
616+
619617

620618
if __name__ == "__main__":
621-
test_main()
619+
unittest.main()

Lib/test/test_collections.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -2351,19 +2351,10 @@ def test_gt(self):
23512351
self.assertFalse(Counter(a=2, b=1, c=0) > Counter('aab'))
23522352

23532353

2354-
################################################################################
2355-
### Run tests
2356-
################################################################################
2357-
2358-
def test_main(verbose=None):
2359-
NamedTupleDocs = doctest.DocTestSuite(module=collections)
2360-
test_classes = [TestNamedTuple, NamedTupleDocs, TestOneTrickPonyABCs,
2361-
TestCollectionABCs, TestCounter, TestChainMap,
2362-
TestUserObjects,
2363-
]
2364-
support.run_unittest(*test_classes)
2365-
support.run_doctest(collections, verbose)
2354+
def load_tests(loader, tests, pattern):
2355+
tests.addTest(doctest.DocTestSuite(collections))
2356+
return tests
23662357

23672358

23682359
if __name__ == "__main__":
2369-
test_main(verbose=True)
2360+
unittest.main()

Lib/test/test_deque.py

+5-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from collections import deque
2+
import doctest
23
import unittest
34
from test import support, seq_tests
45
import gc
@@ -1033,31 +1034,10 @@ def test_free_after_iterating(self):
10331034

10341035
__test__ = {'libreftest' : libreftest}
10351036

1036-
def test_main(verbose=None):
1037-
import sys
1038-
test_classes = (
1039-
TestBasic,
1040-
TestVariousIteratorArgs,
1041-
TestSubclass,
1042-
TestSubclassWithKwargs,
1043-
TestSequence,
1044-
)
1037+
def load_tests(loader, tests, pattern):
1038+
tests.addTest(doctest.DocTestSuite())
1039+
return tests
10451040

1046-
support.run_unittest(*test_classes)
1047-
1048-
# verify reference counting
1049-
if verbose and hasattr(sys, "gettotalrefcount"):
1050-
import gc
1051-
counts = [None] * 5
1052-
for i in range(len(counts)):
1053-
support.run_unittest(*test_classes)
1054-
gc.collect()
1055-
counts[i] = sys.gettotalrefcount()
1056-
print(counts)
1057-
1058-
# doctests
1059-
from test import test_deque
1060-
support.run_doctest(test_deque, verbose)
10611041

10621042
if __name__ == "__main__":
1063-
test_main(verbose=True)
1043+
unittest.main()

Lib/test/test_descrtut.py

+9-15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
from test.support import sortdict
1212
import pprint
13+
import doctest
14+
import unittest
15+
1316

1417
class defaultdict(dict):
1518
def __init__(self, default=None):
@@ -469,19 +472,10 @@ def m(self):
469472
"tut7": test_7,
470473
"tut8": test_8}
471474

472-
# Magic test name that regrtest.py invokes *after* importing this module.
473-
# This worms around a bootstrap problem.
474-
# Note that doctest and regrtest both look in sys.argv for a "-v" argument,
475-
# so this works as expected in both ways of running regrtest.
476-
def test_main(verbose=None):
477-
# Obscure: import this module as test.test_descrtut instead of as
478-
# plain test_descrtut because the name of this module works its way
479-
# into the doctest examples, and unless the full test.test_descrtut
480-
# business is used the name can change depending on how the test is
481-
# invoked.
482-
from test import support, test_descrtut
483-
support.run_doctest(test_descrtut, verbose)
484-
485-
# This part isn't needed for regrtest, but for running the test directly.
475+
def load_tests(loader, tests, pattern):
476+
tests.addTest(doctest.DocTestSuite())
477+
return tests
478+
479+
486480
if __name__ == "__main__":
487-
test_main(1)
481+
unittest.main()

Lib/test/test_extcall.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -520,11 +520,14 @@
520520
521521
"""
522522

523-
import sys
523+
import doctest
524+
import unittest
524525
from test import support
525526

526-
def test_main():
527-
support.run_doctest(sys.modules[__name__], True)
527+
def load_tests(loader, tests, pattern):
528+
tests.addTest(doctest.DocTestSuite())
529+
return tests
530+
528531

529532
if __name__ == '__main__':
530-
test_main()
533+
unittest.main()

Lib/test/test_generators.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import gc
33
import pickle
44
import sys
5+
import doctest
56
import unittest
67
import weakref
78
import inspect
@@ -2371,15 +2372,10 @@ def printsolution(self, x):
23712372
"refleaks": refleaks_tests,
23722373
}
23732374

2374-
# Magic test name that regrtest.py invokes *after* importing this module.
2375-
# This worms around a bootstrap problem.
2376-
# Note that doctest and regrtest both look in sys.argv for a "-v" argument,
2377-
# so this works as expected in both ways of running regrtest.
2378-
def test_main(verbose=None):
2379-
from test import support, test_generators
2380-
support.run_unittest(__name__)
2381-
support.run_doctest(test_generators, verbose)
2375+
def load_tests(loader, tests, pattern):
2376+
tests.addTest(doctest.DocTestSuite())
2377+
return tests
2378+
23822379

2383-
# This part isn't needed for regrtest, but for running the test directly.
23842380
if __name__ == "__main__":
2385-
test_main(1)
2381+
unittest.main()

Lib/test/test_genexps.py

+10-17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import sys
2+
import doctest
3+
import unittest
4+
5+
16
doctests = """
27
38
Test simple loop with conditional
@@ -274,28 +279,16 @@
274279
275280
"""
276281

277-
import sys
278-
279282
# Trace function can throw off the tuple reuse test.
280283
if hasattr(sys, 'gettrace') and sys.gettrace():
281284
__test__ = {}
282285
else:
283286
__test__ = {'doctests' : doctests}
284287

285-
def test_main(verbose=None):
286-
from test import support
287-
from test import test_genexps
288-
support.run_doctest(test_genexps, verbose)
289-
290-
# verify reference counting
291-
if verbose and hasattr(sys, "gettotalrefcount"):
292-
import gc
293-
counts = [None] * 5
294-
for i in range(len(counts)):
295-
support.run_doctest(test_genexps, verbose)
296-
gc.collect()
297-
counts[i] = sys.gettotalrefcount()
298-
print(counts)
288+
def load_tests(loader, tests, pattern):
289+
tests.addTest(doctest.DocTestSuite())
290+
return tests
291+
299292

300293
if __name__ == "__main__":
301-
test_main(verbose=True)
294+
unittest.main()

Lib/test/test_http_cookies.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Simple test suite for http/cookies.py
22

33
import copy
4-
from test.support import run_unittest, run_doctest
54
import unittest
5+
import doctest
66
from http import cookies
77
import pickle
88

@@ -479,9 +479,11 @@ def test_repr(self):
479479
r'Set-Cookie: key=coded_val; '
480480
r'expires=\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+')
481481

482-
def test_main():
483-
run_unittest(CookieTests, MorselTests)
484-
run_doctest(cookies)
482+
483+
def load_tests(loader, tests, pattern):
484+
tests.addTest(doctest.DocTestSuite(cookies))
485+
return tests
486+
485487

486488
if __name__ == '__main__':
487-
test_main()
489+
unittest.main()

Lib/test/test_itertools.py

+6-21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import doctest
12
import unittest
23
from test import support
34
from itertools import *
@@ -2690,26 +2691,10 @@ def test_permutations_sizeof(self):
26902691

26912692
__test__ = {'libreftest' : libreftest}
26922693

2693-
def test_main(verbose=None):
2694-
test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC,
2695-
RegressionTests, LengthTransparency,
2696-
SubclassWithKwargsTest, TestExamples,
2697-
TestPurePythonRoughEquivalents,
2698-
SizeofTest)
2699-
support.run_unittest(*test_classes)
2700-
2701-
# verify reference counting
2702-
if verbose and hasattr(sys, "gettotalrefcount"):
2703-
import gc
2704-
counts = [None] * 5
2705-
for i in range(len(counts)):
2706-
support.run_unittest(*test_classes)
2707-
gc.collect()
2708-
counts[i] = sys.gettotalrefcount()
2709-
print(counts)
2710-
2711-
# doctest the examples in the library reference
2712-
support.run_doctest(sys.modules[__name__], verbose)
2694+
def load_tests(loader, tests, pattern):
2695+
tests.addTest(doctest.DocTestSuite())
2696+
return tests
2697+
27132698

27142699
if __name__ == "__main__":
2715-
test_main(verbose=True)
2700+
unittest.main()

Lib/test/test_listcomps.py

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import doctest
2+
import unittest
3+
4+
15
doctests = """
26
########### Tests borrowed from or inspired by test_genexps.py ############
37
@@ -144,21 +148,10 @@
144148

145149
__test__ = {'doctests' : doctests}
146150

147-
def test_main(verbose=None):
148-
import sys
149-
from test import support
150-
from test import test_listcomps
151-
support.run_doctest(test_listcomps, verbose)
152-
153-
# verify reference counting
154-
if verbose and hasattr(sys, "gettotalrefcount"):
155-
import gc
156-
counts = [None] * 5
157-
for i in range(len(counts)):
158-
support.run_doctest(test_listcomps, verbose)
159-
gc.collect()
160-
counts[i] = sys.gettotalrefcount()
161-
print(counts)
151+
def load_tests(loader, tests, pattern):
152+
tests.addTest(doctest.DocTestSuite())
153+
return tests
154+
162155

163156
if __name__ == "__main__":
164-
test_main(verbose=True)
157+
unittest.main()

Lib/test/test_metaclass.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import doctest
2+
import unittest
3+
4+
15
doctests = """
26
37
Basic class construction.
@@ -256,10 +260,10 @@
256260
else:
257261
__test__ = {'doctests' : doctests}
258262

259-
def test_main(verbose=False):
260-
from test import support
261-
from test import test_metaclass
262-
support.run_doctest(test_metaclass, verbose)
263+
def load_tests(loader, tests, pattern):
264+
tests.addTest(doctest.DocTestSuite())
265+
return tests
266+
263267

264268
if __name__ == "__main__":
265-
test_main(verbose=True)
269+
unittest.main()

0 commit comments

Comments
 (0)