summaryrefslogtreecommitdiff
path: root/t/unit/conftest.py
diff options
context:
space:
mode:
Diffstat (limited to 't/unit/conftest.py')
-rw-r--r--t/unit/conftest.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/t/unit/conftest.py b/t/unit/conftest.py
new file mode 100644
index 0000000..9a59124
--- /dev/null
+++ b/t/unit/conftest.py
@@ -0,0 +1,54 @@
+from unittest.mock import MagicMock
+import pytest
+
+sentinel = object()
+
+class _patching:
+
+ 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 _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
+
+
+@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)