summaryrefslogtreecommitdiff
path: root/fixtures/tests
diff options
context:
space:
mode:
authorJulien Danjou <julien@danjou.info>2015-02-09 15:21:11 +0100
committerRobert Collins <robertc@robertcollins.net>2015-06-22 11:52:54 +1200
commit08be09df430c5175fc7022dba18c7684d3f94389 (patch)
tree5f3fd93b5152038e0e61e88bd5401ea4dd5b2711 /fixtures/tests
parent8baf138829febc3a99a80003756a9631f5bad031 (diff)
downloadfixtures-git-08be09df430c5175fc7022dba18c7684d3f94389.tar.gz
Add a new mockpatch fixture
This fixture provides an easy usage for mock (unittest.mock in Python 3).
Diffstat (limited to 'fixtures/tests')
-rw-r--r--fixtures/tests/_fixtures/__init__.py1
-rw-r--r--fixtures/tests/_fixtures/test_mockpatch.py74
2 files changed, 75 insertions, 0 deletions
diff --git a/fixtures/tests/_fixtures/__init__.py b/fixtures/tests/_fixtures/__init__.py
index e4d9403..1b4c359 100644
--- a/fixtures/tests/_fixtures/__init__.py
+++ b/fixtures/tests/_fixtures/__init__.py
@@ -17,6 +17,7 @@ def load_tests(loader, standard_tests, pattern):
test_modules = [
'environ',
'logger',
+ 'mockpatch',
'monkeypatch',
'packagepath',
'popen',
diff --git a/fixtures/tests/_fixtures/test_mockpatch.py b/fixtures/tests/_fixtures/test_mockpatch.py
new file mode 100644
index 0000000..75d70fe
--- /dev/null
+++ b/fixtures/tests/_fixtures/test_mockpatch.py
@@ -0,0 +1,74 @@
+# Copyright 2014 IBM Corp.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+try:
+ from unittest import mock
+except ImportError:
+ import mock
+
+import testtools
+
+from fixtures import (
+ MockPatch,
+ MockPatchMultiple,
+ MockPatchObject,
+)
+
+
+class Foo(object):
+ def bar(self):
+ return self
+
+
+def mocking_bar(self):
+ return 'mocked!'
+
+
+class TestMockPatch(testtools.TestCase):
+ def test_mock_patch_with_replacement(self):
+ self.useFixture(MockPatch('%s.Foo.bar' % (__name__), mocking_bar))
+ instance = Foo()
+ self.assertEqual(instance.bar(), 'mocked!')
+
+ def test_mock_patch_without_replacement(self):
+ self.useFixture(MockPatch('%s.Foo.bar' % (__name__)))
+ instance = Foo()
+ self.assertIsInstance(instance.bar(), mock.MagicMock)
+
+
+class TestMockMultiple(testtools.TestCase):
+ def test_mock_multiple_with_replacement(self):
+ self.useFixture(MockPatchMultiple('%s.Foo' % (__name__),
+ bar=mocking_bar))
+ instance = Foo()
+ self.assertEqual(instance.bar(), 'mocked!')
+
+ def test_mock_patch_without_replacement(self):
+ self.useFixture(MockPatchMultiple(
+ '%s.Foo' % (__name__),
+ bar=MockPatchMultiple.DEFAULT))
+ instance = Foo()
+ self.assertIsInstance(instance.bar(), mock.MagicMock)
+
+
+class TestMockPatchObject(testtools.TestCase):
+ def test_mock_patch_object_with_replacement(self):
+ self.useFixture(MockPatchObject(Foo, 'bar', mocking_bar))
+ instance = Foo()
+ self.assertEqual(instance.bar(), 'mocked!')
+
+ def test_mock_patch_object_without_replacement(self):
+ self.useFixture(MockPatchObject(Foo, 'bar'))
+ instance = Foo()
+ self.assertIsInstance(instance.bar(), mock.MagicMock)