Skip to content

Scopes, priorities, and built-in variables

Chapter lab: Variables and control flowch07-control-flow.zip · 3 KB

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:

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch07-control-flow.zip && unzip -q ch07-control-flow.zip && rm ch07-control-flow.zip

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

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

VAR scope: how far a value reachesstack diagram. CLI --variable NAME:value overrides the *** Variables *** sectionGLOBALthe whole run, every suiteSUITEthis suite and its testsTESTthis test onlylocaldefault: one keyword or blockCLI --variable NAME:value overrides the *** Variables *** section

Task 1 — Use a built-in variable and a scoped VAR

Section titled “Task 1 — Use a built-in variable and a scoped VAR”
Edit

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

Done 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} hello
Run

Run it:

Terminal window
uv run robot checks
Terminal window
robot checks
Terminal window
poetry run robot checks

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

  1. Which is a built-in (automatic) variable?

    Show the answer

    Answer: ${TEST_NAME} — Robot Framework provides automatic variables like ${TEST_NAME}, ${CURDIR}, and ${OUTPUT_DIR}.

  2. A command-line --variable NAME:value beats…

    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.

All terms in the glossary →