本文介绍了如何在Robot Framework中将变量定义为具有列表值的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个测试用例中,我需要定义一个字典,其中的键是字符串,值是字符串数组.如何在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

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

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

推荐答案

除了使用 Evaluate 关键字之外,您还有其他几个选择.

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

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

  1. You could use a Python variable file:

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

套房:

*** Settings ***
Variables    VariableFile.py

*** Test Cases ***
Test
    Log    ${DICTIONARY}

  • 您可以单独定义列表,然后在定义字典时将它们作为标量变量传递.

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

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

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

  • 这篇关于如何在Robot Framework中将变量定义为具有列表值的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    07-30 16:41