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 support for resetall to create_autospec #390

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/pytest_mock/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ def __init__(self, config: Any) -> None:
self.call = mock_module.call
self.ANY = mock_module.ANY
self.DEFAULT = mock_module.DEFAULT
self.create_autospec = mock_module.create_autospec
self.sentinel = mock_module.sentinel
self.mock_open = mock_module.mock_open
if hasattr(mock_module, "seal"):
self.seal = mock_module.seal

def create_autospec(
self, spec: Any, spec_set: bool = False, instance: bool = False, **kwargs: Any
) -> MockType:
m: MockType = self.mock_module.create_autospec(
spec, spec_set, instance, **kwargs
)
self._patches_and_mocks.append((None, m))
return m

def resetall(
self, *, return_value: bool = False, side_effect: bool = False
) -> None:
Expand Down Expand Up @@ -102,7 +110,8 @@ def stopall(self) -> None:
times.
"""
for p, m in reversed(self._patches_and_mocks):
p.stop()
if p:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be more explicit here:

Suggested change
if p:
if p is not None:

Given p is Any, is theoretically possible for it to implement __bool__ and return False for some reason.

p.stop()
self._patches_and_mocks.clear()

def stop(self, mock: unittest.mock.MagicMock) -> None:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ def ls(cls, path):
return os.listdir(path)


class TestObject:
"""
Class that is used for testing create_autospec with child mocks
"""

def run(self) -> str:
return "not mocked"


@pytest.fixture
def check_unix_fs_mocked(
tmpdir: Any, mocker: MockerFixture
Expand Down Expand Up @@ -185,23 +194,35 @@ def test_mocker_resetall(mocker: MockerFixture) -> None:
listdir = mocker.patch("os.listdir", return_value="foo")
open = mocker.patch("os.open", side_effect=["bar", "baz"])

mocked_object = mocker.create_autospec(TestObject)
mocked_object.run.return_value = "mocked"

assert listdir("/tmp") == "foo"
assert open("/tmp/foo.txt") == "bar"
assert mocked_object.run() == "mocked"
listdir.assert_called_once_with("/tmp")
open.assert_called_once_with("/tmp/foo.txt")
mocked_object.run.assert_called_once()

mocker.resetall()

assert not listdir.called
assert not open.called
assert not mocked_object.called
assert listdir.return_value == "foo"
assert list(open.side_effect) == ["baz"]
assert mocked_object.run.return_value == "mocked"

mocker.resetall(return_value=True, side_effect=True)

assert isinstance(listdir.return_value, mocker.Mock)
assert open.side_effect is None

if sys.version_info >= (3, 9):
# The reset on child mocks have been implemented in 3.9
# https://bugs.python.org/issue38932
assert mocked_object.run.return_value != "mocked"
Comment on lines +220 to +223
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't really like this check here, maybe test needs to be split, and then it's easier to use pytest.mark.skipif to skip for 3.8 and below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe have a completely separate test for resetting child mocks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check is fine, when we drop support for Python versions it is common to search for sys.version_info checks like these. 👍



class TestMockerStub:
def test_call(self, mocker: MockerFixture) -> None:
Expand Down
Loading