Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resetall() arguments #214

Merged
merged 4 commits into from Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/pytest_mock/plugin.py
Expand Up @@ -68,10 +68,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:
"""
Expand Down
15 changes: 11 additions & 4 deletions tests/test_pytest_mock.py
Expand Up @@ -170,18 +170,25 @@ 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")

mocker.resetall()

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:
Expand Down