-
-
Notifications
You must be signed in to change notification settings - Fork 146
Implement spy_return_list
.
#417
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
Merged
nicoddemus
merged 5 commits into
pytest-dev:main
from
frank-lenormand:feat-spy_return_list
Mar 21, 2024
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,14 @@ | |
import sys | ||
import unittest.mock | ||
import warnings | ||
from dataclasses import dataclass | ||
from dataclasses import field | ||
from typing import Any | ||
from typing import Callable | ||
from typing import Dict | ||
from typing import Generator | ||
from typing import Iterable | ||
from typing import Iterator | ||
from typing import List | ||
from typing import Mapping | ||
from typing import Optional | ||
|
@@ -43,16 +46,55 @@ class PytestMockWarning(UserWarning): | |
"""Base class for all warnings emitted by pytest-mock.""" | ||
|
||
|
||
@dataclass | ||
class MockCacheItem: | ||
mock: MockType | ||
patch: Optional[Any] = None | ||
|
||
|
||
@dataclass | ||
class MockCache: | ||
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. Loved this refactoring. 👍 |
||
cache: List[MockCacheItem] = field(default_factory=list) | ||
|
||
def find(self, mock: MockType) -> MockCacheItem: | ||
the_mock = next( | ||
(mock_item for mock_item in self.cache if mock_item.mock == mock), None | ||
) | ||
if the_mock is None: | ||
raise ValueError("This mock object is not registered") | ||
return the_mock | ||
|
||
def add(self, mock: MockType, **kwargs: Any) -> MockCacheItem: | ||
try: | ||
return self.find(mock) | ||
except ValueError: | ||
self.cache.append(MockCacheItem(mock=mock, **kwargs)) | ||
return self.cache[-1] | ||
|
||
def remove(self, mock: MockType) -> None: | ||
mock_item = self.find(mock) | ||
self.cache.remove(mock_item) | ||
|
||
def clear(self) -> None: | ||
self.cache.clear() | ||
|
||
def __iter__(self) -> Iterator[MockCacheItem]: | ||
return iter(self.cache) | ||
|
||
def __reversed__(self) -> Iterator[MockCacheItem]: | ||
return reversed(self.cache) | ||
|
||
|
||
class MockerFixture: | ||
""" | ||
Fixture that provides the same interface to functions in the mock module, | ||
ensuring that they are uninstalled at the end of each test. | ||
""" | ||
|
||
def __init__(self, config: Any) -> None: | ||
self._patches_and_mocks: List[Tuple[Any, unittest.mock.MagicMock]] = [] | ||
self._mock_cache: MockCache = MockCache() | ||
self.mock_module = mock_module = get_mock_module(config) | ||
self.patch = self._Patcher(self._patches_and_mocks, mock_module) # type: MockerFixture._Patcher | ||
self.patch = self._Patcher(self._mock_cache, mock_module) # type: MockerFixture._Patcher | ||
# aliases for convenience | ||
self.Mock = mock_module.Mock | ||
self.MagicMock = mock_module.MagicMock | ||
|
@@ -75,7 +117,7 @@ def create_autospec( | |
m: MockType = self.mock_module.create_autospec( | ||
spec, spec_set, instance, **kwargs | ||
) | ||
self._patches_and_mocks.append((None, m)) | ||
self._mock_cache.add(m) | ||
return m | ||
|
||
def resetall( | ||
|
@@ -93,37 +135,39 @@ def resetall( | |
else: | ||
supports_reset_mock_with_args = (self.Mock,) | ||
|
||
for p, m in self._patches_and_mocks: | ||
for mock_item in self._mock_cache: | ||
# See issue #237. | ||
if not hasattr(m, "reset_mock"): | ||
if not hasattr(mock_item.mock, "reset_mock"): | ||
continue | ||
if isinstance(m, supports_reset_mock_with_args): | ||
m.reset_mock(return_value=return_value, side_effect=side_effect) | ||
# NOTE: The mock may be a dictionary | ||
if hasattr(mock_item.mock, "spy_return_list"): | ||
mock_item.mock.spy_return_list = [] | ||
if isinstance(mock_item.mock, supports_reset_mock_with_args): | ||
mock_item.mock.reset_mock( | ||
return_value=return_value, side_effect=side_effect | ||
) | ||
else: | ||
m.reset_mock() | ||
mock_item.mock.reset_mock() | ||
|
||
def stopall(self) -> None: | ||
""" | ||
Stop all patchers started by this fixture. Can be safely called multiple | ||
times. | ||
""" | ||
for p, m in reversed(self._patches_and_mocks): | ||
if p is not None: | ||
p.stop() | ||
self._patches_and_mocks.clear() | ||
for mock_item in reversed(self._mock_cache): | ||
if mock_item.patch is not None: | ||
mock_item.patch.stop() | ||
self._mock_cache.clear() | ||
|
||
def stop(self, mock: unittest.mock.MagicMock) -> None: | ||
""" | ||
Stops a previous patch or spy call by passing the ``MagicMock`` object | ||
returned by it. | ||
""" | ||
for index, (p, m) in enumerate(self._patches_and_mocks): | ||
if mock is m: | ||
p.stop() | ||
del self._patches_and_mocks[index] | ||
break | ||
else: | ||
raise ValueError("This mock object is not registered") | ||
mock_item = self._mock_cache.find(mock) | ||
if mock_item.patch: | ||
mock_item.patch.stop() | ||
self._mock_cache.remove(mock) | ||
|
||
def spy(self, obj: object, name: str) -> MockType: | ||
""" | ||
|
@@ -146,6 +190,7 @@ def wrapper(*args, **kwargs): | |
raise | ||
else: | ||
spy_obj.spy_return = r | ||
spy_obj.spy_return_list.append(r) | ||
return r | ||
|
||
async def async_wrapper(*args, **kwargs): | ||
|
@@ -158,6 +203,7 @@ async def async_wrapper(*args, **kwargs): | |
raise | ||
else: | ||
spy_obj.spy_return = r | ||
spy_obj.spy_return_list.append(r) | ||
return r | ||
|
||
if asyncio.iscoroutinefunction(method): | ||
|
@@ -169,6 +215,7 @@ async def async_wrapper(*args, **kwargs): | |
|
||
spy_obj = self.patch.object(obj, name, side_effect=wrapped, autospec=autospec) | ||
spy_obj.spy_return = None | ||
spy_obj.spy_return_list = [] | ||
spy_obj.spy_exception = None | ||
return spy_obj | ||
|
||
|
@@ -206,8 +253,8 @@ class _Patcher: | |
|
||
DEFAULT = object() | ||
|
||
def __init__(self, patches_and_mocks, mock_module): | ||
self.__patches_and_mocks = patches_and_mocks | ||
def __init__(self, mock_cache, mock_module): | ||
self.__mock_cache = mock_cache | ||
self.mock_module = mock_module | ||
|
||
def _start_patch( | ||
|
@@ -219,7 +266,7 @@ def _start_patch( | |
""" | ||
p = mock_func(*args, **kwargs) | ||
mocked: MockType = p.start() | ||
self.__patches_and_mocks.append((p, mocked)) | ||
self.__mock_cache.add(mock=mocked, patch=p) | ||
if hasattr(mocked, "reset_mock"): | ||
# check if `mocked` is actually a mock object, as depending on autospec or target | ||
# parameters `mocked` can be anything | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.