Skip to content

Commit 9477f59

Browse files
authored
Merge pull request #2173 from jeffwidman/patch-1
Switch monkeypatch fixture to yield syntax
2 parents b769e41 + 6d81c68 commit 9477f59

File tree

3 files changed

+8
-13
lines changed

3 files changed

+8
-13
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Janne Vanhala
7171
Jason R. Coombs
7272
Javier Domingo Cansino
7373
Javier Romero
74+
Jeff Widman
7475
John Towler
7576
Jon Sonesen
7677
Jordan Guymon

_pytest/monkeypatch.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
@pytest.fixture
14-
def monkeypatch(request):
14+
def monkeypatch():
1515
"""The returned ``monkeypatch`` fixture provides these
1616
helper methods to modify objects, dictionaries or os.environ::
1717
@@ -30,8 +30,8 @@ def monkeypatch(request):
3030
will be raised if the set/deletion operation has no target.
3131
"""
3232
mpatch = MonkeyPatch()
33-
request.addfinalizer(mpatch.undo)
34-
return mpatch
33+
yield mpatch
34+
mpatch.undo()
3535

3636

3737
def resolve(name):

testing/test_monkeypatch.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@
77

88

99
@pytest.fixture
10-
def mp(request):
10+
def mp():
1111
cwd = os.getcwd()
1212
sys_path = list(sys.path)
13-
14-
def cleanup():
15-
sys.path[:] = sys_path
16-
os.chdir(cwd)
17-
18-
request.addfinalizer(cleanup)
19-
return MonkeyPatch()
13+
yield MonkeyPatch()
14+
sys.path[:] = sys_path
15+
os.chdir(cwd)
2016

2117

2218
def test_setattr():
@@ -329,5 +325,3 @@ def test_issue1338_name_resolving():
329325
monkeypatch.delattr('requests.sessions.Session.request')
330326
finally:
331327
monkeypatch.undo()
332-
333-

0 commit comments

Comments
 (0)