#3290 Improved monkeypatch to support some form of with statement
This commit is contained in:
parent
2962c7367c
commit
659c044372
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue