Variables
Chapter lab: Variables and user keywords
This chapter works in a prepared project. Download it before the first task; it unpacks into its own folder.
Download ch04-variables-keywords.zip
Or fetch and unpack it from a terminal in VS Code:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch04-variables-keywords.zip && unzip -q ch04-variables-keywords.zip && rm ch04-variables-keywords.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch04-variables-keywords.zip -OutFile ch04-variables-keywords.zip; Expand-Archive ch04-variables-keywords.zip -DestinationPath .; Remove-Item ch04-variables-keywords.zipOpen the folder in VS Code with code rfe-ch04-variables-keywords (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-ch04-variables-keywords/
Directory.vscode/
- extensions.json
Directorychecks/
- imports.robot
Directoryresources/
- common.resource
- .gitignore
- .python-version
- LAB.md
- pyproject.toml
- README.md
- requirements.txt
- uv.lock
Variables come in three shapes, each with its own prefix:
- Scalar —
${name}holds one value. - List —
@{name}holds an ordered sequence. - Dictionary —
&{name}holds key/value pairs.
Declare them in *** Variables ***, or create them inline with VAR
(Robot Framework 7+) inside a test or keyword. Access a list item with
${list}[0] and a dictionary value with ${dict}[key].
Task 1 — Declare scalar, list, and dictionary variables
Section titled “Task 1 — Declare scalar, list, and dictionary variables”Goal: create checks/variables.robot with one scalar, one list, and one
dictionary variable, and a test that reads all three.
Here is the same shape over different data — a city, some days, and a few ages. Adapt the names and values to the shop.
Worked example — adapt it, don’t copy it
*** Variables ***${CITY} Helsinki@{DAYS} mon tue wed&{AGES} ada=31 grace=45
*** Test Cases ***Values Are Set Should Be Equal ${CITY} Helsinki Length Should Be ${DAYS} 3 Should Be Equal ${AGES}[ada] 31Done when
checks/variables.robot defines ${SHOP}, @{PRODUCTS}, and
&{PRICES}, and the assertions pass.
Hint 1
Declare a scalar ${SHOP}, a list @{PRODUCTS}, and a dictionary &{PRICES} in
the *** Variables *** section.
Hint 2
Read them in a test: Should Be Equal ${SHOP} RF Demo Shop,
Length Should Be ${PRODUCTS} 3, and dictionary access
${PRICES}[backpack].
Hint 3
Create one more variable inline with VAR ${note} checked inside a test.
Solution
*** Variables ***${SHOP} RF Demo Shop@{PRODUCTS} backpack lamp mug&{PRICES} backpack=79 lamp=34 mug=19
*** Test Cases ***Variables Are Defined Should Be Equal ${SHOP} RF Demo Shop Length Should Be ${PRODUCTS} 3 Should Be Equal ${PRICES}[backpack] 79
Var Creates Inline VAR ${note} checked Should Be Equal ${note} checkedTask 2 — Assert them and pass
Section titled “Task 2 — Assert them and pass”Run the suite:
uv run robot checksrobot checkspoetry run robot checksDone when
the test Variables Are Defined passes.
Check your understanding
Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.
Which prefix declares a list variable?
${x}is a scalar,@{x}a list,&{x}a dictionary.Show the answer
Answer:
@{name}—${x}is a scalar,@{x}a list,&{x}a dictionary.What does
VARdo?VAR(Robot Framework 7+) creates a variable inline inside a test or keyword, with an optional scope.Show the answer
Answer: Creates a variable inline, in the current scope —
VAR(Robot Framework 7+) creates a variable inline inside a test or keyword, with an optional scope.
Key terms
- ${} @{} &{}concept
- Scalar, list, and dictionary variable prefixes.
- VARkeyword
- RF 7+ inline variable creation with optional scope (local/TEST/SUITE/GLOBAL).