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

ⅰ亾dé卋堺 提交于 2021-01-29 06:14:54

问题


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:


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}
    


来源:https://stackoverflow.com/questions/64021715/how-to-define-a-variable-being-a-dictionary-with-list-values-in-robot-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!