简体   繁体   中英

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?

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:

*** 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. This means all test suite files in the folder for example.


Next step is to create your __init__.robot file:

*** 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 . ${my_static} will be accessible in all of them. The Setup Static Variable keyword will be executed once, when the execution reaches the test_folder .


In your test suite files you can use the variable even without importing the resource file.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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