From c3e9aa1d8201544a84ba38ed13216a41ffb9f891 Mon Sep 17 00:00:00 2001 From: Alexandre Marty Date: Sat, 24 Apr 2021 23:46:03 +0200 Subject: [PATCH] Add resetall() arguments (#214) --- src/pytest_mock/plugin.py | 11 ++++++++--- tests/test_pytest_mock.py | 15 +++++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/pytest_mock/plugin.py b/src/pytest_mock/plugin.py index 15147bc..b3b97ad 100644 --- a/src/pytest_mock/plugin.py +++ b/src/pytest_mock/plugin.py @@ -60,10 +60,15 @@ def __init__(self, config: Any) -> None: if hasattr(mock_module, "seal"): self.seal = mock_module.seal - def resetall(self) -> None: - """Call reset_mock() on all patchers started by this fixture.""" + def resetall(self, *, return_value: bool = False, side_effect: bool = False) -> None: + """ + Call reset_mock() on all patchers started by this fixture. + + :param bool return_value: Reset the return_value of mocks. + :param bool side_effect: Reset the side_effect of mocks. + """ for m in self._mocks: - m.reset_mock() + m.reset_mock(return_value=return_value, side_effect=side_effect) def stopall(self) -> None: """ diff --git a/tests/test_pytest_mock.py b/tests/test_pytest_mock.py index 55f5ec4..14e01ec 100644 --- a/tests/test_pytest_mock.py +++ b/tests/test_pytest_mock.py @@ -177,11 +177,11 @@ def test_mocker_aliases(name: str, pytestconfig: Any) -> None: def test_mocker_resetall(mocker: MockerFixture) -> None: - listdir = mocker.patch("os.listdir") - open = mocker.patch("os.open") + listdir = mocker.patch("os.listdir", return_value="foo") + open = mocker.patch("os.open", side_effect=["bar", "baz"]) - listdir("/tmp") - open("/tmp/foo.txt") + assert listdir("/tmp") == "foo" + assert open("/tmp/foo.txt") == "bar" listdir.assert_called_once_with("/tmp") open.assert_called_once_with("/tmp/foo.txt") @@ -189,6 +189,13 @@ def test_mocker_resetall(mocker: MockerFixture) -> None: assert not listdir.called assert not open.called + assert listdir.return_value == "foo" + assert list(open.side_effect) == ["baz"] + + mocker.resetall(return_value=True, side_effect=True) + + assert isinstance(listdir.return_value, mocker.Mock) + assert open.side_effect is None class TestMockerStub: