Search code examples
pythondictionaryrobotframework

How to define a variable being a dictionary with list values in Robot Framework


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

Solution

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