I need to write utest in Python for below method

106 Views Asked by At

Function prototype :

def sample_test(self, param1, param2, optional_param1=False, optional_param2=False, 
            optional_param3={}):
    if optional_param2:
       print("In param2")
    if optional_param3:
       print("In param3")

Now if I call the method as sample_test(param1, param2, False, {"key": "val"}) In param2 gets printed .optional_param2 is assigned with dict and the test is executed wrongly as optional_param2 is considered as True. I want to write utest for the same so that these errors are caught beforehand.. I want to write utest for the method so that these type of errors are caught and function is executed efficiently .

1

There are 1 best solutions below

0
On

You can assign inputs directly, like

def function(v1=0,v2=1):
    return (v1,v2)
print(function(v2=7))
>>(0,7)

Plugging this into your code might look like:

sample_test(param1, param2, False, optional_param3={"key":"val"})

in which optional_param2 would default to False while optional_param3 receives the correct dictionary.