Is it possible to override a list variable in robot?

Viewed 441

Under Variables I've got the list my_list:

*** Variables ***
${my_list}    ['1', '2', '3']

Now I'm wondering if it's possible to override the variable my_list with another list when running the robot test from the command line.

1 Answers

Update:

For the sake of completeness of the answer to the question you can find the updates bellow.

Robot Framework doesn't support variable override using command line variables for types like list and dictionaries. But if needed a list variable defined in *** Variables *** section can be overridden from the command line using --variablefile command line switch. You can find an example bellow:

python_variables.py

LIST__my_real_list_variable = 3, 2
my_real_list_variable2 = [5, 0]

yourRobotTestSuite.robot

*** Variables ***
${my_list}    ['1', '2', '3']
@{my_real_list_variable}    1    2    3 
@{my_real_list_variable2}    1    2    3     

*** Test Cases ***
command line variable overrides
    Log    ${my_list}    
    Log Many    @{my_real_list_variable}
    Should Be True    "${my_list}"=="['8','4']"    
    Should Be True    ${my_real_list_variable[0]}==3    
    Should Be True    ${my_real_list_variable2[0]}==5
    

robot --variable my_list:['8','4'] --variablefile python_variables.py yourRobotTestSuite.robot

Source of info: https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#setting-variables-in-command-line

--variable EXAMPLE:value

--variable HOST:localhost:7272 --variable USER:robot

Variables set from the command line are globally available for all executed test data files, and they also override possible variables with the same names in the Variable section...

To conclude all that I've wrote above: If you want to override a varibale defined in *** Variables *** section defined like this

*** Variables ***
${my_list}    ['1', '2', '3']

The type of ${my_list} is string and not list. But with a little logic in the test or keyword you can do

${my_list}    Evaluate    ${my_list}

in this way now ${my_list} is a real list which can be iterated in a loop for example. The other way to override a list is using --variablefile, but this according to my opinion is the harder way to achieve this "real list" override and requires precondition to create or override the content of python_variables.py before executing the test.

Related