简体   繁体   English

Robot Framework中的静态变量

[英]Static Variables in Robot Framework

Is there any such thing as a static type of variable in robot framework? 机器人框架中是否存在诸如静态类型的变量之类的东西? and if so how do i invoke it? 如果可以,我该如何调用它? or is the solution to do it via a python file? 还是通过python文件做到这一点的解决方案?

The problem i'm trying to fix is, i want a variable to be set once and for it to remember the value that is set. 我要解决的问题是,我希望一次设置一个变量,让它记住设置的值。 Unfortunately this variable is in a resource file (it's separate to my test suite files). 不幸的是,该变量位于资源文件中(与我的测试套件文件分开)。

Yes you can set static variables in robot framework. 是的,您可以在机器人框架中设置静态变量。 Your resource file should have something like this in it. 您的资源文件中应该包含类似的内容。

*** Settings ***
...
*** Variables ***
${MyVariable}    MyValue

*** Keywords ***
...

Your test should look something like this 您的测试应该看起来像这样

*** Settings ***  

Resource    (Path to resource file)

*** Test Cases ***
My Test Case
[Documentation]    This is documentation
My keyword    MyVariable

Given the following suite structure: 给定以下套件结构:

/test_folder
    __init__.robot
    variables.resource
    test_s1.robot
    test_s2.robot
    test_s3.robot

Now in your variables.resource file you can create and initialize your variable like: 现在,在您的variables.resource文件中,您可以像下面这样创建和初始化变量:

*** Keywords ***
Setup Static Variable
    ${my_static}=    Init My Static    # Get time here
    Set Suite Variable    ${my_static}    children=true

Here with the Set Suite Variable you can make your variable accessible in the current suite (in which this keyword is actually called) and with the children=true option, in all sub-suites. 在这里,通过Set Suite Variable,您可以使变量在当前套件(实际上称为此关键字)中以及所有子套件中的children=true选项中均可访问。 This means all test suite files in the folder for example. 例如,这意味着文件夹中的所有测试套件文件。


Next step is to create your __init__.robot file: 下一步是创建__init__.robot文件:

*** Settings ***
Resource            variables.resource
Suite Setup         Setup Static Variable

The current suite will be test_folder and the children will be test_s1 , test_s2 and test_s3 . 当前套件为test_folder ,子级为test_s1test_s2test_s3 ${my_static} will be accessible in all of them. ${my_static}将可在所有这些文件中访问。 The Setup Static Variable keyword will be executed once, when the execution reaches the test_folder . 当执行到达test_folder时,将执行一次Setup Static Variable关键字。


In your test suite files you can use the variable even without importing the resource file. 在测试套件文件中,即使不导入资源文件也可以使用变量。

test_s1.robot: test_s1.robot:

*** Test Cases ***
My First Test
    Log    ${my_static}

If your variable is a constant and not calculated during run-time, @Justin's answer is what your are looking for. 如果您的变量是一个常量并且在运行时未计算,@ Justin的答案就是您要寻找的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM