From cf122fded55c77bd90b082fd81c7add9cca98006 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 19 Sep 2021 15:27:33 +0300 Subject: [PATCH 1/2] [3.9] bpo-45229: Remove test_main in many tests (GH-28405) Instead of explicitly enumerate test classes for run_unittest() use the unittest ability to discover tests. This also makes these tests discoverable and runnable with unittest. load_tests() can be used for dynamic generating tests and adding doctests. setUpModule(), tearDownModule() and addModuleCleanup() can be used for running code before and after all module tests.. (cherry picked from commit 40348acc180580371d25f75f46b27048e35f2435) Co-authored-by: Serhiy Storchaka --- Lib/lib2to3/tests/data/py2_test_grammar.py | 7 +--- Lib/lib2to3/tests/data/py3_test_grammar.py | 7 +--- Lib/test/support/__init__.py | 10 +---- Lib/test/test_argparse.py | 6 +-- Lib/test/test_bdb.py | 9 +---- Lib/test/test_bigaddrspace.py | 5 +-- Lib/test/test_bigmem.py | 5 +-- Lib/test/test_bool.py | 4 +- Lib/test/test_bz2.py | 12 ++---- Lib/test/test_c_locale_coercion.py | 9 ++--- Lib/test/test_cmd_line.py | 6 +-- Lib/test/test_cmd_line_script.py | 6 +-- Lib/test/test_complex.py | 4 +- Lib/test/test_concurrent_futures.py | 12 ++---- Lib/test/test_descr.py | 14 +++---- Lib/test/test_devpoll.py | 7 +--- Lib/test/test_difflib.py | 16 ++++---- Lib/test/test_distutils.py | 12 +++--- Lib/test/test_dtrace.py | 17 +++------ Lib/test/test_fcntl.py | 6 +-- Lib/test/test_filecmp.py | 5 +-- Lib/test/test_fileio.py | 15 +++----- Lib/test/test_ftplib.py | 15 ++------ Lib/test/test_gc.py | 37 +++++++++---------- Lib/test/test_global.py | 14 ++++--- Lib/test/test_gzip.py | 6 +-- Lib/test/test_httpservers.py | 20 ++-------- Lib/test/test_importlib/test_locks.py | 14 +++---- .../test_importlib/test_threaded_import.py | 18 ++++----- Lib/test/test_inspect.py | 17 +-------- Lib/test/test_iter.py | 8 +--- Lib/test/test_logging.py | 26 +++---------- Lib/test/test_lzma.py | 13 +------ Lib/test/test_mailbox.py | 10 +---- Lib/test/test_multibytecodec.py | 4 +- Lib/test/test_optparse.py | 5 +-- Lib/test/test_ossaudiodev.py | 5 +-- Lib/test/test_pipes.py | 9 +++-- Lib/test/test_pkgutil.py | 8 ++-- Lib/test/test_poll.py | 7 +--- Lib/test/test_poplib.py | 12 ++---- Lib/test/test_posix.py | 16 ++------ Lib/test/test_profile.py | 6 +-- Lib/test/test_pydoc.py | 21 +++-------- Lib/test/test_resource.py | 5 +-- Lib/test/test_sax.py | 18 +-------- Lib/test/test_selectors.py | 27 +++++++------- Lib/test/test_ssl.py | 20 +++------- Lib/test/test_support.py | 17 +-------- Lib/test/test_tcl.py | 5 +-- Lib/test/test_threadsignals.py | 10 ++--- Lib/test/test_timeout.py | 10 ++--- Lib/test/test_tracemalloc.py | 12 +----- Lib/test/test_unicode_file.py | 6 +-- Lib/test/test_unicode_file_functions.py | 12 +----- Lib/test/test_unittest.py | 12 +++--- Lib/test/test_urllib2_localnet.py | 13 ++----- Lib/test/test_winreg.py | 5 +-- Lib/test/test_xmlrpc.py | 14 ++----- Lib/test/test_xmlrpc_net.py | 9 ++--- Lib/test/test_zipimport.py | 14 ++----- 61 files changed, 211 insertions(+), 483 deletions(-) diff --git a/Lib/lib2to3/tests/data/py2_test_grammar.py b/Lib/lib2to3/tests/data/py2_test_grammar.py index 866316173a5b3b..f9e4ea1374f907 100644 --- a/Lib/lib2to3/tests/data/py2_test_grammar.py +++ b/Lib/lib2to3/tests/data/py2_test_grammar.py @@ -8,7 +8,7 @@ # regression test, the filterwarnings() call has been added to # regrtest.py. -from test.test_support import run_unittest, check_syntax_error +from test.test_support import check_syntax_error import unittest import sys # testing import * @@ -967,8 +967,5 @@ def _checkeval(msg, ret): self.assertEqual((6 < 4 if 0 else 2), 2) -def test_main(): - run_unittest(TokenTests, GrammarTests) - if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/lib2to3/tests/data/py3_test_grammar.py b/Lib/lib2to3/tests/data/py3_test_grammar.py index e1eee524874f84..a4a3f7eac0dded 100644 --- a/Lib/lib2to3/tests/data/py3_test_grammar.py +++ b/Lib/lib2to3/tests/data/py3_test_grammar.py @@ -8,7 +8,7 @@ # regression test, the filterwarnings() call has been added to # regrtest.py. -from test.support import run_unittest, check_syntax_error +from test.support import check_syntax_error import unittest import sys # testing import * @@ -952,8 +952,5 @@ def _checkeval(msg, ret): self.assertEqual((6 < 4 if 0 else 2), 2) -def test_main(): - run_unittest(TokenTests, GrammarTests) - if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 7e5a29b18de393..51af6f37d9bce2 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -1525,9 +1525,8 @@ def check_sizeof(test, o, size): # Decorator for running a function in a different locale, correctly resetting # it afterwards. +@contextlib.contextmanager def run_with_locale(catstr, *locales): - def decorator(func): - def inner(*args, **kwds): try: import locale category = getattr(locale, catstr) @@ -1546,16 +1545,11 @@ def inner(*args, **kwds): except: pass - # now run the function, resetting the locale on exceptions try: - return func(*args, **kwds) + yield finally: if locale and orig_locale: locale.setlocale(category, orig_locale) - inner.__name__ = func.__name__ - inner.__doc__ = func.__doc__ - return inner - return decorator #======================================================================= # Decorator for running a function in a specific timezone, correctly diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index ab5f35f0beea56..82c1f6c60f8282 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -5394,13 +5394,11 @@ def test_exit_on_error_with_bad_args(self): self.parser.parse_args('--integers a'.split()) -def test_main(): - support.run_unittest(__name__) +def tearDownModule(): # Remove global references to avoid looking like we have refleaks. RFile.seen = {} WFile.seen = set() - if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_bdb.py b/Lib/test/test_bdb.py index ae168805678829..405f741e36b048 100644 --- a/Lib/test/test_bdb.py +++ b/Lib/test/test_bdb.py @@ -1149,13 +1149,6 @@ def main(): with TracerRun(self) as tracer: tracer.runcall(tfunc_import) -def test_main(): - test.support.run_unittest( - StateTestCase, - RunTestCase, - BreakpointTestCase, - IssuesTestCase, - ) if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_bigaddrspace.py b/Lib/test/test_bigaddrspace.py index aa1f8ca75a9811..50272e99607fd3 100644 --- a/Lib/test/test_bigaddrspace.py +++ b/Lib/test/test_bigaddrspace.py @@ -92,10 +92,7 @@ def test_repeat(self): x = None -def test_main(): - support.run_unittest(BytesTest, StrTest) - if __name__ == '__main__': if len(sys.argv) > 1: support.set_memlimit(sys.argv[1]) - test_main() + unittest.main() diff --git a/Lib/test/test_bigmem.py b/Lib/test/test_bigmem.py index 6a244dd8c94800..859f1539e20b80 100644 --- a/Lib/test/test_bigmem.py +++ b/Lib/test/test_bigmem.py @@ -1247,11 +1247,8 @@ def test_sort(self, size): self.assertEqual(l[:10], [1] * 10) self.assertEqual(l[-10:], [5] * 10) -def test_main(): - support.run_unittest(StrTest, BytesTest, BytearrayTest, - TupleTest, ListTest) if __name__ == '__main__': if len(sys.argv) > 1: support.set_memlimit(sys.argv[1]) - test_main() + unittest.main() diff --git a/Lib/test/test_bool.py b/Lib/test/test_bool.py index 909a59a9d2af64..5550f74c084cbc 100644 --- a/Lib/test/test_bool.py +++ b/Lib/test/test_bool.py @@ -362,8 +362,6 @@ def test_real_and_imag(self): self.assertIs(type(False.real), int) self.assertIs(type(False.imag), int) -def test_main(): - support.run_unittest(BoolTest) if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py index c84f70ebb094a6..fe12816ff06268 100644 --- a/Lib/test/test_bz2.py +++ b/Lib/test/test_bz2.py @@ -1002,15 +1002,9 @@ def test_newline(self): self.assertEqual(f.readlines(), [text]) -def test_main(): - support.run_unittest( - BZ2FileTest, - BZ2CompressorTest, - BZ2DecompressorTest, - CompressDecompressTest, - OpenTest, - ) +def tearDownModule(): support.reap_children() + if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_c_locale_coercion.py b/Lib/test/test_c_locale_coercion.py index fcc85992345dbc..71f934756e26a1 100644 --- a/Lib/test/test_c_locale_coercion.py +++ b/Lib/test/test_c_locale_coercion.py @@ -427,12 +427,9 @@ def test_PYTHONCOERCECLOCALE_set_to_one(self): self.assertEqual(cmd.stdout.rstrip(), loc) -def test_main(): - support.run_unittest( - LocaleConfigurationTests, - LocaleCoercionTests - ) +def tearDownModule(): support.reap_children() + if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index a3560b4b1fd9b2..712d861c4d5a3f 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -841,9 +841,9 @@ def test_sys_flags_not_set(self): ) -def test_main(): - support.run_unittest(CmdLineTest, IgnoreEnvironmentTest) +def tearDownModule(): support.reap_children() + if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py index 7a3707d0f112fa..7cb1370c981b02 100644 --- a/Lib/test/test_cmd_line_script.py +++ b/Lib/test/test_cmd_line_script.py @@ -737,9 +737,9 @@ def test_nonexisting_script(self): self.assertNotEqual(proc.returncode, 0) -def test_main(): - support.run_unittest(CmdLineTest) +def tearDownModule(): support.reap_children() + if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index 0db9be5190da4c..7122300522bff5 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -751,8 +751,6 @@ def test_format(self): self.assertEqual(format(complex(INF, 1), 'F'), 'INF+1.000000j') self.assertEqual(format(complex(INF, -1), 'F'), 'INF-1.000000j') -def test_main(): - support.run_unittest(ComplexTest) if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_concurrent_futures.py b/Lib/test/test_concurrent_futures.py index ad64f4bdb5322d..8545c26d950059 100644 --- a/Lib/test/test_concurrent_futures.py +++ b/Lib/test/test_concurrent_futures.py @@ -1500,16 +1500,10 @@ def test_multiple_set_exception(self): self.assertEqual(f.exception(), e) -_threads_key = None - def setUpModule(): - global _threads_key - _threads_key = support.threading_setup() - - -def tearDownModule(): - support.threading_cleanup(*_threads_key) - multiprocessing.util._cleanup_tests() + unittest.addModuleCleanup(multiprocessing.util._cleanup_tests) + thread_info = support.threading_setup() + unittest.addModuleCleanup(support.threading_cleanup, *thread_info) if __name__ == "__main__": diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index c7a191bccd69c4..2fa68b201172f5 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -4945,8 +4945,11 @@ def test_repr(self): self.assertIn('{!r}: {!r}'.format(k, v), r) -class PTypesLongInitTest(unittest.TestCase): +class AAAPTypesLongInitTest(unittest.TestCase): # This is in its own TestCase so that it can be run before any other tests. + # (Hence the 'AAA' in the test class name: to make it the first + # item in a list sorted by name, like + # unittest.TestLoader.getTestCaseNames() does.) def test_pytype_long_ready(self): # Testing SF bug 551412 ... @@ -5684,12 +5687,5 @@ class A(metaclass=M): pass -def test_main(): - # Run all local test cases, with PTypesLongInitTest first. - support.run_unittest(PTypesLongInitTest, OperatorsTest, - ClassPropertiesAndMethods, DictProxyTests, - MiscTests, PicklingTests, SharedKeyTests, - MroTest) - if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_devpoll.py b/Lib/test/test_devpoll.py index 110c006862737b..85e0accb611b1d 100644 --- a/Lib/test/test_devpoll.py +++ b/Lib/test/test_devpoll.py @@ -6,7 +6,7 @@ import random import select import unittest -from test.support import run_unittest, cpython_only +from test.support import cpython_only if not hasattr(select, 'devpoll') : raise unittest.SkipTest('test works only on Solaris OS family') @@ -138,8 +138,5 @@ def test_events_mask_overflow_c_limits(self): self.assertRaises(OverflowError, pollster.modify, 1, USHRT_MAX + 1) -def test_main(): - run_unittest(DevPollTests) - if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_difflib.py b/Lib/test/test_difflib.py index 42ac1fdcd81cdc..d9399364f6aadc 100644 --- a/Lib/test/test_difflib.py +++ b/Lib/test/test_difflib.py @@ -1,5 +1,5 @@ import difflib -from test.support import run_unittest, findfile +from test.support import findfile import unittest import doctest import sys @@ -547,12 +547,14 @@ def test_longest_match_with_popular_chars(self): self.assertFalse(self.longer_match_exists(a, b, match.size)) -def test_main(): +def setUpModule(): difflib.HtmlDiff._default_prefix = 0 - Doctests = doctest.DocTestSuite(difflib) - run_unittest( - TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs, - TestOutputFormat, TestBytes, TestJunkAPIs, TestFindLongest, Doctests) + + +def load_tests(loader, tests, pattern): + tests.addTest(doctest.DocTestSuite(difflib)) + return tests + if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_distutils.py b/Lib/test/test_distutils.py index a37f11791754d2..790d39c6d35aec 100644 --- a/Lib/test/test_distutils.py +++ b/Lib/test/test_distutils.py @@ -9,16 +9,14 @@ import test.support -def test_main(): - # used by regrtest - test.support.run_unittest(distutils.tests.test_suite()) - test.support.reap_children() - - def load_tests(*_): # used by unittest return distutils.tests.test_suite() +def tearDownModule(): + test.support.reap_children() + + if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_dtrace.py b/Lib/test/test_dtrace.py index 8612e276d76588..1db73cc2d22207 100644 --- a/Lib/test/test_dtrace.py +++ b/Lib/test/test_dtrace.py @@ -6,7 +6,7 @@ import types import unittest -from test.support import findfile, run_unittest +from test.support import findfile def abspath(filename): @@ -97,7 +97,7 @@ class SystemTapBackend(TraceBackend): COMMAND = ["stap", "-g"] -class TraceTests(unittest.TestCase): +class TraceTests: # unittest.TestCase options maxDiff = None @@ -149,30 +149,25 @@ def test_line(self): self.run_case("line") -class DTraceNormalTests(TraceTests): +class DTraceNormalTests(TraceTests, unittest.TestCase): backend = DTraceBackend() optimize_python = 0 -class DTraceOptimizedTests(TraceTests): +class DTraceOptimizedTests(TraceTests, unittest.TestCase): backend = DTraceBackend() optimize_python = 2 -class SystemTapNormalTests(TraceTests): +class SystemTapNormalTests(TraceTests, unittest.TestCase): backend = SystemTapBackend() optimize_python = 0 -class SystemTapOptimizedTests(TraceTests): +class SystemTapOptimizedTests(TraceTests, unittest.TestCase): backend = SystemTapBackend() optimize_python = 2 -def test_main(): - run_unittest(DTraceNormalTests, DTraceOptimizedTests, SystemTapNormalTests, - SystemTapOptimizedTests) - - if __name__ == '__main__': test_main() diff --git a/Lib/test/test_fcntl.py b/Lib/test/test_fcntl.py index 9ab68c67241f46..2a3209eb7db203 100644 --- a/Lib/test/test_fcntl.py +++ b/Lib/test/test_fcntl.py @@ -6,7 +6,7 @@ import sys import unittest from multiprocessing import Process -from test.support import (verbose, TESTFN, unlink, run_unittest, import_module, +from test.support import (verbose, TESTFN, unlink, import_module, cpython_only) # Skip test if no fcntl module. @@ -188,8 +188,6 @@ def test_fcntl_f_getpath(self): res = fcntl.fcntl(self.f.fileno(), fcntl.F_GETPATH, bytes(len(expected))) self.assertEqual(expected, res) -def test_main(): - run_unittest(TestFcntl) if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_filecmp.py b/Lib/test/test_filecmp.py index b5b24a24c8ddea..f6228cc4133acd 100644 --- a/Lib/test/test_filecmp.py +++ b/Lib/test/test_filecmp.py @@ -210,8 +210,5 @@ def _assert_report(self, dircmp_report, expected_report_lines): self.assertEqual(report_lines, expected_report_lines) -def test_main(): - support.run_unittest(FileCompareTestCase, DirCompareTestCase) - if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index 77c7f69a03e1d7..2671d6a174e87b 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -9,7 +9,7 @@ from weakref import proxy from functools import wraps -from test.support import (TESTFN, TESTFN_UNICODE, check_warnings, run_unittest, +from test.support import (TESTFN, TESTFN_UNICODE, check_warnings, make_bad_fd, cpython_only, swap_attr, gc_collect) from collections import UserList @@ -605,15 +605,12 @@ def test_open_code(self): self.assertNotEqual(w.warnings, []) -def test_main(): +def tearDownModule(): # Historically, these tests have been sloppy about removing TESTFN. # So get rid of it no matter what. - try: - run_unittest(CAutoFileTests, PyAutoFileTests, - COtherFileTests, PyOtherFileTests) - finally: - if os.path.exists(TESTFN): - os.unlink(TESTFN) + if os.path.exists(TESTFN): + os.unlink(TESTFN) + if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py index b76287bfe1c274..6f8f977b3950d5 100644 --- a/Lib/test/test_ftplib.py +++ b/Lib/test/test_ftplib.py @@ -12,6 +12,7 @@ import os import threading import time +import unittest try: import ssl except ImportError: @@ -1136,18 +1137,10 @@ def test__all__(self): support.check__all__(self, ftplib, blacklist=blacklist) -def test_main(): - tests = [TestFTPClass, TestTimeouts, - TestIPv6Environment, - TestTLS_FTPClassMixin, TestTLS_FTPClass, - MiscTestCase] - +def setUpModule(): thread_info = support.threading_setup() - try: - support.run_unittest(*tests) - finally: - support.threading_cleanup(*thread_info) + unittest.addModuleCleanup(support.threading_cleanup, *thread_info) if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py index 59cff20e6a7e7f..3313804703f7e2 100644 --- a/Lib/test/test_gc.py +++ b/Lib/test/test_gc.py @@ -1,6 +1,6 @@ import unittest import unittest.mock -from test.support import (verbose, refcount_test, run_unittest, +from test.support import (verbose, refcount_test, cpython_only, start_threads, temp_dir, TESTFN, unlink, import_module) @@ -1388,30 +1388,27 @@ def search_func(encoding): assert_python_ok("-c", code) -def test_main(): +def setUpModule(): + global enabled, debug enabled = gc.isenabled() gc.disable() assert not gc.isenabled() debug = gc.get_debug() gc.set_debug(debug & ~gc.DEBUG_LEAK) # this test is supposed to leak + gc.collect() # Delete 2nd generation garbage + + +def tearDownModule(): + gc.set_debug(debug) + # test gc.enable() even if GC is disabled by default + if verbose: + print("restoring automatic collection") + # make sure to always test gc.enable() + gc.enable() + assert gc.isenabled() + if not enabled: + gc.disable() - try: - gc.collect() # Delete 2nd generation garbage - run_unittest( - GCTests, - GCCallbackTests, - GCTogglingTests, - PythonFinalizationTests) - finally: - gc.set_debug(debug) - # test gc.enable() even if GC is disabled by default - if verbose: - print("restoring automatic collection") - # make sure to always test gc.enable() - gc.enable() - assert gc.isenabled() - if not enabled: - gc.disable() if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_global.py b/Lib/test/test_global.py index 8159602be98ee3..e60f00206bf4ce 100644 --- a/Lib/test/test_global.py +++ b/Lib/test/test_global.py @@ -1,6 +1,6 @@ """Verify that warnings are issued for global statements following use.""" -from test.support import run_unittest, check_syntax_error, check_warnings +from test.support import check_syntax_error, check_warnings import unittest import warnings @@ -52,10 +52,12 @@ def test4(self): compile(prog_text_4, "", "exec") -def test_main(): - with warnings.catch_warnings(): - warnings.filterwarnings("error", module="") - run_unittest(GlobalTests) +def setUpModule(): + cm = warnings.catch_warnings() + cm.__enter__() + unittest.addModuleCleanup(cm.__exit__, None, None, None) + warnings.filterwarnings("error", module="") + if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 1af23c69b4bd7b..1d41efa6c1f89f 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -831,9 +831,5 @@ def test_decompress_cannot_have_flags_compression(self): self.assertEqual(out, b'') -def test_main(verbose=None): - support.run_unittest(TestGzip, TestOpen, TestCommandLine) - - if __name__ == "__main__": - test_main(verbose=True) + unittest.main() diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py index 70e562f4052d9f..c1494d29ca8707 100644 --- a/Lib/test/test_httpservers.py +++ b/Lib/test/test_httpservers.py @@ -1300,21 +1300,9 @@ def test_server_test_ipv4(self, _): self.assertEqual(mock_server.address_family, socket.AF_INET) -def test_main(verbose=None): - cwd = os.getcwd() - try: - support.run_unittest( - RequestHandlerLoggingTestCase, - BaseHTTPRequestHandlerTestCase, - BaseHTTPServerTestCase, - SimpleHTTPServerTestCase, - CGIHTTPServerTestCase, - SimpleHTTPRequestHandlerTestCase, - MiscTestCase, - ScriptTestCase - ) - finally: - os.chdir(cwd) +def setUpModule(): + unittest.addModuleCleanup(os.chdir, os.getcwd()) + if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_importlib/test_locks.py b/Lib/test/test_importlib/test_locks.py index 21794d911ef694..bb6e083c4aff6f 100644 --- a/Lib/test/test_importlib/test_locks.py +++ b/Lib/test/test_importlib/test_locks.py @@ -4,6 +4,7 @@ import sys import threading +import unittest import weakref from test import support @@ -138,15 +139,10 @@ def test_all_locks(self): ) = test_util.test_both(LifetimeTests, init=init) -@support.reap_threads -def test_main(): - support.run_unittest(Frozen_ModuleLockAsRLockTests, - Source_ModuleLockAsRLockTests, - Frozen_DeadlockAvoidanceTests, - Source_DeadlockAvoidanceTests, - Frozen_LifetimeTests, - Source_LifetimeTests) +def setUpModule(): + thread_info = support.threading_setup() + unittest.addModuleCleanup(support.threading_cleanup, *thread_info) if __name__ == '__main__': - test_main() + unittets.main() diff --git a/Lib/test/test_importlib/test_threaded_import.py b/Lib/test/test_importlib/test_threaded_import.py index d1f8db42fbe37e..221d0da20dcc98 100644 --- a/Lib/test/test_importlib/test_threaded_import.py +++ b/Lib/test/test_importlib/test_threaded_import.py @@ -14,8 +14,9 @@ import threading import unittest from unittest import mock +from test import support from test.support import ( - verbose, run_unittest, TESTFN, reap_threads, + verbose, TESTFN, forget, unlink, rmtree, start_threads, script_helper) def task(N, done, done_tasks, errors): @@ -257,19 +258,16 @@ def test_multiprocessing_pool_circular_import(self): script_helper.assert_python_ok(fn) -@reap_threads -def test_main(): - old_switchinterval = None +def setUpModule(): + thread_info = support.threading_setup() + unittest.addModuleCleanup(support.threading_cleanup, *thread_info) try: old_switchinterval = sys.getswitchinterval() + unittest.addModuleCleanup(sys.setswitchinterval, old_switchinterval) sys.setswitchinterval(1e-5) except AttributeError: pass - try: - run_unittest(ThreadedImportTests) - finally: - if old_switchinterval is not None: - sys.setswitchinterval(old_switchinterval) + if __name__ == "__main__": - test_main() + unittets.main() diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index dc7f5c24271047..17c5d87f8ab457 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -24,7 +24,7 @@ except ImportError: ThreadPoolExecutor = None -from test.support import run_unittest, TESTFN, DirsOnSysPath, cpython_only +from test.support import TESTFN, DirsOnSysPath, cpython_only from test.support import MISSING_C_DOCSTRINGS, ALWAYS_EQ from test.support.script_helper import assert_python_ok, assert_python_failure from test import inspect_fodder as mod @@ -4081,18 +4081,5 @@ def test_getsource_reload(self): self.assertInspectEqual(path, module) -def test_main(): - run_unittest( - TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBlockComments, - TestBuggyCases, TestInterpreterStack, TestClassesAndFunctions, TestPredicates, - TestGetcallargsFunctions, TestGetcallargsMethods, - TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState, - TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject, - TestBoundArguments, TestSignaturePrivateHelpers, - TestSignatureDefinitions, TestIsDataDescriptor, - TestGetClosureVars, TestUnwrap, TestMain, TestReload, - TestGetCoroutineState, TestGettingSourceOfToplevelFrames - ) - if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py index 524346939886db..d238985f11b32e 100644 --- a/Lib/test/test_iter.py +++ b/Lib/test/test_iter.py @@ -2,7 +2,7 @@ import sys import unittest -from test.support import run_unittest, TESTFN, unlink, cpython_only +from test.support import TESTFN, unlink, cpython_only from test.support import check_free_after_iterating, ALWAYS_EQ, NEVER_EQ import pickle import collections.abc @@ -1036,9 +1036,5 @@ def test_error_iter(self): self.assertRaises(ZeroDivisionError, iter, BadIterableClass()) -def test_main(): - run_unittest(TestCase) - - if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 8a3ffb5584fb82..08fb79506c5ad4 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -5365,25 +5365,11 @@ def test__all__(self): # Set the locale to the platform-dependent default. I have no idea # why the test does this, but in any case we save the current locale # first and restore it at the end. -@support.run_with_locale('LC_ALL', '') -def test_main(): - tests = [ - BuiltinLevelsTest, BasicFilterTest, CustomLevelsAndFiltersTest, - HandlerTest, MemoryHandlerTest, ConfigFileTest, SocketHandlerTest, - DatagramHandlerTest, MemoryTest, EncodingTest, WarningsTest, - ConfigDictTest, ManagerTest, FormatterTest, BufferingFormatterTest, - StreamHandlerTest, LogRecordFactoryTest, ChildLoggerTest, - QueueHandlerTest, ShutdownTest, ModuleLevelMiscTest, BasicConfigTest, - LoggerAdapterTest, LoggerTest, SMTPHandlerTest, FileHandlerTest, - RotatingFileHandlerTest, LastResortTest, LogRecordTest, - ExceptionTest, SysLogHandlerTest, IPv6SysLogHandlerTest, HTTPHandlerTest, - NTEventLogHandlerTest, TimedRotatingFileHandlerTest, - UnixSocketHandlerTest, UnixDatagramHandlerTest, UnixSysLogHandlerTest, - MiscTestCase - ] - if hasattr(logging.handlers, 'QueueListener'): - tests.append(QueueListenerTest) - support.run_unittest(*tests) +def setUpModule(): + cm = support.run_with_locale('LC_ALL', '') + cm.__enter__() + unittest.addModuleCleanup(cm.__exit__, None, None, None) + if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_lzma.py b/Lib/test/test_lzma.py index ef7dd6325d49f3..ad9484d8ceb366 100644 --- a/Lib/test/test_lzma.py +++ b/Lib/test/test_lzma.py @@ -10,7 +10,7 @@ import unittest from test.support import ( - _4G, TESTFN, import_module, bigmemtest, run_unittest, unlink + _4G, TESTFN, import_module, bigmemtest, unlink ) lzma = import_module("lzma") @@ -1936,14 +1936,5 @@ def test_filter_properties_roundtrip(self): ) -def test_main(): - run_unittest( - CompressorDecompressorTestCase, - CompressDecompressFunctionTestCase, - FileTestCase, - OpenTestCase, - MiscellaneousTestCase, - ) - if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index 6f891d413cd8f1..37b45005621e74 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -2299,15 +2299,9 @@ def test__all__(self): support.check__all__(self, mailbox, blacklist=blacklist) -def test_main(): - tests = (TestMailboxSuperclass, TestMaildir, TestMbox, TestMMDF, TestMH, - TestBabyl, TestMessage, TestMaildirMessage, TestMboxMessage, - TestMHMessage, TestBabylMessage, TestMMDFMessage, - TestMessageConversion, TestProxyFile, TestPartialFile, - MaildirTestCase, TestFakeMailBox, MiscTestCase) - support.run_unittest(*tests) +def tearDownModule(): support.reap_children() if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_multibytecodec.py b/Lib/test/test_multibytecodec.py index 3cf5d7beb144b3..96013ed5052527 100644 --- a/Lib/test/test_multibytecodec.py +++ b/Lib/test/test_multibytecodec.py @@ -380,8 +380,6 @@ class TestHZStateful(TestStateful): reset = b'~}' expected_reset = expected + reset -def test_main(): - support.run_unittest(__name__) if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_optparse.py b/Lib/test/test_optparse.py index 437fdd2be8d85a..b825ca1bdaa299 100644 --- a/Lib/test/test_optparse.py +++ b/Lib/test/test_optparse.py @@ -1655,8 +1655,5 @@ def test__all__(self): support.check__all__(self, optparse, blacklist=blacklist) -def test_main(): - support.run_unittest(__name__) - if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_ossaudiodev.py b/Lib/test/test_ossaudiodev.py index 624fbf21ba7d88..f1b0d642c7ecaf 100644 --- a/Lib/test/test_ossaudiodev.py +++ b/Lib/test/test_ossaudiodev.py @@ -187,7 +187,7 @@ def test_on_closed(self): mixer.close() self.assertRaises(ValueError, mixer.fileno) -def test_main(): +def setUpModule(): try: dsp = ossaudiodev.open('w') except (ossaudiodev.error, OSError) as msg: @@ -196,7 +196,6 @@ def test_main(): raise unittest.SkipTest(msg) raise dsp.close() - support.run_unittest(__name__) if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_pipes.py b/Lib/test/test_pipes.py index 1d538b9898b812..72e1cabab28d77 100644 --- a/Lib/test/test_pipes.py +++ b/Lib/test/test_pipes.py @@ -3,7 +3,7 @@ import string import unittest import shutil -from test.support import TESTFN, run_unittest, unlink, reap_children +from test.support import TESTFN, unlink, reap_children if os.name != 'posix': raise unittest.SkipTest('pipes module only works on posix') @@ -194,9 +194,10 @@ def testClone(self): self.assertNotEqual(id(t.steps), id(u.steps)) self.assertEqual(t.debugging, u.debugging) -def test_main(): - run_unittest(SimplePipeTests) + +def tearDownModule(): reap_children() + if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py index 54725dc641b1de..e48d33dc916d97 100644 --- a/Lib/test/test_pkgutil.py +++ b/Lib/test/test_pkgutil.py @@ -1,5 +1,5 @@ from pathlib import Path -from test.support import run_unittest, unload, check_warnings, CleanImport +from test.support import unload, check_warnings, CleanImport import unittest import sys import importlib @@ -623,9 +623,7 @@ def test_iter_importers_avoids_emulation(self): self.assertEqual(len(w.warnings), 0) -def test_main(): - run_unittest(PkgutilTests, PkgutilPEP302Tests, ExtendPathTests, - NestedNamespacePackageTest, ImportlibMigrationTests) +def tearDownModule(): # this is necessary if test is run repeated (like when finding leaks) import zipimport import importlib @@ -634,4 +632,4 @@ def test_main(): if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_poll.py b/Lib/test/test_poll.py index ef966bf0f56080..294131da795a7b 100644 --- a/Lib/test/test_poll.py +++ b/Lib/test/test_poll.py @@ -7,7 +7,7 @@ import threading import time import unittest -from test.support import TESTFN, run_unittest, reap_threads, cpython_only +from test.support import TESTFN, reap_threads, cpython_only try: select.poll @@ -226,8 +226,5 @@ def test_poll_blocks_with_negative_ms(self): os.close(w) -def test_main(): - run_unittest(PollTests) - if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_poplib.py b/Lib/test/test_poplib.py index b670afcf4e62ee..5228fc46f4b6ef 100644 --- a/Lib/test/test_poplib.py +++ b/Lib/test/test_poplib.py @@ -11,6 +11,7 @@ import errno import threading +import unittest from unittest import TestCase, skipUnless from test import support as test_support from test.support import hashlib_helper @@ -533,15 +534,10 @@ def testTimeoutValue(self): poplib.POP3(HOST, self.port, timeout=0) -def test_main(): - tests = [TestPOP3Class, TestTimeouts, - TestPOP3_SSLClass, TestPOP3_TLSClass] +def setUpModule(): thread_info = test_support.threading_setup() - try: - test_support.run_unittest(*tests) - finally: - test_support.threading_cleanup(*thread_info) + unittest.addModuleCleanup(test_support.threading_cleanup, *thread_info) if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index f4edb8bd9575f4..890b7e04da3649 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -2132,17 +2132,9 @@ def test_utime(self): os.utime("path", dir_fd=0) -def test_main(): - try: - support.run_unittest( - PosixTester, - PosixGroupsTester, - TestPosixSpawn, - TestPosixSpawnP, - TestPosixWeaklinking - ) - finally: - support.reap_children() +def tearDownModule(): + support.reap_children() + if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_profile.py b/Lib/test/test_profile.py index 233649899ec20c..3bb367b0578734 100644 --- a/Lib/test/test_profile.py +++ b/Lib/test/test_profile.py @@ -6,7 +6,7 @@ import os from difflib import unified_diff from io import StringIO -from test.support import TESTFN, run_unittest, unlink, temp_dir, change_cwd +from test.support import TESTFN, unlink, temp_dir, change_cwd from contextlib import contextmanager import profile @@ -155,12 +155,10 @@ def silent(): finally: sys.stdout = stdout -def test_main(): - run_unittest(ProfileTest) def main(): if '-r' not in sys.argv: - test_main() + unittest.main() else: regenerate_expected_output(__file__, ProfileTest) diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py index 0bbdc42c635be4..9b0e94de49d60c 100644 --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -1569,20 +1569,11 @@ def test_sys_path_adjustment_when_curdir_already_included(self): self.assertIsNone(self._get_revised_path(trailing_argv0dir)) -@reap_threads -def test_main(): - try: - test.support.run_unittest(PydocDocTest, - PydocImportTest, - TestDescriptions, - PydocServerTest, - PydocUrlHandlerTest, - TestHelper, - PydocWithMetaClasses, - TestInternalUtilities, - ) - finally: - reap_children() +def setUpModule(): + thread_info = test.support.threading_setup() + unittest.addModuleCleanup(test.support.threading_cleanup, *thread_info) + unittest.addModuleCleanup(reap_children) + if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_resource.py b/Lib/test/test_resource.py index e5ece5284cf15b..089dbf9b4015e4 100644 --- a/Lib/test/test_resource.py +++ b/Lib/test/test_resource.py @@ -172,8 +172,5 @@ def __getitem__(self, key): limits) -def test_main(verbose=None): - support.run_unittest(ResourceTest) - if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py index ce3a422b502a06..3e7fedaa6da702 100644 --- a/Lib/test/test_sax.py +++ b/Lib/test/test_sax.py @@ -22,7 +22,7 @@ from urllib.error import URLError import urllib.request from test import support -from test.support import findfile, run_unittest, FakePath, TESTFN +from test.support import findfile, FakePath, TESTFN TEST_XMLFILE = findfile("test.xml", subdir="xmltestdata") TEST_XMLFILE_OUT = findfile("test.xml.out", subdir="xmltestdata") @@ -1353,19 +1353,5 @@ def test_nsattrs_wattr(self): self.assertEqual(attrs.getQNameByName((ns_uri, "attr")), "ns:attr") -def test_main(): - run_unittest(MakeParserTest, - ParseTest, - SaxutilsTest, - PrepareInputSourceTest, - StringXmlgenTest, - BytesXmlgenTest, - WriterXmlgenTest, - StreamWriterXmlgenTest, - StreamReaderWriterXmlgenTest, - ExpatReaderTest, - ErrorReportingTest, - XmlReaderTest) - if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_selectors.py b/Lib/test/test_selectors.py index 2274c39a79a532..3a3f87760a975b 100644 --- a/Lib/test/test_selectors.py +++ b/Lib/test/test_selectors.py @@ -48,7 +48,7 @@ def find_ready_matching(ready, flag): return match -class BaseSelectorTestCase(unittest.TestCase): +class BaseSelectorTestCase: def make_socketpair(self): rd, wr = socketpair() @@ -492,26 +492,28 @@ def test_above_fd_setsize(self): self.assertEqual(NUM_FDS // 2, len(fds)) -class DefaultSelectorTestCase(BaseSelectorTestCase): +class DefaultSelectorTestCase(BaseSelectorTestCase, unittest.TestCase): SELECTOR = selectors.DefaultSelector -class SelectSelectorTestCase(BaseSelectorTestCase): +class SelectSelectorTestCase(BaseSelectorTestCase, unittest.TestCase): SELECTOR = selectors.SelectSelector @unittest.skipUnless(hasattr(selectors, 'PollSelector'), "Test needs selectors.PollSelector") -class PollSelectorTestCase(BaseSelectorTestCase, ScalableSelectorMixIn): +class PollSelectorTestCase(BaseSelectorTestCase, ScalableSelectorMixIn, + unittest.TestCase): SELECTOR = getattr(selectors, 'PollSelector', None) @unittest.skipUnless(hasattr(selectors, 'EpollSelector'), "Test needs selectors.EpollSelector") -class EpollSelectorTestCase(BaseSelectorTestCase, ScalableSelectorMixIn): +class EpollSelectorTestCase(BaseSelectorTestCase, ScalableSelectorMixIn, + unittest.TestCase): SELECTOR = getattr(selectors, 'EpollSelector', None) @@ -528,7 +530,8 @@ def test_register_file(self): @unittest.skipUnless(hasattr(selectors, 'KqueueSelector'), "Test needs selectors.KqueueSelector)") -class KqueueSelectorTestCase(BaseSelectorTestCase, ScalableSelectorMixIn): +class KqueueSelectorTestCase(BaseSelectorTestCase, ScalableSelectorMixIn, + unittest.TestCase): SELECTOR = getattr(selectors, 'KqueueSelector', None) @@ -560,19 +563,15 @@ def test_empty_select_timeout(self): @unittest.skipUnless(hasattr(selectors, 'DevpollSelector'), "Test needs selectors.DevpollSelector") -class DevpollSelectorTestCase(BaseSelectorTestCase, ScalableSelectorMixIn): +class DevpollSelectorTestCase(BaseSelectorTestCase, ScalableSelectorMixIn, + unittest.TestCase): SELECTOR = getattr(selectors, 'DevpollSelector', None) - -def test_main(): - tests = [DefaultSelectorTestCase, SelectSelectorTestCase, - PollSelectorTestCase, EpollSelectorTestCase, - KqueueSelectorTestCase, DevpollSelectorTestCase] - support.run_unittest(*tests) +def tearDownModule(): support.reap_children() if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index f928bcea2d1a54..42ebcc04dc2b83 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -2281,6 +2281,7 @@ def test_bio_read_write_data(self): self.ssl_io_loop(sock, incoming, outgoing, sslobj.unwrap) +@support.requires_resource('network') class NetworkedTests(unittest.TestCase): def test_timeout_connect_ex(self): @@ -4812,7 +4813,7 @@ def sni_cb(sock, servername, ctx): s.connect((HOST, server.port)) -def test_main(verbose=False): +def setUpModule(): if support.verbose: plats = { 'Mac': platform.mac_ver, @@ -4843,20 +4844,9 @@ def test_main(verbose=False): if not os.path.exists(filename): raise support.TestFailed("Can't read certificate file %r" % filename) - tests = [ - ContextTests, BasicSocketTests, SSLErrorTests, MemoryBIOTests, - SSLObjectTests, SimpleBackgroundTests, ThreadedTests, - TestPostHandshakeAuth, TestSSLDebug - ] - - if support.is_resource_enabled('network'): - tests.append(NetworkedTests) - thread_info = support.threading_setup() - try: - support.run_unittest(*tests) - finally: - support.threading_cleanup(*thread_info) + unittest.addModuleCleanup(support.threading_cleanup, *thread_info) + if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 60a7741194f2fc..97be7af2d72251 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -705,20 +705,5 @@ def test_print_warning(self): # SuppressCrashReport -def _warn_about_deprecation(): - # In 3.10+ this lives in test.support.warnings_helper - warnings.warn( - "This is used in test_support test to ensure" - " support.ignore_deprecations_from() works as expected." - " You should not be seeing this.", - DeprecationWarning, - stacklevel=0, - ) - - -def test_main(): - tests = [TestSupport] - support.run_unittest(*tests) - if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_tcl.py b/Lib/test/test_tcl.py index fa974d181136e7..cd142ab6b03353 100644 --- a/Lib/test/test_tcl.py +++ b/Lib/test/test_tcl.py @@ -799,8 +799,5 @@ def setUpModule(): print('patchlevel =', tcl.call('info', 'patchlevel')) -def test_main(): - support.run_unittest(TclTest, TkinterTest, BigmemTclTest) - if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_threadsignals.py b/Lib/test/test_threadsignals.py index eeacd3698cb138..b000adc32232a0 100644 --- a/Lib/test/test_threadsignals.py +++ b/Lib/test/test_threadsignals.py @@ -230,7 +230,7 @@ def send_signals(): signal.signal(signal.SIGUSR1, old_handler) -def test_main(): +def setUpModule(): global signal_blackboard signal_blackboard = { signal.SIGUSR1 : {'tripped': 0, 'tripped_by': 0 }, @@ -238,10 +238,8 @@ def test_main(): signal.SIGALRM : {'tripped': 0, 'tripped_by': 0 } } oldsigs = registerSignals(handle_signals, handle_signals, handle_signals) - try: - support.run_unittest(ThreadSignals) - finally: - registerSignals(*oldsigs) + unittest.addModuleCleanup(registerSignals, *oldsigs) + if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_timeout.py b/Lib/test/test_timeout.py index ac803f5d638237..fb73b5a45208f8 100644 --- a/Lib/test/test_timeout.py +++ b/Lib/test/test_timeout.py @@ -290,13 +290,9 @@ def testRecvfromTimeout(self): self._sock_operation(1, 1.5, 'recvfrom', 1024) -def test_main(): +def setUpModule(): support.requires('network') - support.run_unittest( - CreationTestCase, - TCPTimeoutTestCase, - UDPTimeoutTestCase, - ) + if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_tracemalloc.py b/Lib/test/test_tracemalloc.py index b10d1798c29777..687e2b65733da7 100644 --- a/Lib/test/test_tracemalloc.py +++ b/Lib/test/test_tracemalloc.py @@ -1081,15 +1081,5 @@ def test_stop_untrack(self): self.untrack() -def test_main(): - support.run_unittest( - TestTraceback, - TestTracemallocEnabled, - TestSnapshot, - TestFilters, - TestCommandLine, - TestCAPI, - ) - if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_unicode_file.py b/Lib/test/test_unicode_file.py index e8feb42c6b0c25..a9412e40e761a8 100644 --- a/Lib/test/test_unicode_file.py +++ b/Lib/test/test_unicode_file.py @@ -5,7 +5,7 @@ import unicodedata import unittest -from test.support import (run_unittest, rmtree, change_cwd, +from test.support import (rmtree, change_cwd, TESTFN_ENCODING, TESTFN_UNICODE, TESTFN_UNENCODABLE, create_empty_file) if not os.path.supports_unicode_filenames: @@ -133,8 +133,6 @@ def test_directories(self): self._do_directory(TESTFN_UNENCODABLE+ext, TESTFN_UNENCODABLE+ext) -def test_main(): - run_unittest(__name__) if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_unicode_file_functions.py b/Lib/test/test_unicode_file_functions.py index 1cd0d621cd4b3a..e36c7cddf3e9a9 100644 --- a/Lib/test/test_unicode_file_functions.py +++ b/Lib/test/test_unicode_file_functions.py @@ -181,15 +181,5 @@ class UnicodeNFKDFileTests(UnicodeFileTests): normal_form = 'NFKD' -def test_main(): - support.run_unittest( - UnicodeFileTests, - UnicodeNFCFileTests, - UnicodeNFDFileTests, - UnicodeNFKCFileTests, - UnicodeNFKDFileTests, - ) - - if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_unittest.py b/Lib/test/test_unittest.py index bfc3ded6f128da..1079c7df2e51c2 100644 --- a/Lib/test/test_unittest.py +++ b/Lib/test/test_unittest.py @@ -3,14 +3,14 @@ from test import support -def test_main(): - # used by regrtest - support.run_unittest(unittest.test.suite()) - support.reap_children() - def load_tests(*_): # used by unittest return unittest.test.suite() + +def tearDownModule(): + support.reap_children() + + if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_urllib2_localnet.py b/Lib/test/test_urllib2_localnet.py index ed426b05a71982..e73132ab699746 100644 --- a/Lib/test/test_urllib2_localnet.py +++ b/Lib/test/test_urllib2_localnet.py @@ -660,17 +660,10 @@ def test_line_iteration(self): self.assertEqual(index + 1, len(lines)) -threads_key = None - def setUpModule(): - # Store the threading_setup in a key and ensure that it is cleaned up - # in the tearDown - global threads_key - threads_key = support.threading_setup() - -def tearDownModule(): - if threads_key: - support.threading_cleanup(*threads_key) + thread_info = support.threading_setup() + unittest.addModuleCleanup(support.threading_cleanup, *thread_info) + if __name__ == "__main__": unittest.main() diff --git a/Lib/test/test_winreg.py b/Lib/test/test_winreg.py index 5c25ec8f7ec674..1e689f74851b93 100644 --- a/Lib/test/test_winreg.py +++ b/Lib/test/test_winreg.py @@ -489,12 +489,9 @@ def test_exception_numbers(self): with self.assertRaises(FileNotFoundError) as ctx: QueryValue(HKEY_CLASSES_ROOT, 'some_value_that_does_not_exist') -def test_main(): - support.run_unittest(LocalWinregTests, RemoteWinregTests, - Win64WinregTests) if __name__ == "__main__": if not REMOTE_NAME: print("Remote registry calls can be tested using", "'test_winreg.py --remote \\\\machine_name'") - test_main() + unittest.main() diff --git a/Lib/test/test_xmlrpc.py b/Lib/test/test_xmlrpc.py index 75544035381a99..83b618b749162b 100644 --- a/Lib/test/test_xmlrpc.py +++ b/Lib/test/test_xmlrpc.py @@ -1502,16 +1502,10 @@ def test_xmlrpcserver_has_use_builtin_types_flag(self): self.assertTrue(server.use_builtin_types) -@support.reap_threads -def test_main(): - support.run_unittest(XMLRPCTestCase, HelperTestCase, DateTimeTestCase, - BinaryTestCase, FaultTestCase, UseBuiltinTypesTestCase, - SimpleServerTestCase, SimpleServerEncodingTestCase, - KeepaliveServerTestCase1, KeepaliveServerTestCase2, - GzipServerTestCase, GzipUtilTestCase, HeadersServerTestCase, - MultiPathServerTestCase, ServerProxyTestCase, FailingServerTestCase, - CGIHandlerTestCase, SimpleXMLRPCDispatcherTestCase) +def setUpModule(): + thread_info = support.threading_setup() + unittest.addModuleCleanup(support.threading_cleanup, *thread_info) if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_xmlrpc_net.py b/Lib/test/test_xmlrpc_net.py index f3652b86f75102..51167b28bc5a1e 100644 --- a/Lib/test/test_xmlrpc_net.py +++ b/Lib/test/test_xmlrpc_net.py @@ -5,6 +5,9 @@ import xmlrpc.client as xmlrpclib +support.requires("network") + + @unittest.skip('XXX: buildbot.python.org/all/xmlrpc/ is gone') class PythonBuildersTest(unittest.TestCase): @@ -24,9 +27,5 @@ def test_python_builders(self): self.assertTrue([x for x in builders if "3.x" in x], builders) -def test_main(): - support.requires("network") - support.run_unittest(PythonBuildersTest) - if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py index 80c98b4b4661d8..2e2438863da284 100644 --- a/Lib/test/test_zipimport.py +++ b/Lib/test/test_zipimport.py @@ -766,15 +766,9 @@ def _testBogusZipFile(self): zipimport._zip_directory_cache.clear() -def test_main(): - try: - support.run_unittest( - UncompressedZipImportTestCase, - CompressedZipImportTestCase, - BadFileZipImportTestCase, - ) - finally: - support.unlink(TESTMOD) +def tearDownModule(): + support.unlink(TESTMOD) + if __name__ == "__main__": - test_main() + unittest.main() From c20faba3eb18d6964b0a0f0f64fdb4d6b79a8a1b Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 19 Sep 2021 17:03:10 +0300 Subject: [PATCH 2/2] Restore _warn_about_deprecation in test_support. --- Lib/test/test_support.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 97be7af2d72251..159346dca6194f 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -705,5 +705,16 @@ def test_print_warning(self): # SuppressCrashReport +def _warn_about_deprecation(): + # In 3.10+ this lives in test.support.warnings_helper + warnings.warn( + "This is used in test_support test to ensure" + " support.ignore_deprecations_from() works as expected." + " You should not be seeing this.", + DeprecationWarning, + stacklevel=0, + ) + + if __name__ == '__main__': unittest.main()