diff --git a/testing/_py/test_local.py b/testing/_py/test_local.py index 6e2c44bc2e0..ceacd70ae2e 100644 --- a/testing/_py/test_local.py +++ b/testing/_py/test_local.py @@ -2,6 +2,7 @@ import os import sys import time +from unittest import mock import pytest from py import error @@ -978,6 +979,12 @@ def test_error_preservation(self, path1): class TestImport: + @pytest.fixture(autouse=True) + def preserve_sys(self): + with mock.patch.dict(sys.modules): + with mock.patch.object(sys, "path", list(sys.path)): + yield + def test_pyimport(self, path1): obj = path1.join("execfile.py").pyimport() assert obj.x == 42 diff --git a/testing/test_pathlib.py b/testing/test_pathlib.py index c901dc6f435..577c7749fd9 100644 --- a/testing/test_pathlib.py +++ b/testing/test_pathlib.py @@ -91,6 +91,12 @@ def path1(self, tmp_path_factory: TempPathFactory) -> Generator[Path, None, None yield path assert path.joinpath("samplefile").exists() + @pytest.fixture(autouse=True) + def preserve_sys(self): + with unittest.mock.patch.dict(sys.modules): + with unittest.mock.patch.object(sys, "path", list(sys.path)): + yield + def setuptestfs(self, path: Path) -> None: # print "setting up test fs for", repr(path) samplefile = path / "samplefile"