testing: Add rollback_sysmodules fixture to unload modules after tests

This commit is contained in:
Takeshi KOMIYA
2020-12-28 23:21:14 +09:00
parent 476169284d
commit 1a659c6ca7

View File

@@ -250,3 +250,15 @@ def tempdir(tmpdir: str) -> "util.path":
this fixture is for compat with old test implementation.
"""
return util.path(tmpdir)
@pytest.fixture
def rollback_sysmodules():
"""Rollback sys.modules to before testing to unload modules during tests."""
try:
sysmodules = list(sys.modules)
yield
finally:
for modname in list(sys.modules):
if modname not in sysmodules:
sys.modules.pop(modname)