N00b issue: Global variables in Robot Framework

I’ve asked this in the VS Code forum, but I see that it’s actually something that I don’t understand, so I’m posting here…

My main robot file declares a variable, ${MESSAGE}, in the *** Variables *** section. This file also includes a keywords file as a resource in the *** Settings *** section. When I open the keywords file in VS Code, it says that the variable is not found.

My main file, “test1.robot”:

*** Settings ***
Documentation  This is Steve's first robot framework script
Resource  keywords.robot

*** Variables ***
${MESSAGE}      Hello World!        # This is a global variable

*** Test Cases ***
Test case 1
    [Documentation]    Write the global message variable to the log  
    keywords.Log our message

My keyword file, “keywords.robot”:

*** Settings ***
Documentation    Keywords for Steve's testing


*** Keywords ***
Log our message
    [Documentation]  This puts our message in the log.
    Log a message  ${MESSAGE}        # Why doesn't VS Code see this global variable?


Log a message
    [Arguments]  ${msg}
    [Documentation]  This puts a message in the log.
    Log  ${msg}

Why doesn’t VS Code see ${MESSAGE} in the keywords file, since I declared it in the file that calls the keywords?

The code appears to run fine… What am I misunderstanding?

If you look at the Variable section in a test case file sub-section in 2.6.5 Variable priorities and scopes, you’ll notice the second paragraph:

Variables created in the Variable sections are available in all other sections in the file where they are created. This means that they can be used also in the Setting section, for example, for importing more variables from resource and variable files.

That means these variables are not considered globals - rather, suite-level variables.

I don’t know what LSP you’re using (whether Robocorp’s now-deprecated LSP or Robotcode, which is still being developed), but you can supply runtime variables with a configuration file to declared global variables. If you want the variables to be independent of your VS Code launch.json you can use other techniques, including a YAML file (my contribution to the secondary docs site).

Thank you… For some reason I thought that the variable would be available for all keywords, even in resource keyword files.

How would I declare a variable as global in the *** Variables *** section of my robot file? I want to declare a variable once, in my main test file so it can be used in various files in my suite. Everything I see online refers to using Set Global Variable in a test case.

FYI, I am using the RobotCode extension.

Due to technical reasons, we’re still using Robocorp’s LSP and not RoboCode, so I can’t entirely guide you here. I do know that the author suggests putting configuration parameters in a robot.toml file (example in codebase).

Another route you could go is using a global setup/teardown, which you can put in a __init__.robot file, in which you could execute a keyword that sets global variables (reference). If you execute Set Global Variable in this context, it should be available to all the suites in that directory.

I hope this helps! :crossed_fingers:

Hi,

From previous attempts to define variables in init.robot files, the variables here are not passed to the tests in the concerned suite. They remain at the init file level I think.

But I use global or suite variables to avoid multiple definition and maintenance in tests files this way:

  • First I would suggest you to rename your keywords file in keywords.resource and call it this way. It’s easier to identify files, and for example Robocop would suggest you to do so
  • Define the variable directly in this file with a variable section (if it is global to the suite)
  • if you’re message is specific by test, then solution I could be in the tests:
    - Define variable with:
    VAR ${WARNING} message scope=SUITE
    - Or pass it has an argument to your keyword

Also last idea with VSCode, for project related variable (that you need to access everywhere), you can also add them in settings.json

Regards
Charlie

Your code is valid and runs, because the suite variables are available to all keywords called by this suite.

However, from VSCode Robotcode point of view, it does not know if all the suite which are going to call your resource file are going to define this MESSAGE variable. So it assumes the variable is missing.

The way to fix this, advised by the author of Robotcode VSCode extension (somewhere I can unfortunately not find now, but most certainly on slack), is to declare the MESSAGE variable in a *** Variables *** section in your resource file, with a default value such as "UNDEFINED VALUE SHOULD BE DEFINED IN TEST SUITE FILE". This way, if you reuse this resource file from another test suite and forget to define MESSAGE, robot framework will at least find the default value.

You can read more about variable scope and overriding here. In particular:

Variable section in a test case file
Variables created using the Variable section in a test case file are available for all the test cases in that file. These variables override possible variables with same names in imported resource and variable files.

As a side note, I would advise calling your resources files “.resource” to underline that they do not have test cases inside and can be imported in other “resource” or in “robot” files. This is mentioned here in the doc:

The recommended extension for resource files is .resource. For backwards compatibility reasons also .robot, .txt and .tsv work, but using .resource may be mandated in the future.

2 Likes