user2381567 user2381567 - 4 months ago 76
Python Question

How to use extra arguments passed with Variable file - Robot framework

In Robot Framework user guide there is a section that describes how to pass variable files and also some possible variables if needed.

Example:

pybot --variablefile taking_arguments.py:arg1:arg2



My question is can i use these possible variables arg1 and arg2 in the taking_arguments.py file afterwards and if i can then how?

Right now i have this:

pybot --variablefile taking_arguments.py:arg1:arg2


taking_arguments.py contents:

IP_PREFIX = arg1


But that results in

NameError: name 'arg1' is not defined

Answer

The only way to use variables in an argument file using the --variablefile filename.py:arg1:arg2 syntax is to have your variable file implement the function get_variables. This function will be passed the arguments you specify on the command line, and must return a dictionary of variable names and values.

For example, consider the following variable file, named "variables.py":

def get_variables(arg1, arg2):
    variables = {"argument 1": arg1,
                 "argument 2": arg2,
                }
    return variables

This file creates two robot variables, named ${argument 1} and ${argument 2}. The values for these variables will be the values of the arguments that were passed in. You might use this variable file like this:

pybot --variablefile variables.py:one:two ...

In this case, the strings "one" and "two" will be passed to get_variables as the two arguments. These will then be associated with the two variables, resulting in ${argument 1} being set to one and ${argument 2} being set to two.