Scopes, priorities, and built-in variables
Chapter lab: Variables and control flow
This chapter works in a prepared project. Download it before the first task; it unpacks into its own folder.
Download ch07-control-flow.zip
Or fetch and unpack it from a terminal in VS Code:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch07-control-flow.zip && unzip -q ch07-control-flow.zip && rm ch07-control-flow.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch07-control-flow.zip -OutFile ch07-control-flow.zip; Expand-Archive ch07-control-flow.zip -DestinationPath .; Remove-Item ch07-control-flow.zipOpen the folder in VS Code with code rfe-ch07-control-flow (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-ch07-control-flow/
Directory.vscode/
- extensions.json
Directorychecks/
Directorycatalog/
- catalog.robot
Directorysmoke/
- smoke.robot
- .gitignore
- .python-version
- LAB.md
- pyproject.toml
- README.md
- requirements.txt
- uv.lock
Robot Framework gives every run a set of built-in variables — automatic
values you can read without declaring them: ${TEST_NAME}, ${CURDIR} (the
suite’s folder), ${OUTPUT_DIR}, and more.
Variables also have scope and priority. A VAR can be local (default),
TEST, SUITE, or GLOBAL. And a value set on the command line with
--variable NAME:value overrides the *** Variables *** section, so you
can retarget a run without editing files.
Task 1 — Use a built-in variable and a scoped VAR
Section titled “Task 1 — Use a built-in variable and a scoped VAR”Goal: create checks/scopes.robot with a test that reads a built-in
variable, and another that creates a SUITE-scoped variable with VAR.
Here is the same idea with different built-ins and a different value. Adapt them.
Worked example — adapt it, don’t copy it
*** Test Cases ***Reads Built In Values Should Not Be Empty ${SUITE_NAME} Log Output goes to ${OUTPUT_DIR}
Var With Scope VAR ${token} abc123 scope=SUITE Should Be Equal ${token} abc123Done when
checks/scopes.robot defines the test Built In Variables Exist,
and it passes.
Hint 1
Read a built-in without declaring it: Should Not Be Empty ${TEST_NAME} and
Log Running from ${CURDIR}.
Hint 2
Create a scoped variable with VAR ${shared} hello scope=SUITE, then
assert it equals hello.
Solution
*** Test Cases ***Built In Variables Exist Should Not Be Empty ${TEST_NAME} Log Running from ${CURDIR}
Scoped Var Is Set VAR ${shared} hello scope=SUITE Should Be Equal ${shared} helloTask 2 — Run the suite green
Section titled “Task 2 — Run the suite green”Run it:
uv run robot checksrobot checkspoetry run robot checksDone when
the test Built In Variables Exist passes.
Check your understanding
Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.
Which is a built-in (automatic) variable?
Robot Framework provides automatic variables like
${TEST_NAME},${CURDIR}, and${OUTPUT_DIR}.Show the answer
Answer:
${TEST_NAME}— Robot Framework provides automatic variables like${TEST_NAME},${CURDIR}, and${OUTPUT_DIR}.A command-line
--variable NAME:valuebeats…CLI variables have higher priority than the Variables section, so they override defaults at run time.
Show the answer
Answer: a value set in the
*** Variables ***section — CLI variables have higher priority than the Variables section, so they override defaults at run time.
Key terms
- built-in variablesconcept
- Automatic values like ${TEST_NAME}, ${CURDIR}, ${OUTPUT_DIR}.
- variable priorityconcept
- CLI --variable > Variables section; scopes: local < TEST < SUITE < GLOBAL.