I want to test a class created with default parameters by replacing the value of the default parameter during unit testing.
For example, I want the following line (throughout the code)
obj = SomeClass()
To look like it was called with a parameter
obj = SomeClass(overiden_parameter)
One solution might be to create simple subclass:
``` def OriginalClass(object):
def __init_(some_param="default_value"):
...
```
``` def MockedOriginalClass(OriginalClass):
def __init_():
super(MockedOriginalClass, self).__init__("some_other_value)
...
```
How to a mock/patch OriginalClass
to be MockedOriginalClass
thoughout the code? Keep in mind that I do want to keep functionality of the original class, the only one thing I want to change is it's default __init__
parameter.
I feel this is a very simple thing to do with Mocking, I just didn't quite figure how to do it.
I found out about this question: Python unittest mock: Is it possible to mock the value of a method's default arguments at test time?
It's very close but I don't think the same trick can be applied to the __init__
method.
SomeClass
and then have the mock act as if the parameter was passed in? – Martijn Pieters♦SomeClass
with one that simply patch one default input parameter. – Rastikan