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.