Skip to content

Commit 9f6f211

Browse files
committed
unittest: Reset python env between tests.
1 parent cb8d108 commit 9f6f211

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

python-stdlib/unittest/unittest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
traceback = None
1111

1212

13+
def _snapshot_modules():
14+
return {k: v for k, v in sys.modules.items()}
15+
16+
17+
__modules__ = _snapshot_modules()
18+
19+
1320
class SkipTest(Exception):
1421
pass
1522

@@ -375,6 +382,13 @@ def _test_cases(mod):
375382

376383

377384
def run_module(runner, module, path, top):
385+
if not module:
386+
raise ValueError("Empty module name")
387+
388+
# Reset the python environment before running test
389+
sys.modules.clear()
390+
sys.modules.update(__modules__)
391+
378392
sys_path_initial = sys.path[:]
379393
# Add script dir and top dir to import path
380394
sys.path.insert(0, str(path))
@@ -395,6 +409,8 @@ def run_module(runner, module, path, top):
395409
def discover(runner: TestRunner):
396410
from unittest_discover import discover
397411

412+
global __modules__
413+
__modules__ = _snapshot_modules()
398414
return discover(runner=runner)
399415

400416

0 commit comments

Comments
 (0)