Skip to content

Commit

Permalink
Add resetall() arguments (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-marty authored Apr 24, 2021
1 parent 28ef007 commit c3e9aa1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/pytest_mock/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
15 changes: 11 additions & 4 deletions tests/test_pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,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

0 comments on commit c3e9aa1

Please sign in to comment.