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.
This commit is contained in:
Serhiy Storchaka 2021-09-19 15:27:33 +03:00 committed by GitHub
parent 664448d81f
commit 40348acc18
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
61 changed files with 208 additions and 484 deletions

View file

@ -4,6 +4,7 @@ init = test_util.import_importlib('importlib')
import sys
import threading
import unittest
import weakref
from test import support
@ -139,15 +140,10 @@ class LifetimeTests:
) = test_util.test_both(LifetimeTests, init=init)
@threading_helper.reap_threads
def test_main():
support.run_unittest(Frozen_ModuleLockAsRLockTests,
Source_ModuleLockAsRLockTests,
Frozen_DeadlockAvoidanceTests,
Source_DeadlockAvoidanceTests,
Frozen_LifetimeTests,
Source_LifetimeTests)
def setUpModule():
thread_info = threading_helper.threading_setup()
unittest.addModuleCleanup(threading_helper.threading_cleanup, *thread_info)
if __name__ == '__main__':
test_main()
unittets.main()

View file

@ -14,7 +14,7 @@ import shutil
import threading
import unittest
from unittest import mock
from test.support import (verbose, run_unittest)
from test.support import verbose
from test.support.import_helper import forget
from test.support.os_helper import (TESTFN, unlink, rmtree)
from test.support import script_helper, threading_helper
@ -258,19 +258,16 @@ class ThreadedImportTests(unittest.TestCase):
script_helper.assert_python_ok(fn)
@threading_helper.reap_threads
def test_main():
old_switchinterval = None
def setUpModule():
thread_info = threading_helper.threading_setup()
unittest.addModuleCleanup(threading_helper.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()