From 6d81c684ccdf502e5e76c04d63e3229e1e5188a2 Mon Sep 17 00:00:00 2001 From: Jeff Widman Date: Wed, 4 Jan 2017 05:31:44 -0800 Subject: [PATCH] Switch monkeypatch fixture to yield syntax --- AUTHORS | 1 + _pytest/monkeypatch.py | 6 +++--- testing/test_monkeypatch.py | 14 ++++---------- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/AUTHORS b/AUTHORS index 8c6fa1b5d78..e3bbdc5d100 100644 --- a/AUTHORS +++ b/AUTHORS @@ -71,6 +71,7 @@ Janne Vanhala Jason R. Coombs Javier Domingo Cansino Javier Romero +Jeff Widman John Towler Jon Sonesen Jordan Guymon diff --git a/_pytest/monkeypatch.py b/_pytest/monkeypatch.py index 852e72beda5..2651cf04421 100644 --- a/_pytest/monkeypatch.py +++ b/_pytest/monkeypatch.py @@ -11,7 +11,7 @@ @pytest.fixture -def monkeypatch(request): +def monkeypatch(): """The returned ``monkeypatch`` fixture provides these helper methods to modify objects, dictionaries or os.environ:: @@ -30,8 +30,8 @@ def monkeypatch(request): will be raised if the set/deletion operation has no target. """ mpatch = MonkeyPatch() - request.addfinalizer(mpatch.undo) - return mpatch + yield mpatch + mpatch.undo() def resolve(name): diff --git a/testing/test_monkeypatch.py b/testing/test_monkeypatch.py index 3fcd20f321b..9d02e2cc0b6 100644 --- a/testing/test_monkeypatch.py +++ b/testing/test_monkeypatch.py @@ -7,16 +7,12 @@ @pytest.fixture -def mp(request): +def mp(): cwd = os.getcwd() sys_path = list(sys.path) - - def cleanup(): - sys.path[:] = sys_path - os.chdir(cwd) - - request.addfinalizer(cleanup) - return MonkeyPatch() + yield MonkeyPatch() + sys.path[:] = sys_path + os.chdir(cwd) def test_setattr(): @@ -329,5 +325,3 @@ def test_issue1338_name_resolving(): monkeypatch.delattr('requests.sessions.Session.request') finally: monkeypatch.undo() - -