Skip to content

Instantly share code, notes, and snippets.

@GLMeece
Last active December 9, 2022 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GLMeece/79954b3ea2f8efa5f3ed5c2bd6a267b8 to your computer and use it in GitHub Desktop.
Save GLMeece/79954b3ea2f8efa5f3ed5c2bd6a267b8 to your computer and use it in GitHub Desktop.
How to Import YAML Variables into Robot Framework

How to Import YAML Variables into Robot Framework

I recently answered this question on the Robot Framework forum.

For switching between runtime environments, my go-to is to create YAML files that encapsulate essential differences between environments. If you focus on just the things that change, then your maintenance load will be lower.

I like YAML because you can represent the 3 primitive data types supported:

  • Scalar
  • List
  • Dictionary

You'll need to install PyYAML first:

pip install pyyaml

To "slurp up" the variables at runtime, just pass the -V switch, along with the path to the YAML file you want to use (you can use relative or absolute paths - I recommend the former). So, for example, if the environment is 'QA' and you want to use the file called qa_env.yaml at the root of your project, then you'd have a command-line string like this:

robot -V qa_env.yaml mytestdir/

Here's a quick example of what the YAML could look like:

# These are scalars:
base_url: https://qaserver.mycompany.com:8080
admin_user: iAmAdMiN
admin_password: eieioscoobydoo1234
# Now, a list:
my_list:
     - Item 1
     - Item two
     - 3
# Finally, a dictionary:
a_dict:
     key_1: A string
     key_2: 1 # an int

All the data structures are imported at runtime as the "final say" for variable values. As a super-quick example, to assign them to existing variables at runtime, you'd do something like this:

*** Variables ***
${app_url}     ${base_url}
@{the_list}    @{my_list}
&{the_dict}    &{a_dict}

For the official word on this, see Variable file as YAML.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment