I need to wrap a class in a mock like so:
Original code:
class _InnerImpl(object):
def __init__(self, arg1, arg2, arg3):
# do stuff
I would like to do something like this:
from unittest.mock import patch
def my_wrapper(*args, **kwargs):
kwargs['arg1'] = gen_next_value() # change one of the arguments
return _InnerImpl(*args, **kwargs) # call the real thing with modified args
def run(user_input):
with patch(_InnerImpl, my_wrapper):
some_method(user_input)
Unfortunately the way that patch
works, I can't call the "real thing" because it is also called inside the with patch
block. This results in infinite recursion loop.
I'm using this in an implementation outside of unit test. So this isn't for unit tests. I've tried various things such as saving __init__
reference, using wraps
. Nothing works.