Skip to content

A static variable file

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

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:

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

Edit

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"}
Run

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] Delivered

Run it:

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

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

  1. A variable file supplies…

    Show the answer

    Answer: variables — A variable file's module-level names (or a get_variables dict) become Robot Framework variables — the data counterpart to a library's keywords.

  2. How do you import a variable file?

    Show the answer

    Answer: Variables file.py — Use the Variables setting; Library imports keywords and Resource imports 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.

All terms in the glossary →