From 59b2995406b11816e2750f5fa4457b281e414caa Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 4 Sep 2023 14:01:37 +0300 Subject: [PATCH 1/2] gh-89392: Remove support of test_main() in libregrtest --- Lib/test/libregrtest/runtest.py | 11 +++-------- Lib/test/test_regrtest.py | 8 ++++---- .../2023-09-04-15-18-14.gh-issue-89392.8A4T5p.rst | 2 ++ 3 files changed, 9 insertions(+), 12 deletions(-) create mode 100644 Misc/NEWS.d/next/Tests/2023-09-04-15-18-14.gh-issue-89392.8A4T5p.rst diff --git a/Lib/test/libregrtest/runtest.py b/Lib/test/libregrtest/runtest.py index 6e3fab1a88318f..b1ef362b4f02c6 100644 --- a/Lib/test/libregrtest/runtest.py +++ b/Lib/test/libregrtest/runtest.py @@ -440,14 +440,9 @@ def _load_run_test(result: TestResult, ns: Namespace) -> None: test_mod = importlib.import_module(module_name) - # If the test has a test_main, that will run the appropriate - # tests. If not, use normal unittest test runner. - test_main = getattr(test_mod, "test_main", None) - if test_main is not None: - test_func = test_main - else: - def test_func(): - return run_unittest(test_mod) + assert not hasattr(test_mod, "test_main") + def test_func(): + return run_unittest(test_mod) try: with save_env(ns, result.test_name): diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py index eb321c4ca05f1a..222a4607bcbdc8 100644 --- a/Lib/test/test_regrtest.py +++ b/Lib/test/test_regrtest.py @@ -1802,9 +1802,9 @@ def my_function(): 7948648 """ - def test_main(): - testmod = sys.modules[__name__] - return support.run_doctest(testmod) + def load_tests(loader, tests, pattern): + tests.addTest(doctest.DocTestSuite()) + return tests ''') testname = self.create_test(code=code) @@ -1813,7 +1813,7 @@ def test_main(): self.check_executed_tests(output, [testname], failed=[testname], randomize=True, - stats=TestStats(4, 2, 1)) + stats=TestStats(1, 1, 0)) class TestUtils(unittest.TestCase): diff --git a/Misc/NEWS.d/next/Tests/2023-09-04-15-18-14.gh-issue-89392.8A4T5p.rst b/Misc/NEWS.d/next/Tests/2023-09-04-15-18-14.gh-issue-89392.8A4T5p.rst new file mode 100644 index 00000000000000..e1dea8e78cdd4e --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2023-09-04-15-18-14.gh-issue-89392.8A4T5p.rst @@ -0,0 +1,2 @@ +Removed support of ``test_main()`` function in tests. They now always use +normal unittest test runner. From 8e2ef4e579640ec865cbce2c21943f4c405996f7 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 5 Sep 2023 07:20:08 +0300 Subject: [PATCH 2/2] Update Lib/test/libregrtest/runtest.py --- Lib/test/libregrtest/runtest.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/libregrtest/runtest.py b/Lib/test/libregrtest/runtest.py index b1ef362b4f02c6..9cb71fbfb1d178 100644 --- a/Lib/test/libregrtest/runtest.py +++ b/Lib/test/libregrtest/runtest.py @@ -440,7 +440,9 @@ def _load_run_test(result: TestResult, ns: Namespace) -> None: test_mod = importlib.import_module(module_name) - assert not hasattr(test_mod, "test_main") + if hasattr(test_mod, "test_main"): + # https://github.com/python/cpython/issues/89392 + raise Exception("Module {result.test_name} defines test_main() which is no longer supported by regrtest") def test_func(): return run_unittest(test_mod)