list 来自变量文件的 Robotframework 列表变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14564042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:56:33  来源:igfitidea点击:

Robotframework list variable from variable file

listvariablesfile-uploadrobotframework

提问by Itanium

I'm having a problem reading a list variable from a file. I have a file (variables.py) with 3 variables :

我在从文件中读取列表变量时遇到问题。我有一个包含 3 个变量的文件 (variables.py):

TEST1=212
TEST2=[111, 222, 333, 444, 555, 666]
TESTS3="sadasd"

Both ${TEST1}and ${TEST3}are accessible (I get values from variable file) But when I try to access second variable with @{TEST2}[2], I get an error :

这两个${TEST1}${TEST3}可访问(我从变量文件中的值),但是当我试图进入第二个变量有@{TEST2}[2],我得到一个错误:

FAIL : Non-existing variable '@{TEST2}[2]'

This only happens, if I try to use variables from file. If I create list variable in RIDE, I can easly access it with @{Variable}[{$index}]

只有当我尝试使用文件中的变量时才会发生这种情况。如果我在 RIDE 中创建列表变量,我可以很容易地访问它@{Variable}[{$index}]

If I try this syntax : ${TEST2}[2], I get :

如果我尝试这种语法 : ${TEST2}[2],我会得到:

'[111, 222, 333, 444, 555, 666][2]'

So robotframework knows that there is a variable with given name, but doesn't know that it's a list variable. Am I doing something wrong?

因此,robotframework 知道有一个具有给定名称的变量,但不知道它是一个列表变量。难道我做错了什么?

回答by janne

To distinguish explicitly between a list that is a value of a scalar variable and a list variable, you have to use LIST__prefix for @{vars} in the variable file. See Robot Framework User Guide: Creating variables directlyfor details.

要明确区分作为标量变量值的列表和列表变量,您必须LIST__在变量文件中为 @{vars}使用前缀。有关 详细信息,请参阅Robot Framework 用户指南:直接创建变量

In your case, this would be:

在您的情况下,这将是:

LIST__TEST2 = [111, 222, 333, 444, 555, 666]

In general, there are three ways to initialize lists in variable files:

一般而言,初始化变量文件中的列表有以下三种方式:

STRINGS = ["one", "two", "three", "four"]
LIST__STRINGS = ["one", "two", "three", "four"]

Do not confuse this with the syntax for the *** Variables ***section, where initializing a list would be:

不要将此与该*** Variables ***部分的语法混淆,其中初始化列表将是:

*** Variables ***
@{STRINGS}     | one | two | three | four

You can access individual elements in a list assigned to scalar variable like this:

您可以访问分配给标量变量的列表中的单个元素,如下所示:

${TEST2[0]}