From 9f6f21150622b25e665cc5d9533839a5cc092121 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Tue, 12 Apr 2022 05:41:15 +1000 Subject: [PATCH] unittest: Reset python env between tests. --- python-stdlib/unittest/unittest.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/python-stdlib/unittest/unittest.py b/python-stdlib/unittest/unittest.py index 5a87f601..dd4bb607 100644 --- a/python-stdlib/unittest/unittest.py +++ b/python-stdlib/unittest/unittest.py @@ -10,6 +10,13 @@ except ImportError: traceback = None +def _snapshot_modules(): + return {k: v for k, v in sys.modules.items()} + + +__modules__ = _snapshot_modules() + + class SkipTest(Exception): pass @@ -375,6 +382,13 @@ def _test_cases(mod): def run_module(runner, module, path, top): + if not module: + raise ValueError("Empty module name") + + # Reset the python environment before running test + sys.modules.clear() + sys.modules.update(__modules__) + sys_path_initial = sys.path[:] # Add script dir and top dir to import path sys.path.insert(0, str(path)) @@ -395,6 +409,8 @@ def run_module(runner, module, path, top): def discover(runner: TestRunner): from unittest_discover import discover + global __modules__ + __modules__ = _snapshot_modules() return discover(runner=runner)