diff options
author | Doug Hellmann <doug@doughellmann.com> | 2014-09-04 11:02:17 -0400 |
---|---|---|
committer | Doug Hellmann <doug@doughellmann.com> | 2014-09-05 08:49:10 -0400 |
commit | 4be5bc3b897d471779e4eb0e0b097747eade425e (patch) | |
tree | 9f3eba15e8533d75aa39d9347b8a7a07ebd1888c | |
parent | 33c0c00ffac49430adacd5430ee894279e76931d (diff) | |
download | oslotest-4be5bc3b897d471779e4eb0e0b097747eade425e.tar.gz |
Add fixture for mock.patch.multiple
Add another fixture to support mock.patch.multiple() so that test suites
using that function don't have to import mock directly.
Change-Id: I58368dddda347410459d8f5a713e7395253c3697
-rw-r--r-- | doc/source/api.rst | 1 | ||||
-rw-r--r-- | oslotest/mockpatch.py | 31 | ||||
-rw-r--r-- | tests/unit/test_mockpatch.py | 14 |
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)) |