From 5d9f74b89fd037fc1c14b909a98371b0b4eafd02 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 19 Oct 2022 22:07:53 -0400 Subject: [PATCH] fix test pollution of sys.modules --- testing/_py/test_local.py | 6 ++++++ testing/test_pathlib.py | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/testing/_py/test_local.py b/testing/_py/test_local.py index 6e2c44bc2e0..995d739cec6 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,11 @@ def test_error_preservation(self, path1): class TestImport: + @pytest.fixture(autouse=True) + def preserve_sys_modules(self): + with mock.patch.dict(sys.modules): + pass + 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..9e4e727e4ac 100644 --- a/testing/test_pathlib.py +++ b/testing/test_pathlib.py @@ -91,6 +91,11 @@ 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_modules(self): + with unittest.mock.patch.dict(sys.modules): + yield + def setuptestfs(self, path: Path) -> None: # print "setting up test fs for", repr(path) samplefile = path / "samplefile"