16

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

3 Answers 3

33

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

Sign up to request clarification or add additional context in comments.

1 Comment

This gave me an error: I required the following: MyKeywords [Arguments] ${a}=0 ${b}=2 ${c}=3
6

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

Comments

0

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.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.