Skip to content

Commit

Permalink
BUG: Tear down common import names in notebook tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob-Stevens-Haas committed Jul 7, 2022
1 parent 3645b55 commit 285b02c
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion test/test_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,28 @@ def _cwd(directory):
os.chdir(cwd)


@pytest.fixture
def purge_notebook_modules():
"""Remove notebook modules from sys.modules after test.
Because these modules share common names in each notebook and
module names have a system-wide scope, import machinery will not
import new modules for successive notebooks unless old modules of
same name are removed from sys.modules.
This might be better served by fixing imports in notebooks using
importlib.
"""
SENTINEL = object()
sys.modules.pop("utils", SENTINEL)
sys.modules.pop("mock_data", SENTINEL)
yield
sys.modules.pop("utils", SENTINEL)
sys.modules.pop("mock_data", SENTINEL)


@pytest.mark.parametrize("directory", notebook_scripts)
def test_notebook_script(directory: Path):
def test_notebook_script(directory: Path, purge_notebook_modules):
# Run in native directory with modified sys.path for imports to work
with _cwd(notebook_dir / directory):
runpy.run_path(str(notebook_dir / directory / "example.py"), run_name="testing")
Expand Down

0 comments on commit 285b02c

Please sign in to comment.