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
Spy exceptions #173
Spy exceptions #173
Conversation
Hi @inderpreet99, thanks for the PR. When we want a mock to raise an exception, we usually set |
Hi @nicoddemus, This PR is not addressing mocked methods raising exceptions, I see that I agree returning an exception is a valid use case, which this PR will not be able to distinguish. So this PR is probably not ideal. I've seen pytest code record Let me know if you have thoughts to solving the problem. I'm not tied to this approach. |
Sorry I wasn't clear, but perhaps showing what I meant in code is simpler: @w
def wrapper(*args, **kwargs):
try:
r = method(*args, **kwargs)
except Exception as e:
result.side_effect = e
raise
else:
result.return_value = r
return r |
@nicoddemus, thanks. I completely misread what you were saying. Just updated the code. I had another problem with not being able to see multiple invocations of a spied method. For example, I think the above code will only store the latest return value. What do you think about having return_values and side_effects as a list that maybe line up exactly with Thanks. |
I'm OK with us having both I guess, so keep the existing |
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.
Looks great, thanks!
Could you please also update the documentation showing the new feature?
Added
I'm facing a bug in regard to recording multiple side effects, so I'll get that code working in a separate branch. Especially since we are OK with keeping both features (last return/exception as well as all returns/exceptions). |
1.13 released! Thanks a lot @inderpreet99! |
On spied methods, we are capturing return value. However when exceptions are raised,
return_value
comes back asFalse
rather than the exception itself.This PR change stores the raised exception into the return value.