From 5f361b33338b8ad308a1e1cc616399358d92f8b1 Mon Sep 17 00:00:00 2001 From: Maciej Katafiasz Date: Tue, 10 Nov 2015 20:18:01 -0800 Subject: [PATCH] Stab at implementing resetall(). See #23 --- pytest_mock.py | 7 +++++++ test_pytest_mock.py | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/pytest_mock.py b/pytest_mock.py index 15ae609..6a99373 100644 --- a/pytest_mock.py +++ b/pytest_mock.py @@ -24,6 +24,13 @@ def __init__(self): self._patches = [] # list of mock._patch objects self.patch = self._Patcher(self._patches) + def resetall(self): + """ + Call reset_mock() on all patchers started by this fixture. + """ + for p in self._patches: + p.get_original()[0].reset_mock() + def stopall(self): """ Stop all patchers started by this fixture. Can be safely called multiple diff --git a/test_pytest_mock.py b/test_pytest_mock.py index 61a203c..23868b8 100644 --- a/test_pytest_mock.py +++ b/test_pytest_mock.py @@ -143,6 +143,21 @@ def test_mocker_has_mock_class_as_attribute_for_instantiation(): assert isinstance(mocker.Mock(), mock_module.Mock) +def test_mocker_resetall(mocker): + listdir = mocker.patch('os.listdir') + open = mocker.patch('os.open') + + listdir("/tmp") + open("/tmp/foo.txt") + listdir.assert_called_once_with("/tmp") + open.assert_called_once_with("/tmp/foo.txt") + + mocker.resetall() + + assert not listdir.called + assert not open.called + + def test_mocker_stub(mocker): def foo(on_something): on_something('foo', 'bar')