unittest: Reset python env between tests.

pull/488/head
Andrew Leech 2022-04-12 05:41:15 +10:00
rodzic cb8d108ac1
commit 9f6f211506
1 zmienionych plików z 16 dodań i 0 usunięć

Wyświetl plik

@ -10,6 +10,13 @@ except ImportError:
traceback = None traceback = None
def _snapshot_modules():
return {k: v for k, v in sys.modules.items()}
__modules__ = _snapshot_modules()
class SkipTest(Exception): class SkipTest(Exception):
pass pass
@ -375,6 +382,13 @@ def _test_cases(mod):
def run_module(runner, module, path, top): 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[:] sys_path_initial = sys.path[:]
# Add script dir and top dir to import path # Add script dir and top dir to import path
sys.path.insert(0, str(path)) sys.path.insert(0, str(path))
@ -395,6 +409,8 @@ def run_module(runner, module, path, top):
def discover(runner: TestRunner): def discover(runner: TestRunner):
from unittest_discover import discover from unittest_discover import discover
global __modules__
__modules__ = _snapshot_modules()
return discover(runner=runner) return discover(runner=runner)