Skip to content

A dynamic variable file

Chapter lab: Variable filesch19b-variable-files.zip · 4 KB

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:

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch19b-variable-files.zip && unzip -q ch19b-variable-files.zip && rm ch19b-variable-files.zip

Open 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:

Terminal window
uv sync
Terminal window
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Keep the environment active in every terminal where you run lesson commands.

Terminal window
poetry install --no-root

Python 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.

Edit

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”
Run

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} 5

Run it:

Terminal window
uv run robot checks/env_config.robot
Terminal window
robot checks/env_config.robot
Terminal window
poetry run robot checks/env_config.robot

Done 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.

  1. A dynamic variable file provides its variables via…

    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.

  2. How can one variable file serve many environments?

    Show the answer

    Answer: give get_variables a parameter passed on the Variables import — Variables env_config.py prod passes prod to get_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 Variables import to select values (e.g. an environment).

All terms in the glossary →