2

I miss sets when I write Robot Framework tests. I tried to emulate what I need like this:

Create Set
    [arguments]    @{elements}
    ${set}=    Evaluate    {e for e in @{elements}}
    [return]    ${set}

Sets Should Be Equal
    [arguments]    ${a}    ${b}
    ${equal}    Evaluate    set(@{a}) == set(@{b})
    Should Be True    ${equal}

Append to Set
    [arguments]    ${set}    ${new}
    Evaluate    ${set}.add(${new})

The two first keywords are fine, it seems.

Append to Set does not work, ${new} never gets added to the set. My guess is that the keyword is working on a copy, and since set.add returns None, the copy gets discarded with no effect.

I've tried to return ${set} from the keyword, and save the result from the keyword call, to no effect.

robot-log.html looks like this for Create Set (working):

enter image description here

and like this for Append to Set (not working):

enter image description here

I can I modify the set passed as argument?

Another issue is that Append to Set requires quoting the new argument, while Create Set does not.

1 Answer 1

2

For both of your issues the solution is to leave out the {, } characters around the variable names.

Append to Set
    [arguments]    ${set}    ${new}
    Evaluate    $set.add($new)

From Evaluate keyword's documentation:

Variables used like ${variable} are replaced in the expression before evaluation. Variables are also available in the evaluation namespace and can be accessed using the special $variable syntax as explained in the Evaluating expressions section.

From the Evaluating expressions section:

Using variables

When a variable is used in the expressing using the normal ${variable} syntax, its value is replaced before the expression is evaluated. This means that the value used in the expression will be the string representation of the variable value, not the variable value itself. This is not a problem with numbers and other objects that have a string representation that can be evaluated directly, but with other objects the behavior depends on the string representation. Most importantly, strings must always be quoted, and if they can contain newlines, they must be triple quoted.

Examples:

Should Be True    ${rc} < 10  Return code greater than 10     
Run Keyword If    '${status}' == 'PASS'   Log     Passed
Run Keyword If    'FAIL' in '''${output}'''   Log     Output contains FAIL

Actual variables values are also available in the evaluation namespace. They can be accessed using special variable syntax without the curly braces like $variable. These variables should never be quoted.

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

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.