Skip to content

Data and failures

Chapter lab: Python you needch17-python-basics.zip · 4 KB

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:

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch17-python-basics.zip && unzip -q ch17-python-basics.zip && rm ch17-python-basics.zip

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

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-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 AssertionError with 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”
Edit

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

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

Run it:

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

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

  1. How does a Python keyword report a failure to Robot Framework?

    Show the answer

    Answer: raise AssertionError(message) — Raising AssertionError fails the test with your message; returning normally means it passed.

  2. A dictionary is the natural structure for…

    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.

All terms in the glossary →