summaryrefslogtreecommitdiff
path: root/oslotest
diff options
context:
space:
mode:
authorJulien Danjou <julien@danjou.info>2015-03-12 14:39:42 +0100
committerJulien Danjou <julien@danjou.info>2015-04-03 16:43:21 +0200
commit247817014dcc1e7fa7ec76c16252bcb876d157a6 (patch)
tree5b20f53686aceb532f4d6fbc64507c01457185e0 /oslotest
parentfd60b9c02537e8c3e630fcdc8813fd26d1ab6e27 (diff)
downloadoslotest-247817014dcc1e7fa7ec76c16252bcb876d157a6.tar.gz
mockpatch: factorize code
This creates a base class and simplify the rest of the inheriting classes code. Change-Id: I010e8720766a3cbb5e3832c1e15385962c576721
Diffstat (limited to 'oslotest')
-rw-r--r--oslotest/mockpatch.py47
1 files changed, 17 insertions, 30 deletions
diff --git a/oslotest/mockpatch.py b/oslotest/mockpatch.py
index 7fbf0a6..fe56306 100644
--- a/oslotest/mockpatch.py
+++ b/oslotest/mockpatch.py
@@ -19,38 +19,31 @@ import fixtures
from six.moves import mock
-class PatchObject(fixtures.Fixture):
- """Deal with code around mock."""
-
- def __init__(self, obj, attr, new=mock.DEFAULT, **kwargs):
- self.obj = obj
- self.attr = attr
- self.kwargs = kwargs
- self.new = new
-
+class _Base(fixtures.Fixture):
def setUp(self):
- super(PatchObject, self).setUp()
- _p = mock.patch.object(self.obj, self.attr, self.new, **self.kwargs)
+ super(_Base, self).setUp()
+ _p = self._get_p()
self.addCleanup(_p.stop)
self.mock = _p.start()
-class Patch(fixtures.Fixture):
+class PatchObject(_Base):
+ """Deal with code around mock."""
+
+ def __init__(self, obj, attr, new=mock.DEFAULT, **kwargs):
+ super(PatchObject, self).__init__()
+ self._get_p = lambda: mock.patch.object(obj, attr, new, **kwargs)
+
+
+class Patch(_Base):
"""Deal with code around mock.patch."""
def __init__(self, obj, new=mock.DEFAULT, **kwargs):
- self.obj = obj
- self.kwargs = kwargs
- self.new = new
-
- def setUp(self):
- super(Patch, self).setUp()
- _p = mock.patch(self.obj, self.new, **self.kwargs)
- self.addCleanup(_p.stop)
- self.mock = _p.start()
+ super(Patch, self).__init__()
+ self._get_p = lambda: mock.patch(obj, new, **kwargs)
-class Multiple(fixtures.Fixture):
+class Multiple(_Base):
"""Deal with code around mock.patch.multiple."""
# Default value to trigger a MagicMock to be created for a named
@@ -70,11 +63,5 @@ class Multiple(fixtures.Fixture):
:param kwargs: names and values of attributes of obj to be mocked.
"""
- self.obj = obj
- self.kwargs = kwargs
-
- def setUp(self):
- super(Multiple, self).setUp()
- _p = mock.patch.multiple(self.obj, **self.kwargs)
- self.addCleanup(_p.stop)
- self.mock = _p.start()
+ super(Multiple, self).__init__()
+ self._get_p = lambda: mock.patch.multiple(obj, **kwargs)