From 659c044372ece204146fee9164fb6144c494eb58 Mon Sep 17 00:00:00 2001 From: feuillemorte Date: Tue, 10 Apr 2018 20:17:51 +0300 Subject: [PATCH] #3290 Improved monkeypatch to support some form of with statement --- _pytest/monkeypatch.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/_pytest/monkeypatch.py b/_pytest/monkeypatch.py index c82ffd053..126c9ca16 100644 --- a/_pytest/monkeypatch.py +++ b/_pytest/monkeypatch.py @@ -4,6 +4,8 @@ from __future__ import absolute_import, division, print_function import os import sys import re +from contextlib import contextmanager + import six from _pytest.fixtures import fixture @@ -106,6 +108,14 @@ class MonkeyPatch(object): self._cwd = None self._savesyspath = None + @contextmanager + def context(self): + m = MonkeyPatch() + try: + yield m + finally: + m.undo() + def setattr(self, target, name, value=notset, raising=True): """ Set attribute value on target, memorizing the old value. By default raise AttributeError if the attribute did not exist.