简体   繁体   中英

Arguments of a keyword

Say I have a user-defined keyword with some arguments:

*** Keywords ***
Add
  [Arguments]    ${foo}  ${bar}=${42}
  [return]       ${${foo} + ${bar}}

No in a python context, I have a dictionary of arguments and want to use it to call the keyword:

from robot.libraries.BuiltIn import BuiltIn
def foo():
    args = {'foo' : 0}
    BuiltIn.run_keyword("Add", args) # does not work, of course

This approach fails because robot expects arguments as a list (either named or ordered correctly). I cannot simply pass all arguments as a list here, because robot will not ignore arguments that are not declared in the keyword.

What I need to do is to

  • Lookup the keyword
  • Iterate over its arguments
  • Create the argument list from the dictionary

Is that possible with the python API?

You want unpacking * for lists and ** for dicts:

def my_func(a, b, c='c', d='d'):
    print(a, b, c, d)

my_func('a', 'b')
>> a b c d

my_list = ['A', 'B']
my_dict = {'c': 'C', 'd': 'D'}
my_func(*my_list, **my_dict)
>> A B C D

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM