summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Collins <robertc@robertcollins.net>2015-06-22 12:22:01 +1200
committerRobert Collins <robertc@robertcollins.net>2015-06-22 12:23:44 +1200
commit670b2a8f1d202e26e9eb6185cc9b7cc631b8e7b7 (patch)
tree9a2bdabc035b788aed007416445c04bf1d9c3ae8
parent08be09df430c5175fc7022dba18c7684d3f94389 (diff)
downloadfixtures-git-670b2a8f1d202e26e9eb6185cc9b7cc631b8e7b7.tar.gz
Fine tune the mock patch.
-rw-r--r--README31
-rw-r--r--fixtures/_fixtures/mockpatch.py15
-rw-r--r--fixtures/tests/_fixtures/test_mockpatch.py6
-rw-r--r--requirements.txt1
-rw-r--r--test-requirements.txt1
5 files changed, 41 insertions, 13 deletions
diff --git a/README b/README
index ecb8980..3c96032 100644
--- a/README
+++ b/README
@@ -300,7 +300,8 @@ Stock Fixtures
In addition to the Fixture, FunctionFixture and MethodFixture classes fixtures
includes a number of precanned fixtures. The API docs for fixtures will list
-the complete set of these, should the dcs be out of date or not to hand.
+the complete set of these, should the dcs be out of date or not to hand. For
+the complete feature set of each fixture please see the API docs.
ByteStream
++++++++++
@@ -340,6 +341,34 @@ tests.
>>> from testtools.compat import BytesIO
>>> fixture = fixtures.FakePopen(lambda _:{'stdout': BytesIO('foobar')})
+MockPatchObject
++++++++++++++++
+
+Adapts ``mock.patch.object`` to be used as a Fixture.
+
+ >>> class Fred:
+ ... value = 1
+ >>> fixture = fixtures.MockPatchObject(Fred, 'value', 2)
+ >>> with fixture:
+ ... Fred().value
+ 2
+ >>> Fred().value
+ 1
+
+MockPatch
++++++++++
+
+Adapts ``mock.patch`` to be used as a Fixture.
+
+ >>> fixture = fixtures.MockPatch('subprocess.Popen.returncode', 3)
+
+MockPatchMultiple
++++++++++++++++++
+
+Adapts ``mock.patch.multiple`` to be used as a Fixture.
+
+ >>> fixture = fixtures.MockPatch('subprocess.Popen', returncode=3)
+
MonkeyPatch
+++++++++++
diff --git a/fixtures/_fixtures/mockpatch.py b/fixtures/_fixtures/mockpatch.py
index 12d86af..bdf1c03 100644
--- a/fixtures/_fixtures/mockpatch.py
+++ b/fixtures/_fixtures/mockpatch.py
@@ -15,12 +15,13 @@
# License for the specific language governing permissions and limitations
# under the License.
+import extras
+
import fixtures
-try:
- from unittest import mock
-except ImportError:
- import mock
+mock = extras.try_imports(['unittest.mock', 'mock'], None)
+mock_default = extras.try_imports(
+ ['unittest.mock.DEFAULT', 'mock.DEFAULT'], None)
class _Base(fixtures.Fixture):
@@ -34,7 +35,7 @@ class _Base(fixtures.Fixture):
class MockPatchObject(_Base):
"""Deal with code around mock."""
- def __init__(self, obj, attr, new=mock.DEFAULT, **kwargs):
+ def __init__(self, obj, attr, new=mock_default, **kwargs):
super(MockPatchObject, self).__init__()
self._get_p = lambda: mock.patch.object(obj, attr, new, **kwargs)
@@ -42,7 +43,7 @@ class MockPatchObject(_Base):
class MockPatch(_Base):
"""Deal with code around mock.patch."""
- def __init__(self, obj, new=mock.DEFAULT, **kwargs):
+ def __init__(self, obj, new=mock_default, **kwargs):
super(MockPatch, self).__init__()
self._get_p = lambda: mock.patch(obj, new, **kwargs)
@@ -52,7 +53,7 @@ class MockPatchMultiple(_Base):
# Default value to trigger a MagicMock to be created for a named
# attribute.
- DEFAULT = mock.DEFAULT
+ DEFAULT = mock_default
def __init__(self, obj, **kwargs):
"""Initialize the mocks
diff --git a/fixtures/tests/_fixtures/test_mockpatch.py b/fixtures/tests/_fixtures/test_mockpatch.py
index 75d70fe..4f48aa9 100644
--- a/fixtures/tests/_fixtures/test_mockpatch.py
+++ b/fixtures/tests/_fixtures/test_mockpatch.py
@@ -12,11 +12,9 @@
# License for the specific language governing permissions and limitations
# under the License.
-try:
- from unittest import mock
-except ImportError:
- import mock
+import extras
+mock = extras.try_imports(['unittest.mock', 'mock'], None)
import testtools
from fixtures import (
diff --git a/requirements.txt b/requirements.txt
index cc78151..f9efc61 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,3 +1,2 @@
pbr>=0.11
testtools>=0.9.22
-mock
diff --git a/test-requirements.txt b/test-requirements.txt
new file mode 100644
index 0000000..e789b5f
--- /dev/null
+++ b/test-requirements.txt
@@ -0,0 +1 @@
+mock;python_version<'3.3'