Henry
Henry

Reputation: 3160

robot test framework: how does Set Variable if work?

I have following Keywords:

  App For Port    [Arguments]    ${app}
  # Try to launch one app on either port, continue if it fails on port 1 and launch it on port 2, record if it fails on both.
      ${returned_port}    Set Variable    ${1}
    : FOR    ${port_num}    IN    1    2
    \    ${returned_port}    Set Variable If    ${port_num} == 2    ${2}
    \    Check App Launch    ${app}    ${port_num}
    Return From Keyword    ${returned_port}

${returned_port} Set Variable ${1} works fine, ${returned_port} is 1 after this line.

However, ${returned_port} Set Variable If ${port_num} == 2 ${2} gives ${returned_port} None value.

I'm really confused. How can I make it to set ${returned_port} to 2?? Thanks in advance!!

Upvotes: 0

Views: 93

Answers (1)

Jan Kovařík
Jan Kovařík

Reputation: 1582

See doc: http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Variable%20If "The second value can also be omitted, in which case it has a default value None." It means Set Variable If sets ${returned_port} to None during first loop iteration.

Besides that you can simplify your code a bit:

App For Port
    [Arguments]    ${app}
    : FOR    ${port_num}    IN    1    2
    \    Check App Launch    ${app}    ${port_num}

... does the same. Does it? If you need to return value from the keyword without condition, just use [Return] ${var}.

And one more - you could consider to use data-driven approach: http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#data-driven-style

Upvotes: 2

Related Questions