File tree Expand file tree Collapse file tree 1 file changed +21
-1
lines changed
Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Original file line number Diff line number Diff 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" )
You can’t perform that action at this time.
0 commit comments