Skip to content

Commit 285b02c

Browse files
BUG: Tear down common import names in notebook tests
1 parent 3645b55 commit 285b02c

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

test/test_notebooks.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,28 @@ def _cwd(directory):
4343
os.chdir(cwd)
4444

4545

46+
@pytest.fixture
47+
def purge_notebook_modules():
48+
"""Remove notebook modules from sys.modules after test.
49+
50+
Because these modules share common names in each notebook and
51+
module names have a system-wide scope, import machinery will not
52+
import new modules for successive notebooks unless old modules of
53+
same name are removed from sys.modules.
54+
55+
This might be better served by fixing imports in notebooks using
56+
importlib.
57+
"""
58+
SENTINEL = object()
59+
sys.modules.pop("utils", SENTINEL)
60+
sys.modules.pop("mock_data", SENTINEL)
61+
yield
62+
sys.modules.pop("utils", SENTINEL)
63+
sys.modules.pop("mock_data", SENTINEL)
64+
65+
4666
@pytest.mark.parametrize("directory", notebook_scripts)
47-
def test_notebook_script(directory: Path):
67+
def test_notebook_script(directory: Path, purge_notebook_modules):
4868
# Run in native directory with modified sys.path for imports to work
4969
with _cwd(notebook_dir / directory):
5070
runpy.run_path(str(notebook_dir / directory / "example.py"), run_name="testing")

0 commit comments

Comments
 (0)