Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f7c4158

Browse files
committed
Adding patch.stopall method to unittest.mock
1 parent bfcb429 commit f7c4158

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

Doc/library/unittest.mock.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,8 +1354,12 @@ method of a `TestCase`:
13541354
As an added bonus you no longer need to keep a reference to the `patcher`
13551355
object.
13561356

1357-
In fact `start` and `stop` are just aliases for the context manager
1358-
`__enter__` and `__exit__` methods.
1357+
It is also possible to stop all patches which have been started by using
1358+
`patch.stopall`.
1359+
1360+
.. function:: patch.stopall
1361+
1362+
Stop all active patches.
13591363

13601364

13611365
TEST_PREFIX

Lib/unittest/mock.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ def _is_started(patcher):
10021002
class _patch(object):
10031003

10041004
attribute_name = None
1005+
_active_patches = set()
10051006

10061007
def __init__(
10071008
self, getter, attribute, new, spec, create,
@@ -1270,8 +1271,18 @@ def __exit__(self, *exc_info):
12701271
if _is_started(patcher):
12711272
patcher.__exit__(*exc_info)
12721273

1273-
start = __enter__
1274-
stop = __exit__
1274+
1275+
def start(self):
1276+
"""Activate a patch, returning any created mock."""
1277+
result = self.__enter__()
1278+
self._active_patches.add(self)
1279+
return result
1280+
1281+
1282+
def stop(self):
1283+
"""Stop an active patch."""
1284+
self._active_patches.discard(self)
1285+
return self.__exit__()
12751286

12761287

12771288

@@ -1562,9 +1573,16 @@ def _clear_dict(in_dict):
15621573
del in_dict[key]
15631574

15641575

1576+
def _patch_stopall():
1577+
"""Stop all active patches."""
1578+
for patch in list(_patch._active_patches):
1579+
patch.stop()
1580+
1581+
15651582
patch.object = _patch_object
15661583
patch.dict = _patch_dict
15671584
patch.multiple = _patch_multiple
1585+
patch.stopall = _patch_stopall
15681586
patch.TEST_PREFIX = 'test'
15691587

15701588
magic_methods = (

Lib/unittest/test/testmock/testpatch.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,24 @@ def test_not_callable_spec_as_list(self):
17621762
p.stop()
17631763

17641764

1765+
def test_patch_stopall(self):
1766+
unlink = os.unlink
1767+
chdir = os.chdir
1768+
path = os.path
1769+
patch('os.unlink', something).start()
1770+
patch('os.chdir', something_else).start()
1771+
1772+
@patch('os.path')
1773+
def patched(mock_path):
1774+
patch.stopall()
1775+
self.assertIs(os.path, mock_path)
1776+
self.assertIs(os.unlink, unlink)
1777+
self.assertIs(os.chdir, chdir)
1778+
1779+
patched()
1780+
self.assertIs(os.path, path)
1781+
1782+
17651783

17661784
if __name__ == '__main__':
17671785
unittest.main()

0 commit comments

Comments
 (0)