A static variable file
Chapter lab: Variable files
This chapter works in a prepared project. Download it before the first task; it unpacks into its own folder.
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 library adds keywords. A variable file adds variables. It is a
Python module imported with the Variables setting: every module-level name
becomes a Robot Framework variable.
Why not just a *** Variables *** section? Because a variable file holds real
Python — lists, dictionaries, values computed at import time — and one file can
be shared across many suites. It is the natural home for configuration: base
URLs, lookup tables, per-environment settings.
Task 1 — Write a variable file
Section titled “Task 1 — Write a variable file”Goal: create variables/shop_config.py with three module-level variables — a
scalar BASE_URL, a list, and a dictionary STATUSES.
Here is the same shape over different data — a warehouse config. Adapt it to the shop.
Worked example — adapt it, don’t copy it
WAREHOUSE = "eu-west"ZONES = ["A", "B", "C", "D"]DOCKS = {"inbound": 3, "outbound": 2}Done when
variables/shop_config.py defines BASE_URL (and the list and dict).
Hint 1
A variable file is just module-level assignments — no function, no class:
BASE_URL = "http://localhost:7272".
Hint 2
Add a list and a dict too — these become @{...} and &{...} variables:
CATEGORIES = ["bags", "mugs", "lamps"] and
STATUSES = {"complete": "Delivered", "pending": "In transit"}.
Solution
BASE_URL = "http://localhost:7272"CATEGORIES = ["bags", "mugs", "lamps"]STATUSES = {"complete": "Delivered", "pending": "In transit"}Task 2 — Import it and run it green
Section titled “Task 2 — Import it and run it green”Create checks/shop_config.robot. Import the variable file with Variables
(note: not Library), then use the variables — including dictionary item access:
*** Settings ***Variables ../variables/shop_config.py
*** Test Cases ***Config Values Resolve Should Be Equal ${BASE_URL} http://localhost:7272 Length Should Be ${CATEGORIES} 3 Should Be Equal ${STATUSES}[complete] DeliveredRun it:
uv run robot checks/shop_config.robotrobot checks/shop_config.robotpoetry run robot checks/shop_config.robotDone when
the test Config Values Resolve passes.
Check your understanding
Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.
A variable file supplies…
A variable file's module-level names (or a
get_variablesdict) become Robot Framework variables — the data counterpart to a library's keywords.Show the answer
Answer: variables — A variable file's module-level names (or a
get_variablesdict) become Robot Framework variables — the data counterpart to a library's keywords.How do you import a variable file?
Use the
Variablessetting;Libraryimports keywords andResourceimports Robot-syntax keywords/variables.Show the answer
Answer:
Variables file.py— Use theVariablessetting;Libraryimports keywords andResourceimports Robot-syntax keywords/variables.
Key terms
- Variables (import)concept
- Import a Python file so its values become RF variables.
- variable file vs * Variables *concept
- A variable file holds real Python (lists, dicts, computed) and is reusable across suites.