I have the following code snippet:
kwargs = {'inventory_file' : self.inventory_file}
for ts in self.test_sets_list:
current_ts = TestSet(name=ts,
test_cases=self.test_sets_list[ts],
om_ip_address=self.om_ip_address,
**kwargs)
class TestSet(object):
def __init__(self, name, test_cases, om_ip_address, **kwargs):
class TestCase(object):
def __init__(self, parameters, config_file, test_failure,
om_ip_address, **kwargs):
I know I'm probably doing wrong, because in **kwargs
I'm unpacking the values in kwargs
. What I want is to pass the arguments of kwargs
all the way to TestCase
. Inside TestSet
I'm trying to pass the kwargs
argument:
instance = TestCase(parameters=params,
config_file=cnf,
om_ip_address=om_ip_address,
**kwargs)
However I'm getting the following error:
Traceback (most recent call last):
File "main.py", line 258, in <module>
main(args)
File "main.py", line 217, in main
operation.collect_tests()
File "main.py", line 157, in collect_tests
**kwargs)
File "test_set.py", line 47, in __init__
self.__instantiate_module(tc, om_ip_address)
File "test_set.py", line 78, in __instantiate_module
**self.kwargs)
TypeError: __init__() got an unexpected keyword argument 'inventory_file
The class is instantiated via:
my_class = getattr(module, test_cases['name'])
instance = my_class(parameters=params,
config_file=cnf,
om_ip_address=om_ip_address)
How can I be able to pass kwargs
all the way to TestCase
?
Calling a function with **kwargs
, where kwargs = {'inventory_file' : self.inventory_file}
is the same as calling
function(inventory_file=self.inventory_file)
and if the definition of function
doesn't have inventory_file in the accepted params list, it will throw the error you got there. Two ways to solve this:
class TestCase(object):
# add inventory_file to the params list with a default value
def __init__(self, parameters, config_file, test_failure,
om_ip_address, inventory_file=None, **kwargs):
self.inventory_file = inventory_file
or:
class TestCase(object):
# check for the inventory file in the kwargs dict
def __init__(self, parameters, config_file, test_failure,
om_ip_address, **kwargs):
if 'inventory_file' in kwargs:
self.inventory_file = kwargs['inventory_file']
and in both cases:
>>> t = TestCase('param1','param2','param3','param4', **{'inventory_file':'file'})
>>> t.inventory_file
'file'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.