A dynamic variable file
Chapter lab: Variable files
This lesson continues in the same lab as the lesson before it. If you don’t have it yet, download it here.
Download ch19b-variable-files.zip
Or fetch and unpack it from a terminal in VS Code:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch19b-variable-files.zip && unzip -q ch19b-variable-files.zip && rm ch19b-variable-files.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch19b-variable-files.zip -OutFile ch19b-variable-files.zip; Expand-Archive ch19b-variable-files.zip -DestinationPath .; Remove-Item ch19b-variable-files.zipOpen the folder in VS Code with code rfe-ch19b-variable-files (or File → Open Folder…) and accept the recommended RobotCode and Python extensions. Then set it up:
uv syncpython3 -m venv .venvsource .venv/bin/activatepip install -r requirements.txtpython -m venv .venv.venv\Scripts\Activate.ps1pip install -r requirements.txtKeep the environment active in every terminal where you run lesson commands.
poetry install --no-rootPython 3.13 or newer is required; uv installs it when it is missing.
Open in browserNo setup? Try the lab right here: its files, an editor and a terminal, running in your browser.
What's inside
Directoryrfe-ch19b-variable-files/
Directory.vscode/
- extensions.json
Directorychecks/
- smoke.robot
Directorylibraries/
- README.md
Directoryvariables/
- README.md
- .gitignore
- .python-version
- LAB.md
- pyproject.toml
- README.md
- requirements.txt
- uv.lock
A static variable file is fixed at import time. A dynamic one computes its variables when imported — from an argument, the environment, or a lookup — so a single file can serve many situations.
The mechanism: instead of module-level names, define a function
get_variables() that returns a dictionary. Its keys become the variables.
Give it a parameter and pass a value on the Variables import, and one file can
supply, say, staging or production settings on demand.
Task 1 — Write a get_variables file
Section titled “Task 1 — Write a get_variables file”Goal: create variables/env_config.py with get_variables(env="staging")
that returns a dictionary of settings for the chosen environment (API_URL,
RETRIES), with staging and prod options.
Here is the same shape over different data — subscription tiers. Adapt it to environments.
Worked example — adapt it, don’t copy it
def get_variables(tier="basic"): tiers = { "basic": {"MAX_ITEMS": 10, "SUPPORT": "email"}, "pro": {"MAX_ITEMS": 100, "SUPPORT": "phone"}, } return tiers[tier]Done when
variables/env_config.py defines a get_variables function.
Hint 1
Return a dict from get_variables — its keys become variables:
def get_variables(env="staging"): returning a per-environment dictionary.
Hint 2
Key the settings by environment and select with the argument:
envs = {"staging": {...}, "prod": {...}} then return envs[env].
Solution
def get_variables(env="staging"): envs = { "staging": {"API_URL": "http://staging.local", "RETRIES": 3}, "prod": {"API_URL": "http://prod.local", "RETRIES": 5}, } return envs[env]Task 2 — Import it with an argument and run it green
Section titled “Task 2 — Import it with an argument and run it green”Create checks/env_config.robot. Pass the environment as an argument to the
Variables import — the file computes the matching settings:
*** Settings ***Variables ../variables/env_config.py prod
*** Test Cases ***Environment Values Resolve Should Be Equal ${API_URL} http://prod.local Should Be Equal As Integers ${RETRIES} 5Run it:
uv run robot checks/env_config.robotrobot checks/env_config.robotpoetry run robot checks/env_config.robotDone when
the test Environment Values Resolve passes.
Check your understanding
Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.
A dynamic variable file provides its variables via…
get_variables()returns a dictionary whose keys become variables; it can compute them from an argument or the environment.Show the answer
Answer: a
get_variables()function returning a dict —get_variables()returns a dictionary whose keys become variables; it can compute them from an argument or the environment.How can one variable file serve many environments?
Variables env_config.py prodpassesprodtoget_variables(env), which returns that environment's settings.Show the answer
Answer: give
get_variablesa parameter passed on theVariablesimport —Variables env_config.py prodpassesprodtoget_variables(env), which returns that environment's settings.
Key terms
- get_variablesconcept
- Function returning a dict of variables, computed at import time.
- parameterized variable fileconcept
- Pass an argument on the
Variablesimport to select values (e.g. an environment).