summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2014-09-05 16:32:16 +0000
committerGerrit Code Review <review@openstack.org>2014-09-05 16:32:16 +0000
commitbbfde1e29c72ee7f90b2982e29b47cd2e1574d6d (patch)
treeb042018fbf083ffdcd6c7b61a054dfb83d5289d0
parent47b55387a8e6e20f0d884333333acf187368c6f9 (diff)
parent4be5bc3b897d471779e4eb0e0b097747eade425e (diff)
downloadoslotest-bbfde1e29c72ee7f90b2982e29b47cd2e1574d6d.tar.gz
Merge "Add fixture for mock.patch.multiple"1.1.0.0a21.1.0
-rw-r--r--doc/source/api.rst1
-rw-r--r--oslotest/mockpatch.py31
-rw-r--r--tests/unit/test_mockpatch.py14
3 files changed, 45 insertions, 1 deletions
diff --git a/doc/source/api.rst b/doc/source/api.rst
index fc873d4..d64c037 100644
--- a/doc/source/api.rst
+++ b/doc/source/api.rst
@@ -15,6 +15,7 @@ oslotest.mockpatch
.. automodule:: oslotest.mockpatch
:members:
+ :special-members:
oslotest.moxstubout
===================
diff --git a/oslotest/mockpatch.py b/oslotest/mockpatch.py
index cf4c735..2144ea2 100644
--- a/oslotest/mockpatch.py
+++ b/oslotest/mockpatch.py
@@ -36,7 +36,6 @@ class PatchObject(fixtures.Fixture):
class Patch(fixtures.Fixture):
-
"""Deal with code around mock.patch."""
def __init__(self, obj, new=mock.DEFAULT, **kwargs):
@@ -49,3 +48,33 @@ class Patch(fixtures.Fixture):
_p = mock.patch(self.obj, self.new, **self.kwargs)
self.mock = _p.start()
self.addCleanup(_p.stop)
+
+
+class Multiple(fixtures.Fixture):
+ """Deal with code around mock.patch.multiple."""
+
+ # Default value to trigger a MagicMock to be created for a named
+ # attribute.
+ DEFAULT = mock.DEFAULT
+
+ def __init__(self, obj, **kwargs):
+ """Initialize the mocks
+
+ Pass name=value to replace obj.name with value.
+
+ Pass name=Multiple.DEFAULT to replace obj.name with a
+ MagicMock instance.
+
+ :param obj: Object or name containing values being mocked.
+ :type obj: str or object
+ :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.mock = _p.start()
+ self.addCleanup(_p.stop)
diff --git a/tests/unit/test_mockpatch.py b/tests/unit/test_mockpatch.py
index d3bc12d..fb3a476 100644
--- a/tests/unit/test_mockpatch.py
+++ b/tests/unit/test_mockpatch.py
@@ -41,6 +41,20 @@ class TestMockPatch(base.BaseTestCase):
self.assertIsInstance(instance.bar(), mock.MagicMock)
+class TestMockMultiple(base.BaseTestCase):
+ def test_mock_multiple_with_replacement(self):
+ self.useFixture(mockpatch.Multiple('%s.Foo' % (__name__),
+ bar=mocking_bar))
+ instance = Foo()
+ self.assertEqual(instance.bar(), 'mocked!')
+
+ def test_mock_patch_without_replacement(self):
+ self.useFixture(mockpatch.Multiple('%s.Foo' % (__name__),
+ bar=mockpatch.Multiple.DEFAULT))
+ instance = Foo()
+ self.assertIsInstance(instance.bar(), mock.MagicMock)
+
+
class TestMockPatchObject(base.BaseTestCase):
def test_mock_patch_object_with_replacement(self):
self.useFixture(mockpatch.PatchObject(Foo, 'bar', mocking_bar))