From 303b6e32ea8478c7fb8e813a1fdab98ad131bd36 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Fri, 5 Aug 2022 23:51:18 +0200 Subject: [PATCH] Make sure `monkeypatch.undo()` is always called --- src/_pytest/monkeypatch.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/_pytest/monkeypatch.py b/src/_pytest/monkeypatch.py index 2c4320656..e1231e97a 100644 --- a/src/_pytest/monkeypatch.py +++ b/src/_pytest/monkeypatch.py @@ -45,9 +45,8 @@ def monkeypatch() -> Generator["MonkeyPatch", None, None]: fixture has finished. The ``raising`` parameter determines if a KeyError or AttributeError will be raised if the set/deletion operation has no target. """ - mpatch = MonkeyPatch() - yield mpatch - mpatch.undo() + with MonkeyPatch.context() as mpatch: + yield mpatch def resolve(name: str) -> object: