Data and failures
Chapter lab: Python you need
This lesson continues in the same lab as the lesson before it. If you don’t have it yet, download it here.
Download ch17-python-basics.zip
Or fetch and unpack it from a terminal in VS Code:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch17-python-basics.zip && unzip -q ch17-python-basics.zip && rm ch17-python-basics.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch17-python-basics.zip -OutFile ch17-python-basics.zip; Expand-Archive ch17-python-basics.zip -DestinationPath .; Remove-Item ch17-python-basics.zipOpen the folder in VS Code with code rfe-ch17-python-basics (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-ch17-python-basics/
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
Two things a useful keyword does: it looks things up, and it fails clearly when something is wrong.
- A dictionary maps keys to values —
{"complete": "Delivered"}— and is the natural way to look a value up by name. - A keyword fails by raising
AssertionErrorwith a message. Robot Framework turns that into a failed test carrying your message, exactly like a built-in assertion. Returning normally means the keyword passed.
Task 1 — Look up a value, or fail clearly
Section titled “Task 1 — Look up a value, or fail clearly”Goal: create libraries/order_rules.py with a function status_label(status)
that returns a human label for a known order status and raises AssertionError
for an unknown one.
Here is the same idea over different data — priority tiers. Adapt it to order
statuses (complete → Delivered, pending → In transit, cancelled →
Cancelled).
Worked example — adapt it, don’t copy it
TIERS = {"low": "green", "normal": "amber", "urgent": "red"}
def tier_color(tier): if tier not in TIERS: raise AssertionError(f"Unknown priority tier: {tier}") return TIERS[tier]Done when
libraries/order_rules.py defines a function status_label.
Hint 1
Put your statuses in a module-level dict, then check membership before returning:
if status not in LABELS: raise, otherwise return LABELS[status].
Hint 2
Fail with a clear message so a failing test reads well:
raise AssertionError(f"Unknown order status: {status}").
Solution
LABELS = {"complete": "Delivered", "pending": "In transit", "cancelled": "Cancelled"}
def status_label(status): if status not in LABELS: raise AssertionError(f"Unknown order status: {status}") return LABELS[status]Task 2 — Import it and run it green
Section titled “Task 2 — Import it and run it green”Create checks/rules.robot:
*** Settings ***Library ../libraries/order_rules.py
*** Test Cases ***Known Status Has A Label ${label}= Status Label complete Should Be Equal ${label} DeliveredRun it:
uv run robot checks/rules.robotrobot checks/rules.robotpoetry run robot checks/rules.robotDone when
the test Known Status Has A Label passes.
Check your understanding
Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.
How does a Python keyword report a failure to Robot Framework?
Raising
AssertionErrorfails the test with your message; returning normally means it passed.Show the answer
Answer:
raise AssertionError(message)— RaisingAssertionErrorfails the test with your message; returning normally means it passed.A dictionary is the natural structure for…
{"complete": "Delivered"}maps a key to a value — a lookup by name.Show the answer
Answer: looking a value up by name —
{"complete": "Delivered"}maps a key to a value — a lookup by name.
Key terms
- raise AssertionErrorconcept
- How a Python keyword fails a test, carrying a message.
- dict lookupconcept
- Map keys to values; check membership before indexing.