Merge pull request #3382 from feuillemorte/3290-improve-monkeypatch

#3290 improve monkeypatch
This commit is contained in:
Bruno Oliveira
2018-04-19 17:05:52 -03:00
committed by GitHub
4 changed files with 55 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,29 @@ class MonkeyPatch(object):
self._cwd = None
self._savesyspath = None
@contextmanager
def context(self):
"""
Context manager that returns a new :class:`MonkeyPatch` object which
undoes any patching done inside the ``with`` block upon exit:
.. code-block:: python
import functools
def test_partial(monkeypatch):
with monkeypatch.context() as m:
m.setattr(functools, "partial", 3)
Useful in situations where it is desired to undo some patches before the test ends,
such as mocking ``stdlib`` functions that might break pytest itself if mocked (for examples
of this see `#3290 <https://github.com/pytest-dev/pytest/issues/3290>`_.
"""
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.