Skip to content

Commit 8c92792

Browse files
committed
Merge pull request #5 from nicoddemus/deprecate-mock
Deprecate mock in favor of mocker
2 parents 3b2fd83 + 68f3da8 commit 8c92792

File tree

4 files changed

+76
-31
lines changed

4 files changed

+76
-31
lines changed

README.rst

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
pytest-mock
33
===========
44

5-
This plugin installs a ``mock`` fixture which is a thin-wrapper around the patching API
5+
This plugin installs a ``mocker`` fixture which is a thin-wrapper around the patching API
66
provided by the excellent `mock <http://pypi.python.org/pypi/mock>`_ package,
77
but with the benefit of not having to worry about undoing patches at the end
88
of a test:
99

1010
.. code-block:: python
1111
1212
13-
def test_unix_fs(mock):
14-
mock.patch('os.remove')
13+
def test_unix_fs(mocker):
14+
mocker.patch('os.remove')
1515
UnixFS.rm('file')
1616
os.remove.assert_called_once_with('file')
1717
@@ -40,27 +40,38 @@ of a test:
4040
Usage
4141
=====
4242

43-
The ``mock`` fixture has the same API as
43+
The ``mocker`` fixture has the same API as
4444
`mock.patch <http://www.voidspace.org.uk/python/mock/patch.html#patch-decorators>`_,
4545
supporting the same arguments:
4646

4747
.. code-block:: python
4848
49-
def test_foo(mock):
49+
def test_foo(mocker):
5050
# all valid calls
51-
mock.patch('os.remove')
52-
mock.patch.object(os, 'listdir', autospec=True)
53-
mocked = mock.patch('os.path.isfile')
51+
mocker.patch('os.remove')
52+
mocker.patch.object(os, 'listdir', autospec=True)
53+
mocked_isfile = mocker.patch('os.path.isfile')
5454
5555
The supported methods are:
5656

57-
* ``mock.patch``: see http://www.voidspace.org.uk/python/mock/patch.html#patch.
58-
* ``mock.patch.object``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-object.
59-
* ``mock.patch.multiple``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-multiple.
60-
* ``mock.patch.dict``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-dict.
61-
* ``mock.stopall()``: stops all active patches at this point.
57+
* ``mocker.patch``: see http://www.voidspace.org.uk/python/mock/patch.html#patch.
58+
* ``mocker.patch.object``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-object.
59+
* ``mocker.patch.multiple``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-multiple.
60+
* ``mocker.patch.dict``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-dict.
61+
* ``mocker.stopall()``: stops all active patches at this point.
6262

6363

64+
Note
65+
----
66+
67+
Prior to version ``0.4.0``, the ``mocker`` fixture was named ``mock``.
68+
This was changed because naming the fixture ``mock`` conflicts with the
69+
actual ``mock`` module, which made using it awkward when access to both the
70+
module and the plugin were required within a test.
71+
72+
The old fixture ``mock`` still works, but its use is discouraged and will be
73+
removed in version ``1.0``.
74+
6475
Requirements
6576
============
6677

pytest_mock.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,23 @@ def __call__(self, *args, **kwargs):
6868

6969

7070
@pytest.yield_fixture
71-
def mock():
71+
def mocker():
7272
"""
7373
return an object that has the same interface to the `mock` module, but
7474
takes care of automatically undoing all patches after each test method.
7575
"""
7676
result = MockFixture()
7777
yield result
78-
result.stopall()
78+
result.stopall()
79+
80+
81+
@pytest.yield_fixture
82+
def mock():
83+
"""
84+
Same as "mocker", but kept only for backward compatibility.
85+
"""
86+
import warnings
87+
warnings.warn('"mock" fixture has been deprecated, use "mocker" instead',
88+
DeprecationWarning)
89+
for m in mocker():
90+
yield m

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup(
1111
name='pytest-mock',
12-
version='0.3.0',
12+
version='0.4.0',
1313
entry_points={
1414
'pytest11': ['pytest-mock = pytest_mock'],
1515
},

test_pytest_mock.py

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import pytest
44

55

6+
pytest_plugins = 'pytester'
7+
68
class UnixFS(object):
79
"""
810
Wrapper to os functions to simulate a Unix file system, used for testing
@@ -19,7 +21,7 @@ def ls(cls, path):
1921

2022

2123
@pytest.fixture
22-
def check_unix_fs_mocked(tmpdir, mock):
24+
def check_unix_fs_mocked(tmpdir, mocker):
2325
"""
2426
performs a standard test in a UnixFS, assuming that both `os.remove` and
2527
`os.listdir` have been mocked previously.
@@ -40,7 +42,7 @@ def check(mocked_rm, mocked_ls):
4042
assert UnixFS.ls(str(tmpdir)) == ['bar.txt']
4143
mocked_ls.assert_called_once_with(str(tmpdir))
4244

43-
mock.stopall()
45+
mocker.stopall()
4446

4547
assert UnixFS.ls(str(tmpdir)) == ['foo.txt']
4648
UnixFS.rm(str(file_name))
@@ -49,43 +51,63 @@ def check(mocked_rm, mocked_ls):
4951
return check
5052

5153

52-
def mock_using_patch_object(mock):
53-
return mock.patch.object(os, 'remove'), mock.patch.object(os, 'listdir')
54+
def mock_using_patch_object(mocker):
55+
return mocker.patch.object(os, 'remove'), mocker.patch.object(os, 'listdir')
5456

5557

56-
def mock_using_patch(mock):
57-
return mock.patch('os.remove'), mock.patch('os.listdir')
58+
def mock_using_patch(mocker):
59+
return mocker.patch('os.remove'), mocker.patch('os.listdir')
5860

5961

60-
def mock_using_patch_multiple(mock):
62+
def mock_using_patch_multiple(mocker):
6163
from pytest_mock import mock_module
6264

63-
r = mock.patch.multiple('os', remove=mock_module.DEFAULT,
64-
listdir=mock_module.DEFAULT)
65+
r = mocker.patch.multiple('os', remove=mock_module.DEFAULT,
66+
listdir=mock_module.DEFAULT)
6567
return r['remove'], r['listdir']
6668

6769

6870
@pytest.mark.parametrize('mock_fs', [mock_using_patch_object, mock_using_patch,
6971
mock_using_patch_multiple],
7072
)
71-
def test_mock_patches(mock_fs, mock, check_unix_fs_mocked):
73+
def test_mock_patches(mock_fs, mocker, check_unix_fs_mocked):
7274
"""
7375
Installs mocks into `os` functions and performs a standard testing of
7476
mock functionality. We parametrize different mock methods to ensure
7577
all (intended, at least) mock API is covered.
7678
"""
77-
mock_fs(mock)
78-
mocked_rm, mocked_ls = mock_fs(mock)
79+
# mock it twice on purpose to ensure we unmock it correctly later
80+
mock_fs(mocker)
81+
mocked_rm, mocked_ls = mock_fs(mocker)
7982
check_unix_fs_mocked(mocked_rm, mocked_ls)
8083

8184

82-
def test_mock_patch_dict(mock):
85+
def test_mock_patch_dict(mocker):
8386
"""
8487
Testing
8588
:param mock:
8689
"""
8790
x = {'original': 1}
88-
mock.patch.dict(x, values=[('new', 10)], clear=True)
91+
mocker.patch.dict(x, values=[('new', 10)], clear=True)
8992
assert x == {'new': 10}
90-
mock.stopall()
93+
mocker.stopall()
9194
assert x == {'original': 1}
95+
96+
97+
def test_mock_fixture_is_deprecated(testdir):
98+
"""
99+
Test that a warning emitted when using deprecated "mock" fixture.
100+
"""
101+
testdir.makepyfile('''
102+
import warnings
103+
import os
104+
warnings.simplefilter('always')
105+
106+
def test_foo(mock, tmpdir):
107+
mock.patch('os.listdir', return_value=['mocked'])
108+
assert os.listdir(str(tmpdir)) == ['mocked']
109+
mock.stopall()
110+
assert os.listdir(str(tmpdir)) == []
111+
''')
112+
result = testdir.runpytest('-s')
113+
result.stderr.fnmatch_lines(['*"mock" fixture has been deprecated*'])

0 commit comments

Comments
 (0)