Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use User Keyword Arguments with default values in RobotFramework

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

like image 663
A20 Tom Avatar asked Mar 01 '15 00:03

A20 Tom


2 Answers

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

like image 171
Blue Avatar answered Oct 17 '22 21:10

Blue


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
like image 5
cris Avatar answered Oct 17 '22 22:10

cris