2

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"]}

1 Answer 1

4

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}
    
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.