Skip to content

Commit

Permalink
Merge pull request #2173 from jeffwidman/patch-1
Browse files Browse the repository at this point in the history
Switch monkeypatch fixture to yield syntax
  • Loading branch information
nicoddemus authored Jan 5, 2017
2 parents b769e41 + 6d81c68 commit 9477f59
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Janne Vanhala
Jason R. Coombs
Javier Domingo Cansino
Javier Romero
Jeff Widman
John Towler
Jon Sonesen
Jordan Guymon
Expand Down
6 changes: 3 additions & 3 deletions _pytest/monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand All @@ -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):
Expand Down
14 changes: 4 additions & 10 deletions testing/test_monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -329,5 +325,3 @@ def test_issue1338_name_resolving():
monkeypatch.delattr('requests.sessions.Session.request')
finally:
monkeypatch.undo()


0 comments on commit 9477f59

Please sign in to comment.