gh-108927: Fix removing testing modules from sys.modules (GH-108952)

It breaks import machinery if the test module has submodules used in
other tests.
This commit is contained in:
Serhiy Storchaka 2023-12-04 17:43:27 +02:00 committed by GitHub
parent c718ab92a5
commit e08b70fab1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 67 additions and 9 deletions

View file

@ -2031,6 +2031,25 @@ class ArgsTestCase(BaseTestCase):
self.check_executed_tests(output, tests,
stats=len(tests), parallel=True)
def test_unload_tests(self):
# Test that unloading test modules does not break tests
# that import from other tests.
# The test execution order matters for this test.
# Both test_regrtest_a and test_regrtest_c which are executed before
# and after test_regrtest_b import a submodule from the test_regrtest_b
# package and use it in testing. test_regrtest_b itself does not import
# that submodule.
# Previously test_regrtest_c failed because test_regrtest_b.util in
# sys.modules was left after test_regrtest_a (making the import
# statement no-op), but new test_regrtest_b without the util attribute
# was imported for test_regrtest_b.
testdir = os.path.join(os.path.dirname(__file__),
'regrtestdata', 'import_from_tests')
tests = [f'test_regrtest_{name}' for name in ('a', 'b', 'c')]
args = ['-Wd', '-E', '-bb', '-m', 'test', '--testdir=%s' % testdir, *tests]
output = self.run_python(args)
self.check_executed_tests(output, tests, stats=3)
def check_add_python_opts(self, option):
# --fast-ci and --slow-ci add "-u -W default -bb -E" options to Python
code = textwrap.dedent(r"""