-
-
Notifications
You must be signed in to change notification settings - Fork 147
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or maybe have a completely separate test for resetting child mocks There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
|
||
class TestMockerStub: | ||
def test_call(self, mocker: MockerFixture) -> None: | ||
|
There was a problem hiding this comment.
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:
Given
p
is Any, is theoretically possible for it to implement__bool__
and returnFalse
for some reason.