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

Record multiple calls to spy #378

Closed
acroz opened this issue Aug 18, 2023 · 2 comments
Closed

Record multiple calls to spy #378

acroz opened this issue Aug 18, 2023 · 2 comments

Comments

@acroz
Copy link

acroz commented Aug 18, 2023

Hi, I have a use case where I'd like to use mocker.spy() to record all calls to and return values from a callable, however as far as I can tell mocker.spy() only records the most recent call (as spy_return).

Would you welcome a merge request adding this, and if so, do you have any opinions on the API?

I was thinking of something like spy_returns or spy_return_list, which would be a list of return values. I'm not sure how best to handle exceptions - put the return values and exceptions in the same list so the user can determine order, or have two lists? Alternatively just assume that if you're expecting an exception there's less likely to be a need to call it multiple times.

@frank-lenormand
Copy link
Contributor

Any thoughts on this?

@nicoddemus
Copy link
Member

Sorry seems I missed this one.

I think spy_return/spy_return_list mirrors MagicMock's call_args/call_args_list nicely, so I would go with spy_return_list which returns the list of call values.

About exceptions, I don't think we have similar support for "list of exceptions" in MagicMock (or even normal exceptions for that matter) to draw from, but keeping consistency with spy_exception, seems reasonable we could have a list of spy_exception_list too.

However I would not implement spy_exception_list right away unless someone presents a use case to track a list of exceptions raised by the spied function.

Having said that, I would love to review a PR which adds spy_return_list. 👍

frank-lenormand added a commit to frank-lenormand/pytest-mock that referenced this issue Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants