Fine tune the mock patch.
This commit is contained in:
parent
08be09df43
commit
670b2a8f1d
31
README
31
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
|
||||
+++++++++++
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 (
|
||||
|
|
|
@ -1,3 +1,2 @@
|
|||
pbr>=0.11
|
||||
testtools>=0.9.22
|
||||
mock
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
mock;python_version<'3.3'
|
Loading…
Reference in New Issue