From 851811f5772c43f72f445e2ce1ac3ea9da951ae3 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 13 Sep 2021 10:49:53 +0300 Subject: bpo-5846: Do not use obsolete unittest functions. (GH-28303) Get rid of use of makeSuite() and findTestCases(). Also make test_math and test_threading_local discoverable. --- Lib/test/support/__init__.py | 5 +++-- Lib/test/support/testresult.py | 2 +- Lib/test/test_dbm.py | 5 ++--- Lib/test/test_email/torture_test.py | 26 ++++++++++---------------- Lib/test/test_io.py | 6 ++++-- Lib/test/test_math.py | 13 +++++-------- Lib/test/test_pdb.py | 10 +++------- Lib/test/test_threading_local.py | 17 +++++++---------- Lib/test/test_zipimport.py | 3 ++- 9 files changed, 37 insertions(+), 50 deletions(-) (limited to 'Lib/test') diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index a86bfca4ce..89f5e5a35c 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -1108,17 +1108,18 @@ def _compile_match_function(patterns): def run_unittest(*classes): """Run tests from unittest.TestCase-derived classes.""" valid_types = (unittest.TestSuite, unittest.TestCase) + loader = unittest.TestLoader() suite = unittest.TestSuite() for cls in classes: if isinstance(cls, str): if cls in sys.modules: - suite.addTest(unittest.findTestCases(sys.modules[cls])) + suite.addTest(loader.loadTestsFromModule(sys.modules[cls])) else: raise ValueError("str arguments must be keys in sys.modules") elif isinstance(cls, valid_types): suite.addTest(cls) else: - suite.addTest(unittest.makeSuite(cls)) + suite.addTest(loader.loadTestsFromTestCase(cls)) _filter_suite(suite, match_test) _run_suite(suite) diff --git a/Lib/test/support/testresult.py b/Lib/test/support/testresult.py index 6f2edda0f5..2cd1366cd8 100644 --- a/Lib/test/support/testresult.py +++ b/Lib/test/support/testresult.py @@ -173,7 +173,7 @@ if __name__ == '__main__': raise RuntimeError('error message') suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(TestTests)) + suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestTests)) stream = io.StringIO() runner_cls = get_test_runner_class(sum(a == '-v' for a in sys.argv)) runner = runner_cls(sys.stdout) diff --git a/Lib/test/test_dbm.py b/Lib/test/test_dbm.py index 0404e063fd..cf0758653f 100644 --- a/Lib/test/test_dbm.py +++ b/Lib/test/test_dbm.py @@ -221,9 +221,8 @@ def load_tests(loader, tests, pattern): classes.append(type("TestCase-" + mod.__name__, (AnyDBMTestCase, unittest.TestCase), {'module': mod})) - suites = [unittest.makeSuite(c) for c in classes] - - tests.addTests(suites) + for c in classes: + tests.addTest(loader.loadTestsFromTestCase(c)) return tests if __name__ == "__main__": diff --git a/Lib/test/test_email/torture_test.py b/Lib/test/test_email/torture_test.py index e72a146eca..9cf9362c9b 100644 --- a/Lib/test/test_email/torture_test.py +++ b/Lib/test/test_email/torture_test.py @@ -12,7 +12,6 @@ import unittest from io import StringIO from test.test_email import TestEmailBase -from test.support import run_unittest import email from email import __file__ as testfile @@ -24,10 +23,11 @@ def openfile(filename): return open(path, 'r') # Prevent this test from running in the Python distro -try: - openfile('crispin-torture.txt') -except OSError: - raise unittest.SkipTest +def setUpModule(): + try: + openfile('crispin-torture.txt') + except OSError: + raise unittest.SkipTest @@ -117,17 +117,11 @@ def _testclasses(): return [getattr(mod, name) for name in dir(mod) if name.startswith('Test')] -def suite(): - suite = unittest.TestSuite() +def load_tests(loader, tests, pattern): + suite = loader.suiteClass() for testclass in _testclasses(): - suite.addTest(unittest.makeSuite(testclass)) + suite.addTest(loader.loadTestsFromTestCase(testclass)) return suite - -def test_main(): - for testclass in _testclasses(): - run_unittest(testclass) - - -if __name__ == '__main__': - unittest.main(defaultTest='suite') +if __name__ == "__main__": + unittest.main() diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index d52f97bb39..d1e3b68cc8 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -4604,7 +4604,7 @@ class PySignalsTest(SignalsTest): test_reentrant_write_text = None -def load_tests(*args): +def load_tests(loader, tests, pattern): tests = (CIOTest, PyIOTest, APIMismatchTest, CBufferedReaderTest, PyBufferedReaderTest, CBufferedWriterTest, PyBufferedWriterTest, @@ -4636,7 +4636,9 @@ def load_tests(*args): for name, obj in py_io_ns.items(): setattr(test, name, obj) - suite = unittest.TestSuite([unittest.makeSuite(test) for test in tests]) + suite = loader.suiteClass() + for test in tests: + suite.addTest(loader.loadTestsFromTestCase(test)) return suite if __name__ == "__main__": diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 42b61c317c..6d67d6293b 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -1,7 +1,7 @@ # Python test set -- math module # XXXX Should not do tests around zero only -from test.support import run_unittest, verbose, requires_IEEE_754 +from test.support import verbose, requires_IEEE_754 from test import support import unittest import itertools @@ -2225,13 +2225,10 @@ class IsCloseTests(unittest.TestCase): self.assertAllNotClose(fraction_examples, rel_tol=1e-9) -def test_main(): +def load_tests(loader, tests, pattern): from doctest import DocFileSuite - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(MathTests)) - suite.addTest(unittest.makeSuite(IsCloseTests)) - suite.addTest(DocFileSuite("ieee754.txt")) - run_unittest(suite) + tests.addTest(DocFileSuite("ieee754.txt")) + return tests if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 5bb8069021..449112978c 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -1975,14 +1975,10 @@ class ChecklineTests(unittest.TestCase): self.assertFalse(db.checkline(os_helper.TESTFN, lineno)) -def load_tests(*args): +def load_tests(loader, tests, pattern): from test import test_pdb - suites = [ - unittest.makeSuite(PdbTestCase), - unittest.makeSuite(ChecklineTests), - doctest.DocTestSuite(test_pdb) - ] - return unittest.TestSuite(suites) + tests.addTest(doctest.DocTestSuite(test_pdb)) + return tests if __name__ == '__main__': diff --git a/Lib/test/test_threading_local.py b/Lib/test/test_threading_local.py index 13facb5133..1567c41000 100644 --- a/Lib/test/test_threading_local.py +++ b/Lib/test/test_threading_local.py @@ -201,22 +201,19 @@ class PyThreadingLocalTest(unittest.TestCase, BaseLocalTest): _local = _threading_local.local -def test_main(): - suite = unittest.TestSuite() - suite.addTest(DocTestSuite('_threading_local')) - suite.addTest(unittest.makeSuite(ThreadLocalTest)) - suite.addTest(unittest.makeSuite(PyThreadingLocalTest)) +def load_tests(loader, tests, pattern): + tests.addTest(DocTestSuite('_threading_local')) local_orig = _threading_local.local def setUp(test): _threading_local.local = _thread._local def tearDown(test): _threading_local.local = local_orig - suite.addTest(DocTestSuite('_threading_local', - setUp=setUp, tearDown=tearDown) - ) + tests.addTests(DocTestSuite('_threading_local', + setUp=setUp, tearDown=tearDown) + ) + return tests - support.run_unittest(suite) if __name__ == '__main__': - test_main() + unittest.main() diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py index c57f8615eb..938674fcbd 100644 --- a/Lib/test/test_zipimport.py +++ b/Lib/test/test_zipimport.py @@ -155,7 +155,8 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase): # zlib.decompress function object, after which the problem being # tested here wouldn't be a problem anymore... # (Hence the 'A' in the test method name: to make it the first - # item in a list sorted by name, like unittest.makeSuite() does.) + # item in a list sorted by name, like + # unittest.TestLoader.getTestCaseNames() does.) # # This test fails on platforms on which the zlib module is # statically linked, but the problem it tests for can't -- cgit v1.2.1