Skip to content

Commit a962a0c

Browse files
committed
New mocker.mock_module variable points to the underlying mock module being used
Fix pytest-dev#71
1 parent f2c7b77 commit a962a0c

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

CHANGELOG.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
1.5.0
2+
-----
3+
4+
* New ``mocker.mock_module`` variable points to the underlying mock module being used
5+
(``unittest.mock`` or ``mock``).
6+
Thanks `@blueyed`_ for the request (`#71`_).
7+
8+
.. _#71: https://github.com/pytest-dev/pytest-mock/pull/71
9+
110
1.4.0
211
-----
312

413
* New configuration variable, ``mock_use_standalone_module`` (defaults to ``False``). This forces
514
the plugin to import ``mock`` instead of ``unittest.mock`` on Python 3. This is useful to import
6-
and use a newer version than the one available in the Python distribution.
15+
a newer version than the one available in the Python distribution.
716

817
* Previously the plugin would first try to import ``mock`` and fallback to ``unittest.mock``
918
in case of an ``ImportError``, but this behavior has been removed because it could hide

pytest_mock.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import sys
33

44
import pytest
5-
65
from _pytest_mock_version import version
6+
77
__version__ = version
88

99

@@ -34,7 +34,7 @@ class MockFixture(object):
3434
def __init__(self, config):
3535
self._patches = [] # list of mock._patch objects
3636
self._mocks = [] # list of MagicMock objects
37-
self._mock_module = mock_module = _get_mock_module(config)
37+
self.mock_module = mock_module = _get_mock_module(config)
3838
self.patch = self._Patcher(self._patches, self._mocks, mock_module)
3939
# aliases for convenience
4040
self.Mock = mock_module.Mock
@@ -104,7 +104,7 @@ def stub(self, name=None):
104104
:rtype: mock.MagicMock
105105
:return: Stub object.
106106
"""
107-
return self._mock_module.MagicMock(spec=lambda *args, **kwargs: None, name=name)
107+
return self.mock_module.MagicMock(spec=lambda *args, **kwargs: None, name=name)
108108

109109
class _Patcher(object):
110110
"""
@@ -115,7 +115,7 @@ class _Patcher(object):
115115
def __init__(self, patches, mocks, mock_module):
116116
self._patches = patches
117117
self._mocks = mocks
118-
self._mock_module = mock_module
118+
self.mock_module = mock_module
119119

120120
def _start_patch(self, mock_func, *args, **kwargs):
121121
"""Patches something by calling the given function from the mock
@@ -130,20 +130,20 @@ def _start_patch(self, mock_func, *args, **kwargs):
130130

131131
def object(self, *args, **kwargs):
132132
"""API to mock.patch.object"""
133-
return self._start_patch(self._mock_module.patch.object, *args, **kwargs)
133+
return self._start_patch(self.mock_module.patch.object, *args, **kwargs)
134134

135135
def multiple(self, *args, **kwargs):
136136
"""API to mock.patch.multiple"""
137-
return self._start_patch(self._mock_module.patch.multiple, *args,
137+
return self._start_patch(self.mock_module.patch.multiple, *args,
138138
**kwargs)
139139

140140
def dict(self, *args, **kwargs):
141141
"""API to mock.patch.dict"""
142-
return self._start_patch(self._mock_module.patch.dict, *args, **kwargs)
142+
return self._start_patch(self.mock_module.patch.dict, *args, **kwargs)
143143

144144
def __call__(self, *args, **kwargs):
145145
"""API to mock.patch"""
146-
return self._start_patch(self._mock_module.patch, *args, **kwargs)
146+
return self._start_patch(self.mock_module.patch, *args, **kwargs)
147147

148148

149149
@pytest.yield_fixture

0 commit comments

Comments
 (0)