Skip to content

Commit 5f76dc3

Browse files
committed
Adding support for patch.dict
1 parent 006ebfe commit 5f76dc3

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ The supported methods are:
4646
* ``mock.patch``: see http://www.voidspace.org.uk/python/mock/patch.html#patch.
4747
* ``mock.patch.object``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-object.
4848
* ``mock.patch.multiple``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-multiple.
49+
* ``mock.patch.dict``: see http://www.voidspace.org.uk/python/mock/patch.html#patch-dict.
4950
* ``mock.stopall()``: stops all active patches at this point.
5051

5152
Why bother with a plugin?

pytest_mock.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ def multiple(self, *args, **kwargs):
5757
return self._start_patch(mock_module.patch.multiple, *args,
5858
**kwargs)
5959

60+
def dict(self, *args, **kwargs):
61+
"""API to mock.patch.dict"""
62+
return self._start_patch(mock_module.patch.dict, *args, **kwargs)
63+
6064

6165
def __call__(self, *args, **kwargs):
6266
"""API to mock.patch"""

test_pytest_mock.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,23 @@ def mock_using_patch_multiple(mock):
6868
@pytest.mark.parametrize('mock_fs', [mock_using_patch_object, mock_using_patch,
6969
mock_using_patch_multiple],
7070
)
71-
def test_mock_fixture(mock_fs, mock, check_unix_fs_mocked):
71+
def test_mock_patches(mock_fs, mock, check_unix_fs_mocked):
7272
"""
7373
Installs mocks into `os` functions and performs a standard testing of
7474
mock functionality. We parametrize different mock methods to ensure
7575
all (intended, at least) mock API is covered.
7676
"""
7777
mocked_rm, mocked_ls = mock_fs(mock)
7878
check_unix_fs_mocked(mocked_rm, mocked_ls)
79+
80+
81+
def test_mock_patch_dict(mock):
82+
"""
83+
Testing
84+
:param mock:
85+
"""
86+
x = {'original': 1}
87+
mock.patch.dict(x, values=[('new', 10)], clear=True)
88+
assert x == {'new': 10}
89+
mock.stopall()
90+
assert x == {'original': 1}

0 commit comments

Comments
 (0)