Skip to content

Commit 855a2b6

Browse files
Refactor testing
1 parent e76e809 commit 855a2b6

31 files changed

Lines changed: 154 additions & 236 deletions

‎Doc/library/test.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,14 @@ The :mod:`!test.support` module defines the following functions:
10821082

10831083
.. versionadded:: 3.6
10841084

1085+
1086+
.. function:: check_deprecated_module_attributes(test_case, module, *names, remove)
1087+
1088+
Assert that accessing each attribute in *names* on *module* emits a
1089+
:exc:`DeprecationWarning` naming the attribute and the Python version
1090+
*remove*, given as a ``(major, minor)`` tuple, in which it will be removed.
1091+
1092+
10851093
.. function:: skip_if_broken_multiprocessing_synchronize()
10861094

10871095
Skip tests if the :mod:`multiprocessing.synchronize` module is missing, if

‎Lib/test/support/__init__.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,6 +1943,24 @@ def test__all__(self):
19431943
test_case.assertCountEqual(module.__all__, expected)
19441944

19451945

1946+
def check_deprecated_module_attributes(test_case, module, *names, remove):
1947+
"""Assert that accessing each of 'names' on 'module' emits a
1948+
DeprecationWarning naming the attribute and the Python version 'remove',
1949+
given as a (major, minor) tuple, in which it will be removed.
1950+
"""
1951+
major, minor = remove
1952+
for name in names:
1953+
with test_case.subTest(name=name):
1954+
with test_case.assertWarns(DeprecationWarning) as cm:
1955+
getattr(module, name)
1956+
test_case.assertEqual(
1957+
str(cm.warning),
1958+
f"{name!r} is deprecated and slated for removal "
1959+
f"in Python {major}.{minor}",
1960+
)
1961+
test_case.assertEqual(cm.filename, __file__)
1962+
1963+
19461964
def suppress_msvcrt_asserts(verbose=False):
19471965
try:
19481966
import msvcrt

‎Lib/test/test_argparse.py‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import warnings
1919

2020
from enum import StrEnum
21+
from test import support
2122
from test.support import (
2223
captured_stderr,
2324
force_not_colorized,
@@ -8070,13 +8071,10 @@ def test_fake_color_theme_raises(self):
80708071

80718072

80728073
class TestModule(unittest.TestCase):
8073-
def test_deprecated__version__(self):
8074-
with self.assertWarnsRegex(
8075-
DeprecationWarning,
8076-
"'__version__' is deprecated and slated for removal in Python 3.20",
8077-
) as cm:
8078-
getattr(argparse, "__version__")
8079-
self.assertEqual(cm.filename, __file__)
8074+
def test_deprecated_module_attributes(self):
8075+
support.check_deprecated_module_attributes(
8076+
self, argparse, "__version__", remove=(3, 20),
8077+
)
80808078

80818079

80828080
def tearDownModule():

‎Lib/test/test_bz2.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,13 +1250,10 @@ def test_bzlib_version(self):
12501250

12511251

12521252
class TestModule(unittest.TestCase):
1253-
def test_deprecated__author__(self):
1254-
with self.assertWarnsRegex(
1255-
DeprecationWarning,
1256-
"'__author__' is deprecated and slated for removal in Python 3.20",
1257-
) as cm:
1258-
getattr(bz2, "__author__")
1259-
self.assertEqual(cm.filename, __file__)
1253+
def test_deprecated_module_attributes(self):
1254+
support.check_deprecated_module_attributes(
1255+
self, bz2, "__author__", remove=(3, 20),
1256+
)
12601257

12611258

12621259
def tearDownModule():

‎Lib/test/test_concurrent_futures/test_future.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,12 @@ def get_snapshot():
341341

342342

343343
class TestModule(unittest.TestCase):
344-
def test_deprecated__author__(self):
344+
def test_deprecated_module_attributes(self):
345345
for mod in (futures, futures.process, futures.thread):
346346
with self.subTest(mod=mod.__name__):
347-
with self.assertWarnsRegex(
348-
DeprecationWarning,
349-
"'__author__' is deprecated and slated for removal in Python 3.20",
350-
) as cm:
351-
getattr(mod, "__author__")
352-
self.assertEqual(cm.filename, __file__)
347+
support.check_deprecated_module_attributes(
348+
self, mod, "__author__", remove=(3, 20),
349+
)
353350

354351

355352
def setUpModule():

‎Lib/test/test_csv.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,13 +2095,10 @@ def test_disallow_instantiation(self):
20952095

20962096

20972097
class TestModule(unittest.TestCase):
2098-
def test_deprecated__version__(self):
2099-
with self.assertWarnsRegex(
2100-
DeprecationWarning,
2101-
"'__version__' is deprecated and slated for removal in Python 3.20",
2102-
) as cm:
2103-
getattr(csv, "__version__")
2104-
self.assertEqual(cm.filename, __file__)
2098+
def test_deprecated_module_attributes(self):
2099+
support.check_deprecated_module_attributes(
2100+
self, csv, "__version__", remove=(3, 20),
2101+
)
21052102

21062103

21072104
if __name__ == '__main__':

‎Lib/test/test_ctypes/__init__.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,15 @@
99

1010

1111
class TestModule(unittest.TestCase):
12-
def test_deprecated__version__(self):
12+
def test_deprecated_module_attributes(self):
1313
import ctypes
1414
import _ctypes
1515

1616
for mod in (ctypes, _ctypes):
1717
with self.subTest(mod=mod):
18-
with self.assertWarnsRegex(
19-
DeprecationWarning,
20-
"'__version__' is deprecated and slated for removal in Python 3.20",
21-
) as cm:
22-
getattr(mod, "__version__")
23-
self.assertEqual(cm.filename, __file__)
18+
support.check_deprecated_module_attributes(
19+
self, mod, "__version__", remove=(3, 20),
20+
)
2421

2522

2623
def load_tests(*args):

‎Lib/test/test_ctypes/test_macholib.py‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import os
3131
import sys
3232
import unittest
33+
from test import support
3334

3435
from ctypes.macholib.dyld import dyld_find
3536
from ctypes.macholib.dylib import dylib_info
@@ -109,15 +110,12 @@ def test_framework_info(self):
109110

110111

111112
class TestModule(unittest.TestCase):
112-
def test_deprecated__version__(self):
113+
def test_deprecated_module_attributes(self):
113114
import ctypes.macholib
114115

115-
with self.assertWarnsRegex(
116-
DeprecationWarning,
117-
"'__version__' is deprecated and slated for removal in Python 3.20",
118-
) as cm:
119-
getattr(ctypes.macholib, "__version__")
120-
self.assertEqual(cm.filename, __file__)
116+
support.check_deprecated_module_attributes(
117+
self, ctypes.macholib, "__version__", remove=(3, 20),
118+
)
121119

122120

123121
if __name__ == "__main__":

‎Lib/test/test_decimal.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6032,13 +6032,10 @@ def doit(ty):
60326032

60336033

60346034
class TestModule:
6035-
def test_deprecated__version__(self):
6036-
with self.assertWarnsRegex(
6037-
DeprecationWarning,
6038-
"'__version__' is deprecated and slated for removal in Python 3.20",
6039-
) as cm:
6040-
getattr(self.decimal, "__version__")
6041-
self.assertEqual(cm.filename, __file__)
6035+
def test_deprecated_module_attributes(self):
6036+
support.check_deprecated_module_attributes(
6037+
self, self.decimal, "__version__", remove=(3, 20),
6038+
)
60426039

60436040

60446041
@requires_cdecimal

‎Lib/test/test_httpservers.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,13 +1742,10 @@ def test_https_client(self):
17421742

17431743

17441744
class TestModule(unittest.TestCase):
1745-
def test_deprecated__version__(self):
1746-
with self.assertWarnsRegex(
1747-
DeprecationWarning,
1748-
"'__version__' is deprecated and slated for removal in Python 3.20",
1749-
) as cm:
1750-
getattr(http.server, "__version__")
1751-
self.assertEqual(cm.filename, __file__)
1745+
def test_deprecated_module_attributes(self):
1746+
support.check_deprecated_module_attributes(
1747+
self, http.server, "__version__", remove=(3, 20),
1748+
)
17521749

17531750

17541751
def setUpModule():

0 commit comments

Comments
 (0)