How to use User Keyword Arguments with default values in RobotFramework

巧了我就是萌 提交于 2020-01-12 07:57:07

问题


Can someone explain me how to use default values for optional keyword arguments in connection with data-driven testcases?

As you can see in my example not all default values are used in some cases:

*** Test Cases ***
| testArgs | [Template] | doSomething
| | 111 : 222 : 333 : 444
| | xxx : 222 : 333 : 444 | xxx
| | 111 : xxx : 333 : 444 | | xxx
| | 111 : xxx : 333 : 444 | ${EMPTY} | xxx
| | None : xxx : 333 : 444 | ${None} | xxx
| | None : xxx : 333 : 444 | ${null} | xxx
| | 111 : 222 : xxx : 444  | | | xxx

*** Keywords ***
| doSomething
| | [Arguments] | ${expected} | ${arg1}=111 | ${arg2}=222 | ${arg3}=333 | ${arg4}=444
| | Log | exp: ${expected}
| | ${rc} | Set Variable | ${arg1} : ${arg2} : ${arg3} : ${arg4}
| | Log | arg: ${rc}
| | Run Keyword If  | '${rc}' == '${expected}'  
| | ... | Log | === equal ===
| | ... | ELSE  
| | ... | Log | !!! diff !!!
| | Log | **************************
| | Should be equal | ${rc} | ${expected}

Result:

testArgs                                                              | FAIL |
Several failures occurred:
1)  : xxx : 333 : 444 != 111 : xxx : 333 : 444
2)  : xxx : 333 : 444 != 111 : xxx : 333 : 444
3)  :  : xxx : 444 != 111 : 222 : xxx : 444

I know that I can use named arguments to set specific keyword arguments. But this is only possible in keyword-driven testcases.

Regards, Tom


回答1:


Call the specific keyword only with which argument you wanted to pass. eg:

*** Test Cases ***
TEST
    MyKeyword   a=1    c=3

*** Keywords ***
MyKeywords
    [Arguments]   ${a}=0   ${b}=2   ${c}=3

Here, I am not passing argument 'b'. By default it will take b=2




回答2:


An example from the Robot Framework User Guide:

*** Keywords ***
    Two Arguments With Defaults
    [Arguments]    ${arg1}=default 1    ${arg2}=${VARIABLE}
    [Documentation]    This keyword takes 0-2 arguments
    Log    1st argument ${arg1}
    Log    2nd argument ${arg2}

 *** Test Cases ***
Example
    Two Arguments With Defaults    arg2=new value

So Peter Bingham is right:

*** Test Cases ***
TEST
    MyKeyword   a=1    c=3

*** Keywords ***
MyKeywords
    [Arguments]   ${a}=0   ${b}=2   ${c}=3



回答3:


You already used the default argument values in your example :

0a) 111 : 222 : 333 : 444 => That is only ${expected} set, all other args are defaults (and it's working).

0b) 111 : xxx : 333 : 444 | | xxx => This outputs no error, ${arg1} default value seems correctly used.

1) None : xxx : 333 : 444 | ${None} | xxx => The 'None' text is not the special value ${None}, you may have the ${expected} corrected with one of thoses : ${None}: xxx : 333 : 444 | ${None} | xxx or None : xxx : 333 : 444 |None| xxx.

2) None : xxx : 333 : 444 | ${null} | xxx => Same remark as above, concerning the difference between the text 'None' and the value ${null}, you may correct it with similar of above props.

3) 111 : 222 : xxx : 444 | | | xxx => Can't find why ${arg1} and ${arg2} are set to ${None} in the result : they should be valued, and this should output no error ; may have you changed this line to 111 : 222 : xxx : 444 | ${None} | ${None} | xxx specially for the output errors ?

There is no problem in your examples, you use default values appropriately. (I know that's a 3-yo thread, but I was looking for an "How-to" about defaulting values in RobotFramework, thus answering.)



来源:https://stackoverflow.com/questions/28788795/how-to-use-user-keyword-arguments-with-default-values-in-robotframework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!