Skip to content

Switch monkeypatch fixture to yield syntax #2173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()