#3290 Improved monkeypatch to support some form of with statement

This commit is contained in:
feuillemorte 2018-04-10 20:17:51 +03:00
parent 2962c7367c
commit 659c044372
1 changed files with 10 additions and 0 deletions

View File

@ -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.