Skip to content

Commit 1416703

Browse files
[3.11] gh-111309: Use unittest to collect and run distutils tests (GH-111311)
* use unittest.main() instead of run_unittest(test_suite()) to run tests from modules via the CLI * add explicit load_tests() to load doctests * use test.support.load_package_tests() to load tests in submodules of distutils.tests * removes no longer needed test_suite() functions
1 parent bb92fda commit 1416703

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+75
-245
lines changed

Lib/distutils/tests/__init__.py

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
"""Test suite for distutils.
22
33
This test suite consists of a collection of test modules in the
4-
distutils.tests package. Each test module has a name starting with
5-
'test' and contains a function test_suite(). The function is expected
6-
to return an initialized unittest.TestSuite instance.
4+
distutils.tests package.
75
86
Tests for the command classes in the distutils.command package are
97
included in distutils.tests as well, instead of using a separate
@@ -13,29 +11,21 @@
1311
"""
1412

1513
import os
16-
import sys
1714
import unittest
18-
from test.support import run_unittest
1915
from test.support.warnings_helper import save_restore_warnings_filters
16+
from test.support import warnings_helper
17+
from test.support import load_package_tests
2018

2119

22-
here = os.path.dirname(__file__) or os.curdir
23-
24-
25-
def test_suite():
26-
suite = unittest.TestSuite()
27-
for fn in os.listdir(here):
28-
if fn.startswith("test") and fn.endswith(".py"):
29-
modname = "distutils.tests." + fn[:-3]
30-
# bpo-40055: Save/restore warnings filters to leave them unchanged.
31-
# Importing tests imports docutils which imports pkg_resources
32-
# which adds a warnings filter.
33-
with save_restore_warnings_filters():
34-
__import__(modname)
35-
module = sys.modules[modname]
36-
suite.addTest(module.test_suite())
37-
return suite
38-
20+
def load_tests(*args):
21+
# bpo-40055: Save/restore warnings filters to leave them unchanged.
22+
# Importing tests imports docutils which imports pkg_resources
23+
# which adds a warnings filter.
24+
with (save_restore_warnings_filters(),
25+
warnings_helper.check_warnings(
26+
("The distutils.sysconfig module is deprecated", DeprecationWarning),
27+
quiet=True)):
28+
return load_package_tests(os.path.dirname(__file__), *args)
3929

4030
if __name__ == "__main__":
41-
run_unittest(test_suite())
31+
unittest.main()

Lib/distutils/tests/test_archive_util.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
ARCHIVE_FORMATS)
1414
from distutils.spawn import find_executable, spawn
1515
from distutils.tests import support
16-
from test.support import run_unittest, patch
16+
from test.support import patch
1717
from test.support.os_helper import change_cwd
1818
from test.support.warnings_helper import check_warnings
1919

@@ -389,8 +389,5 @@ def test_tarfile_root_owner(self):
389389
finally:
390390
archive.close()
391391

392-
def test_suite():
393-
return unittest.TestLoader().loadTestsFromTestCase(ArchiveUtilTestCase)
394-
395392
if __name__ == "__main__":
396-
run_unittest(test_suite())
393+
unittest.main()

Lib/distutils/tests/test_bdist.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Tests for distutils.command.bdist."""
22
import os
33
import unittest
4-
from test.support import run_unittest
54

65
import warnings
76
with warnings.catch_warnings():
@@ -44,9 +43,5 @@ def test_skip_build(self):
4443
'%s should take --skip-build from bdist' % name)
4544

4645

47-
def test_suite():
48-
return unittest.TestLoader().loadTestsFromTestCase(BuildTestCase)
49-
50-
5146
if __name__ == '__main__':
52-
run_unittest(test_suite())
47+
unittest.main()

Lib/distutils/tests/test_bdist_dumb.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import sys
55
import zipfile
66
import unittest
7-
from test.support import run_unittest
87

98
from distutils.core import Distribution
109
from distutils.command.bdist_dumb import bdist_dumb
@@ -90,8 +89,5 @@ def test_simple_built(self):
9089
wanted.append('foo.%s.pyc' % sys.implementation.cache_tag)
9190
self.assertEqual(contents, sorted(wanted))
9291

93-
def test_suite():
94-
return unittest.TestLoader().loadTestsFromTestCase(BuildDumbTestCase)
95-
9692
if __name__ == '__main__':
97-
run_unittest(test_suite())
93+
unittest.main()

Lib/distutils/tests/test_bdist_rpm.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import unittest
44
import sys
55
import os
6-
from test.support import run_unittest, requires_zlib
6+
from test.support import requires_zlib
77

88
from distutils.core import Distribution
99
from distutils.command.bdist_rpm import bdist_rpm
@@ -134,8 +134,5 @@ def test_no_optimize_flag(self):
134134

135135
os.remove(os.path.join(pkg_dir, 'dist', 'foo-0.1-1.noarch.rpm'))
136136

137-
def test_suite():
138-
return unittest.TestLoader().loadTestsFromTestCase(BuildRpmTestCase)
139-
140137
if __name__ == '__main__':
141-
run_unittest(test_suite())
138+
unittest.main()

Lib/distutils/tests/test_build.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import unittest
33
import os
44
import sys
5-
from test.support import run_unittest
65

76
from distutils.command.build import build
87
from distutils.tests import support
@@ -50,8 +49,5 @@ def test_finalize_options(self):
5049
# executable is os.path.normpath(sys.executable)
5150
self.assertEqual(cmd.executable, os.path.normpath(sys.executable))
5251

53-
def test_suite():
54-
return unittest.TestLoader().loadTestsFromTestCase(BuildTestCase)
55-
5652
if __name__ == "__main__":
57-
run_unittest(test_suite())
53+
unittest.main()

Lib/distutils/tests/test_build_clib.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sysconfig
66

77
from test.support import (
8-
run_unittest, missing_compiler_executable, requires_subprocess
8+
missing_compiler_executable, requires_subprocess
99
)
1010

1111
from distutils.command.build_clib import build_clib
@@ -140,8 +140,5 @@ def test_run(self):
140140
# let's check the result
141141
self.assertIn('libfoo.a', os.listdir(build_temp))
142142

143-
def test_suite():
144-
return unittest.TestLoader().loadTestsFromTestCase(BuildCLibTestCase)
145-
146143
if __name__ == "__main__":
147-
run_unittest(test_suite())
144+
unittest.main()

Lib/distutils/tests/test_build_ext.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,5 @@ def build_ext(self, *args, **kwargs):
545545
return build_ext
546546

547547

548-
def test_suite():
549-
suite = unittest.TestSuite()
550-
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(BuildExtTestCase))
551-
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(ParallelBuildExtTestCase))
552-
return suite
553-
554548
if __name__ == '__main__':
555-
support.run_unittest(__name__)
549+
unittest.main()

Lib/distutils/tests/test_build_py.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from distutils.errors import DistutilsFileError
1010

1111
from distutils.tests import support
12-
from test.support import run_unittest, requires_subprocess
12+
from test.support import requires_subprocess
1313

1414

1515
class BuildPyTestCase(support.TempdirManager,
@@ -174,8 +174,5 @@ def test_dont_write_bytecode(self):
174174
self.logs[0][1] % self.logs[0][2])
175175

176176

177-
def test_suite():
178-
return unittest.TestLoader().loadTestsFromTestCase(BuildPyTestCase)
179-
180177
if __name__ == "__main__":
181-
run_unittest(test_suite())
178+
unittest.main()

Lib/distutils/tests/test_build_scripts.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from distutils import sysconfig
99

1010
from distutils.tests import support
11-
from test.support import run_unittest
1211

1312

1413
class BuildScriptsTestCase(support.TempdirManager,
@@ -105,8 +104,5 @@ def test_version_int(self):
105104
for name in expected:
106105
self.assertIn(name, built)
107106

108-
def test_suite():
109-
return unittest.TestLoader().loadTestsFromTestCase(BuildScriptsTestCase)
110-
111107
if __name__ == "__main__":
112-
run_unittest(test_suite())
108+
unittest.main()

Lib/distutils/tests/test_check.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import os
33
import textwrap
44
import unittest
5-
from test.support import run_unittest
65

76
from distutils.command.check import check, HAS_DOCUTILS
87
from distutils.tests import support
@@ -156,8 +155,5 @@ def test_check_all(self):
156155
{}, **{'strict': 1,
157156
'restructuredtext': 1})
158157

159-
def test_suite():
160-
return unittest.TestLoader().loadTestsFromTestCase(CheckTestCase)
161-
162158
if __name__ == "__main__":
163-
run_unittest(test_suite())
159+
unittest.main()

Lib/distutils/tests/test_clean.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from distutils.command.clean import clean
66
from distutils.tests import support
7-
from test.support import run_unittest
87

98
class cleanTestCase(support.TempdirManager,
109
support.LoggingSilencer,
@@ -42,8 +41,5 @@ def test_simple_run(self):
4241
cmd.ensure_finalized()
4342
cmd.run()
4443

45-
def test_suite():
46-
return unittest.TestLoader().loadTestsFromTestCase(cleanTestCase)
47-
4844
if __name__ == "__main__":
49-
run_unittest(test_suite())
45+
unittest.main()

Lib/distutils/tests/test_cmd.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Tests for distutils.cmd."""
22
import unittest
33
import os
4-
from test.support import captured_stdout, run_unittest
4+
from test.support import captured_stdout
55

66
from distutils.cmd import Command
77
from distutils.dist import Distribution
@@ -119,8 +119,5 @@ def test_debug_print(self):
119119
finally:
120120
debug.DEBUG = False
121121

122-
def test_suite():
123-
return unittest.TestLoader().loadTestsFromTestCase(CommandTestCase)
124-
125122
if __name__ == '__main__':
126-
run_unittest(test_suite())
123+
unittest.main()

Lib/distutils/tests/test_config.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from distutils.log import WARN
99

1010
from distutils.tests import support
11-
from test.support import run_unittest
1211

1312
PYPIRC = """\
1413
[distutils]
@@ -134,8 +133,5 @@ def test_config_interpolation(self):
134133
self.assertEqual(config, waited)
135134

136135

137-
def test_suite():
138-
return unittest.TestLoader().loadTestsFromTestCase(PyPIRCCommandTestCase)
139-
140136
if __name__ == "__main__":
141-
run_unittest(test_suite())
137+
unittest.main()

Lib/distutils/tests/test_config_cmd.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
import sysconfig
66
from test.support import (
7-
run_unittest, missing_compiler_executable, requires_subprocess
7+
missing_compiler_executable, requires_subprocess
88
)
99

1010
from distutils.command.config import dump_file, config
@@ -96,8 +96,5 @@ def test_clean(self):
9696
for f in (f1, f2):
9797
self.assertFalse(os.path.exists(f))
9898

99-
def test_suite():
100-
return unittest.TestLoader().loadTestsFromTestCase(ConfigTestCase)
101-
10299
if __name__ == "__main__":
103-
run_unittest(test_suite())
100+
unittest.main()

Lib/distutils/tests/test_core.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
import shutil
77
import sys
8-
from test.support import captured_stdout, run_unittest
8+
from test.support import captured_stdout
99
from test.support import os_helper
1010
import unittest
1111
from distutils.tests import support
@@ -133,8 +133,5 @@ def test_debug_mode(self):
133133
wanted = "options (after parsing config files):\n"
134134
self.assertEqual(stdout.readlines()[0], wanted)
135135

136-
def test_suite():
137-
return unittest.TestLoader().loadTestsFromTestCase(CoreTestCase)
138-
139136
if __name__ == "__main__":
140-
run_unittest(test_suite())
137+
unittest.main()

Lib/distutils/tests/test_cygwinccompiler.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import sys
44
import os
55
from io import BytesIO
6-
from test.support import run_unittest
76

87
from distutils import cygwinccompiler
98
from distutils.cygwinccompiler import (check_config_h,
@@ -147,8 +146,5 @@ def test_get_msvcr(self):
147146
'[MSC v.1999 32 bits (Intel)]')
148147
self.assertRaises(ValueError, get_msvcr)
149148

150-
def test_suite():
151-
return unittest.TestLoader().loadTestsFromTestCase(CygwinCCompilerTestCase)
152-
153149
if __name__ == '__main__':
154-
run_unittest(test_suite())
150+
unittest.main()

Lib/distutils/tests/test_dep_util.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from distutils.dep_util import newer, newer_pairwise, newer_group
66
from distutils.errors import DistutilsFileError
77
from distutils.tests import support
8-
from test.support import run_unittest
98

109
class DepUtilTestCase(support.TempdirManager, unittest.TestCase):
1110

@@ -73,8 +72,5 @@ def test_newer_group(self):
7372
missing='newer'))
7473

7574

76-
def test_suite():
77-
return unittest.TestLoader().loadTestsFromTestCase(DepUtilTestCase)
78-
7975
if __name__ == "__main__":
80-
run_unittest(test_suite())
76+
unittest.main()

Lib/distutils/tests/test_dir_util.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from distutils import log
1313
from distutils.tests import support
14-
from test.support import run_unittest, is_emscripten, is_wasi
14+
from test.support import is_emscripten, is_wasi
1515

1616

1717
class DirUtilTestCase(support.TempdirManager, unittest.TestCase):
@@ -136,8 +136,5 @@ def test_copy_tree_exception_in_listdir(self):
136136
dir_util.copy_tree(src, None)
137137

138138

139-
def test_suite():
140-
return unittest.TestLoader().loadTestsFromTestCase(DirUtilTestCase)
141-
142139
if __name__ == "__main__":
143-
run_unittest(test_suite())
140+
unittest.main()

0 commit comments

Comments
 (0)