-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
case>=1.3.1 | ||
pytest>=3.0,<=5.3.5 | ||
pytest-sugar>=0.9.1 | ||
pytest-rerunfailures>=6.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from unittest.mock import Mock | ||
|
||
class _ContextMock(Mock): | ||
"""Dummy class implementing __enter__ and __exit__ | ||
as the :keyword:`with` statement requires these to be implemented | ||
in the class, not just the instance.""" | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, *exc_info): | ||
pass | ||
|
||
|
||
def ContextMock(*args, **kwargs): | ||
"""Mock that mocks :keyword:`with` statement contexts.""" | ||
obj = _ContextMock(*args, **kwargs) | ||
obj.attach_mock(_ContextMock(), '__enter__') | ||
obj.attach_mock(_ContextMock(), '__exit__') | ||
obj.__enter__.return_value = obj | ||
# if __exit__ return a value the exception is ignored, | ||
# so it must return None here. | ||
obj.__exit__.return_value = None | ||
return obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from unittest.mock import MagicMock | ||
import pytest | ||
|
||
sentinel = object() | ||
|
||
class _patching(object): | ||
|
||
def __init__(self, monkeypatch, request): | ||
self.monkeypatch = monkeypatch | ||
self.request = request | ||
|
||
def __getattr__(self, name): | ||
return getattr(self.monkeypatch, name) | ||
|
||
def __call__(self, path, value=sentinel, name=None, | ||
new=MagicMock, **kwargs): | ||
value = self._value_or_mock(value, new, name, path, **kwargs) | ||
self.monkeypatch.setattr(path, value) | ||
return value | ||
|
||
def object(self, target, attribute, *args, **kwargs): | ||
return _wrap_context( | ||
patch.object(target, attribute, *args, **kwargs), | ||
self.request) | ||
|
||
def _value_or_mock(self, value, new, name, path, **kwargs): | ||
if value is sentinel: | ||
value = new(name=name or path.rpartition('.')[2]) | ||
for k, v in kwargs.items(): | ||
setattr(value, k, v) | ||
return value | ||
|
||
def setattr(self, target, name=sentinel, value=sentinel, **kwargs): | ||
# alias to __call__ with the interface of pytest.monkeypatch.setattr | ||
if value is sentinel: | ||
value, name = name, None | ||
return self(target, value, name=name) | ||
|
||
def setitem(self, dic, name, value=sentinel, new=MagicMock, **kwargs): | ||
# same as pytest.monkeypatch.setattr but default value is MagicMock | ||
value = self._value_or_mock(value, new, name, dic, **kwargs) | ||
self.monkeypatch.setitem(dic, name, value) | ||
return value | ||
|
||
def modules(self, *mods): | ||
modules = [] | ||
for mod in mods: | ||
mod = mod.split('.') | ||
modules.extend(reversed([ | ||
'.'.join(mod[:-i] if i else mod) for i in range(len(mod)) | ||
])) | ||
modules = sorted(set(modules)) | ||
return _wrap_context(mock.module(*modules), self.request) | ||
|
||
|
||
def _wrap_context(context, request): | ||
ret = context.__enter__() | ||
|
||
def fin(): | ||
context.__exit__(*sys.exc_info()) | ||
request.addfinalizer(fin) | ||
return ret | ||
|
||
|
||
@pytest.fixture() | ||
def patching(monkeypatch, request): | ||
"""Monkeypath.setattr shortcut. | ||
Example: | ||
.. code-block:: python | ||
def test_foo(patching): | ||
# execv value here will be mock.MagicMock by default. | ||
execv = patching('os.execv') | ||
patching('sys.platform', 'darwin') # set concrete value | ||
patching.setenv('DJANGO_SETTINGS_MODULE', 'x.settings') | ||
# val will be of type mock.MagicMock by default | ||
val = patching.setitem('path.to.dict', 'KEY') | ||
""" | ||
return _patching(monkeypatch, request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters