diff --git a/testing/_py/test_local.py b/testing/_py/test_local.py index 91b14aa2e..70501b81f 100644 --- a/testing/_py/test_local.py +++ b/testing/_py/test_local.py @@ -618,6 +618,7 @@ class TestLocalPath(CommonFSTests): @skiponwin32 def test_chdir_gone(self, path1): + original_cwd = os.getcwd() p = path1.ensure("dir_to_be_removed", dir=1) p.chdir() p.remove() @@ -628,15 +629,18 @@ class TestLocalPath(CommonFSTests): with pytest.raises(error.ENOENT): with p.as_cwd(): raise NotImplementedError + os.chdir(original_cwd) @skiponwin32 def test_chdir_gone_in_as_cwd(self, path1): + original_cwd = os.getcwd() p = path1.ensure("dir_to_be_removed", dir=1) p.chdir() p.remove() with path1.as_cwd() as old: assert old is None + os.chdir(original_cwd) def test_as_cwd(self, path1): dir = path1.ensure("subdir", dir=1) diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 429fb4e43..1c614dadd 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -6,6 +6,7 @@ import types import pytest from _pytest.config import ExitCode +from _pytest.monkeypatch import MonkeyPatch from _pytest.pathlib import symlink_or_skip from _pytest.pytester import Pytester @@ -651,7 +652,7 @@ class TestInvocationVariants: result.stderr.fnmatch_lines(["*not*found*test_missing*"]) def test_cmdline_python_namespace_package( - self, pytester: Pytester, monkeypatch + self, pytester: Pytester, monkeypatch: MonkeyPatch ) -> None: """Test --pyargs option with namespace packages (#1567). @@ -731,6 +732,7 @@ class TestInvocationVariants: result.stdout.fnmatch_lines( ["*test_world.py::test_other*PASSED*", "*1 passed*"] ) + monkeypatch.undo() def test_invoke_test_and_doctestmodules(self, pytester: Pytester) -> None: p = pytester.makepyfile( diff --git a/testing/test_argcomplete.py b/testing/test_argcomplete.py index 8c10e230b..a863947d8 100644 --- a/testing/test_argcomplete.py +++ b/testing/test_argcomplete.py @@ -67,22 +67,21 @@ class FilesCompleter: class TestArgComplete: @pytest.mark.skipif("sys.platform in ('win32', 'darwin')") - def test_compare_with_compgen( - self, tmp_path: Path, monkeypatch: MonkeyPatch - ) -> None: + def test_compare_with_compgen(self, tmp_path: Path) -> None: from _pytest._argcomplete import FastFilesCompleter ffc = FastFilesCompleter() fc = FilesCompleter() - monkeypatch.chdir(tmp_path) + with MonkeyPatch.context() as mp: + mp.chdir(tmp_path) - assert equal_with_bash("", ffc, fc, out=sys.stdout) + assert equal_with_bash("", ffc, fc, out=sys.stdout) - tmp_path.cwd().joinpath("data").touch() + tmp_path.cwd().joinpath("data").touch() - for x in ["d", "data", "doesnotexist", ""]: - assert equal_with_bash(x, ffc, fc, out=sys.stdout) + for x in ["d", "data", "doesnotexist", ""]: + assert equal_with_bash(x, ffc, fc, out=sys.stdout) @pytest.mark.skipif("sys.platform in ('win32', 'darwin')") def test_remove_dir_prefix(self): diff --git a/testing/test_collection.py b/testing/test_collection.py index ca2e2b731..56e97e92c 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -244,7 +244,7 @@ class TestCollectFS: rec = pytester.inline_run("xyz123/test_2.py") rec.assertoutcome(failed=1) - def test_testpaths_ini(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: + def test_testpaths_ini(self, pytester: Pytester) -> None: pytester.makeini( """ [pytest] @@ -275,10 +275,11 @@ class TestCollectFS: # changing cwd to each subdirectory and running pytest without # arguments collects the tests in that directory normally - for dirname in ("a", "b", "c"): - monkeypatch.chdir(pytester.path.joinpath(dirname)) - items, reprec = pytester.inline_genitems() - assert [x.name for x in items] == ["test_%s" % dirname] + with MonkeyPatch.context() as mp: + for dirname in ("a", "b", "c"): + mp.chdir(pytester.path.joinpath(dirname)) + items, reprec = pytester.inline_genitems() + assert [x.name for x in items] == ["test_%s" % dirname] class TestCollectPluginHookRelay: @@ -660,7 +661,7 @@ class Test_getinitialnodes: for parent in col.listchain(): assert parent.config is config - def test_pkgfile(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: + def test_pkgfile(self, pytester: Pytester) -> None: """Verify nesting when a module is within a package. The parent chain should match: Module -> Package -> Session. Session's parent should always be None. @@ -669,7 +670,7 @@ class Test_getinitialnodes: subdir = tmp_path.joinpath("subdir") x = ensure_file(subdir / "x.py") ensure_file(subdir / "__init__.py") - with monkeypatch.context() as mp: + with MonkeyPatch.context() as mp: mp.chdir(subdir) config = pytester.parseconfigure(x) col = pytester.getnode(config, x)