2

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).

5
  • 1
    Yes, i'm setting the variable to be the time at which it starts Commented Jan 9, 2019 at 14:06
  • In that case you can check out my answer. Commented Jan 9, 2019 at 14:11
  • Are you asking for something truly static, where robot will throw an error if you try to change the value after it's been set? Commented Jan 9, 2019 at 15:04
  • @BryanOakley that's not the issue i'm trying to fix but will be curious to see how you can implement such a feature Commented Jan 9, 2019 at 15:44
  • So, by "static" you're just asking how to set a variable, and don't care that it's unchangeable? I'm just trying to understand what you mean by the word "static". Commented Jan 9, 2019 at 15:57

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Thanks for the reply Bence, i've tried your solution out but it doesn't seem to like it. I'm getting the erro: "My First Test | FAIL | Variable '${my_static}' not found." I am using RIDE as my editor, is the init.robot file correct? I've looked around and some people are saying the you shouldn't call a resource file in the init file?
@WorkerBee How do you start the test? In the log file can you see the Suite Setup executed? Once you have the init.py you have to start your suites from that folder. So in my example with the following command line option: --suite test_folder.test_s1. About the resource file in the init file, I use it like that myself, did not notice any problem with it so far. I will look around too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.