如何在 Robot Framework 中将变量定义为具有列表值的字典

问题描述 投票:0回答:2

在我的一个测试用例中,我需要定义一个字典,其中键是字符串,值是字符串数组。我怎样才能在机器人框架中做到这一点?

我第一次尝试使用如下所示的构造,但行不通。

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

另一个想法可能是使用 Evaluate 并传递字典的 python 表达式,但这是唯一的方法吗?

*** Variables ***
&{Dictionary}     Evaluate  { "A" : ["StringA1",  "StringA2"], "B": ["StringB1","StringB2"]}
python dictionary robotframework
2个回答
5
投票

除了使用

Evaluate
关键字之外,您还有更多选择。

  1. 您可以使用Python变量文件:

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

    套房:

    *** Settings ***
    Variables    VariableFile.py
    
    *** Test Cases ***
    Test
        Log    ${DICTIONARY}
    
  2. 您可以单独定义列表,然后在定义字典时将它们作为标量变量传递。

    *** Variables ***
    @{list1}    StringA1    StringA2
    @{list2}    StringB1    StringB1
    &{Dictionary}    A=${list1}    B=${list2}
    
    *** Test Cases ***
    Test
        Log    ${Dictionary}
    
  3. 您可以使用

    Create List
    Create Dictionary
    关键字创建用户关键字。您可以通过编写一个小型库在 Python 中实现相同的目的。

    *** 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
投票

要向Bence Kaulics答案添加另一个选项,您还可以使用内联Python评估。例如:

&{Dictionary}    A=${{["StringA1","StringA2"]}}    B=${{["StringB1","StringB2"]}}
© www.soinside.com 2019 - 2024. All rights reserved.