3

In one of my testcases I need to define a dictionary, where the keys are string and the values are arrays of strings. How can I do so in Robot Framework?

My first try using a construct as shown below, will not work.

*** Variables ***
&{Dictionary}     A=StringA1  StringA2   
...               B=StringB1   StringB2

Another idea might be to use Evaluate and pass the python expression for a dictionary, but is this the only way how it can done?

*** Variables ***
&{Dictionary}     Evaluate  { "A" : ["StringA1",  "StringA2"], "B": ["StringB1","StringB2"]}

2 Answers 2

5

You have a couple more options beside using the Evaluate keyword.

  1. You could use a Python variable file:

    DICTIONARY = { "A" : ["StringA1",  "StringA2"], "B": ["StringB1","StringB2"]}
    

    Suite:

    *** Settings ***
    Variables    VariableFile.py
    
    *** Test Cases ***
    Test
        Log    ${DICTIONARY}
    
  2. You can define your lists separately, and then pass them as scalar variables when defining the dictionary.

    *** Variables ***
    @{list1}    StringA1    StringA2
    @{list2}    StringB1    StringB1
    &{Dictionary}    A=${list1}    B=${list2}
    
    *** Test Cases ***
    Test
        Log    ${Dictionary}
    
  3. You can create a user keyword using the Create List and Create Dictionary keywords. You can achieve the same in Python by writing a small library.

    *** Test Cases ***
    Test
        ${Dictionary}=    Create Dict With List Elements
        Log    ${Dictionary}
    
    
    *** Keyword ***
    Create Dict With List Elements
        ${list1}=    Create List    StringA1    StringA2
        ${list2}=    Create List    StringB1    StringB1
        ${Dictionary}=    Create Dictionary    A=${list1}    B=${list2}
        [return]    ${Dictionary}
    
Sign up to request clarification or add additional context in comments.

Comments

1

To add another option to Bence Kaulics answer, you can also use Inline Python evaluation. So for example:

&{Dictionary}    A=${{["StringA1","StringA2"]}}    B=${{["StringB1","StringB2"]}}

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.