From 669a472395c17d7ffe19e58bd1951a68115d38ac Mon Sep 17 00:00:00 2001 From: Alexandre Marty Date: Tue, 10 Nov 2020 17:16:07 +0100 Subject: [PATCH 1/4] Added arguments to resetall() --- src/pytest_mock/plugin.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/pytest_mock/plugin.py b/src/pytest_mock/plugin.py index c9f3853..740b16c 100644 --- a/src/pytest_mock/plugin.py +++ b/src/pytest_mock/plugin.py @@ -68,8 +68,13 @@ 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() From 5ea3142bfb3b737529acc14b1556e21716a9c377 Mon Sep 17 00:00:00 2001 From: Alexandre Marty Date: Tue, 10 Nov 2020 17:21:56 +0100 Subject: [PATCH 2/4] Updated tests for resetall() --- tests/test_pytest_mock.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/test_pytest_mock.py b/tests/test_pytest_mock.py index 33a80b2..2e14ebc 100644 --- a/tests/test_pytest_mock.py +++ b/tests/test_pytest_mock.py @@ -170,11 +170,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") @@ -182,6 +182,13 @@ def test_mocker_resetall(mocker: MockerFixture) -> None: assert not listdir.called assert not open.called + assert listdir.return_value == "foo" + assert open.side_effect == ["bar", "baz"] + + mocker.resetall(return_value=True, side_effect=True) + + assert listdir.return_value is None + assert open.side_effect is None class TestMockerStub: From 875bfdb1d2639888c48bda19e13269e9767d711a Mon Sep 17 00:00:00 2001 From: Alexandre Marty Date: Tue, 10 Nov 2020 17:25:42 +0100 Subject: [PATCH 3/4] Actually passing arguments --- src/pytest_mock/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytest_mock/plugin.py b/src/pytest_mock/plugin.py index 740b16c..dc0544d 100644 --- a/src/pytest_mock/plugin.py +++ b/src/pytest_mock/plugin.py @@ -76,7 +76,7 @@ def resetall(self, *, return_value: bool = False, side_effect: bool = False) -> :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: """ From 494d5bbaa0175dc2197da7e00419fd24b5cbf6bf Mon Sep 17 00:00:00 2001 From: Alexandre Marty Date: Tue, 10 Nov 2020 17:35:41 +0100 Subject: [PATCH 4/4] Fixed tests --- tests/test_pytest_mock.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_pytest_mock.py b/tests/test_pytest_mock.py index 2e14ebc..6f97576 100644 --- a/tests/test_pytest_mock.py +++ b/tests/test_pytest_mock.py @@ -183,11 +183,11 @@ def test_mocker_resetall(mocker: MockerFixture) -> None: assert not listdir.called assert not open.called assert listdir.return_value == "foo" - assert open.side_effect == ["bar", "baz"] + assert list(open.side_effect) == ["baz"] mocker.resetall(return_value=True, side_effect=True) - assert listdir.return_value is None + assert isinstance(listdir.return_value, mocker.Mock) assert open.side_effect is None