如何使“导入变量"中的变量全局可用? [英] How to make variables from 'Import Variables' globally available?

查看:54
本文介绍了如何使“导入变量"中的变量全局可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Robot Framework 来自动化一些测试.

I'm currently using Robot Framework to automate some tests.

问题:我试图找出一种方法,使从导入变量"关键字导入的变量全局可用(--variablefile 对我来说不是一个选项,对现在,可悲的是).

Problem: I am trying to figure out a way to make variables imported from 'Import Variables' keyword globally available (--variablefile is not an option for me right now, sadly).

我已经尝试了一些解决方法,但没有运气:

I already tried some workarounds, with no luck:

 1. Import Variables
 2. Get Variables available in current scope
 3. Loop through each variable and get its key and value
 4. Set each variable globally (using Set Global Variable)

我在上述方法中遇到的问题是我的外部文件(标量、列表、字典)中有不同类型的数据,因此在尝试设置字典时设置全局变量"在某些时候会失败.它抱怨字典项'no'不包含'='分隔符.".

The problem I faced in the above approach is that there are different types of data on my external file (scalar, lists, dicts), and therefore 'Set Global Variable' fails at some point when trying to set a dict. It complains about "Dictionary item 'no' does not contain '=' separator.".

我研究了一下,发现机器人不能分配像这样的 dict 类型变量设置全局变量 ${DICT_VAR} {"key1":"data1", "key2":"data2"}".实际解释可以在这里找到 robot-framework github测试.

I researched a bit and found that robot can't assign a dict type variable like this "Set Global Variable ${DICT_VAR} {"key1":"data1", "key2":"data2"}". Practical explanation can be found here robot-framework github tests.

嗯,我正在考虑建立一个小型图书馆来完成这个并使用它:Import Variables/path/to/variables.py scope=global",其中属性范围可以是以下之一:test、suite、global.

Well, I'm thinking about building a small library to accomplish this and use it like: "Import Variables /path/to/variables.py scope=global", where the attribute scope could be one of the following: test, suite, global.

好吧,这就是我的挣扎,伙计们.希望有人可以帮助我.(:

Well, that's my struggle, folks. Hopefully someone can help me with that. (:

所以,关于无法使用 --variablefile 选项:目前,我在测试执行开始时要求用户提供一些输入,其中一个输入是在现有的外部文件中进行选择.我知道我可以对用户说请提供带有外部文件绝对路径的 --variablefile 选项.",但我不希望这样,因为这对用户来说不太实用.

So, about not being able to use --variablefile option: Currently, I'm asking the user for some inputs at the beginning of test execution, and one of these inputs is a selection among existent external files. I know I could say to the user "Please provide --variablefile option with external file absolute path.", but I do not want that since it is less practical for the user.

[SOLVED] 结论: 我意识到我试图做的事情并不意味着使用机器人来完成.Robot 是用于创建和维护测试、生成报告等的工具.在 .robot 文件中应避免使用变量(和全局变量)管理许多文件.然后我想出了一个外部文件(一个 python 脚本),它只是完成所有这些选择文件和设置全局变量的肮脏工作.在这个脚本的最后,我只是调用 subprocess.run("robot", args) 其中 args 是机器人开始执行测试所需的所有参数(-V、-v、-i 等).最后,我能够在外部脚本中实际使用 --variablefile (-V) 选项.

推荐答案

请记住,全局变量本质上应该是常量.尽管可以从关键字或测试用例更新全局变量,但这应该是一个例外操作.为了定期更新变量,应使用套件、测试用例和关键字范围.

Bear in mind that Global variables should be constant in nature. Event though it is possible to update Global variables from a keyword or Test Case, this should be an exceptional action. For regular updating of variables the Suite, Test Case and Keyword scopes should be used.

在下面的示例中,我使用了一个包含嵌套值集的 YAML 变量文件.YAML 导入允许以人类可读的格式定义 Python 列表、标量和字典,这使得它们对这些类型的嵌套导入非常有用.

In the below example I use a YAML variable file that holds a nested set of values. YAML imports allows for the definition of Python Lists, Scalars and Dictionaries in a human readable format, which is what makes them so useful for these types of nested imports.

data.yaml

DATA:
  Set 1:
    Lisa:
      - list item 1
      - list item 2
    Dica:
      dict item 1: value 1
      dict item 2: value 2
    vara: variable value 1
  Set 2:
    Lisa:
      - list item 3
      - list item 4
    Dica:
      dict item 1: value 3
      dict item 2: value 4
    vara: variable value 2

上面的脚本是通过下面的Variables data.yaml 语句加载的.然后,脚本的其余部分采用该结构的特定子集,并通过循环遍历这些项目来创建全局变量.

The above script is loaded through Variables data.yaml statement below. The rest of the script then takes a specific sub-set of that structure and creates Global Variables from the items by looping through them.

create_globals.robot

*** Setting ***
Library    Collections    

Variables    data.yaml

*** Test Cases ***
Test Case
    Create Globals    Set 2
    No Operation


*** Keywords ***
Get Data Set
    [Arguments]    ${name}
    [Return]    ${DATA['${name}']}

Create Globals
    [Arguments]    ${name}
    ${dataset}    Get Data Set    ${name}
    @{global_var_names}    Get Dictionary Keys    ${dataset}
    :FOR    ${global_var_name}    IN    @{global_var_names}
    \    Set Global Variable    ${${global_var_name}}    ${dataset['${global_var_name}']}

因为获取数据集本身是通过关键字抽象的,所以将数据源更改为例如命令行导入甚至数据库的变化相对较小.

Because fetching the data set itself is abstracted through a keyword, changing the source of the data to for example a command line import or even a database is relative small change.

这篇关于如何使“导入变量"中的变量全局可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆