Functions become keywords
Chapter lab: Python you need
This chapter works in a prepared project. Download it before the first task; it unpacks into its own folder.
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
Everything you have imported so far — Collections, Browser, RequestsLibrary
— is a Python library. Now you write one.
When you import a Python module with Library, each of its functions becomes a
keyword. Robot Framework converts the name: order_reference in Python is
Order Reference in a suite. Your libraries live in libraries/; a suite in
checks/ imports one with a relative path, ../libraries/<file>.py.
One wrinkle to know up front: arguments arrive from a suite as text unless
you say otherwise. Passing ${42} sends the integer 42; passing 42 sends the
string "42". (The next lessons make this precise.)
Task 1 — Write a function Robot Framework can call
Section titled “Task 1 — Write a function Robot Framework can call”Goal: create libraries/shop_tools.py with a function
order_reference(prefix, number) that returns a padded reference like
TRN-0042.
Here is the same shape over different data — a warehouse code. Adapt it to build an order reference (four digits, not three).
Worked example — adapt it, don’t copy it
def warehouse_code(zone, number): return f"{zone}-{number:03d}"Done when
libraries/shop_tools.py defines a function order_reference.
Hint 1
A module library is just a .py file with plain functions — no class, no
imports needed here. Define def order_reference(prefix, number):.
Hint 2
Build the string with an f-string and zero-padding to four digits:
return f"{prefix}-{number:04d}".
Solution
def order_reference(prefix, number): return f"{prefix}-{number:04d}"Task 2 — Import it and run it green
Section titled “Task 2 — Import it and run it green”Create checks/orders.robot that imports your library and calls the keyword.
Note the ${42} — it passes the integer 42, which the :04d formatting needs.
*** Settings ***Library ../libraries/shop_tools.py
*** Test Cases ***Order Reference Is Built ${ref}= Order Reference TRN ${42} Should Be Equal ${ref} TRN-0042Run it:
uv run robot checks/orders.robotrobot checks/orders.robotpoetry run robot checks/orders.robotDone when
the test Order Reference Is Built passes.
Check your understanding
Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.
When you import a Python module with
Library, its functions become…Each function in the module becomes a keyword;
order_referenceis called asOrder Reference.Show the answer
Answer: keywords — Each function in the module becomes a keyword;
order_referenceis called asOrder Reference.By default, an argument from a suite reaches your function as…
Arguments arrive as text unless you pass a typed value like
${42}or add a type hint.Show the answer
Answer: text (a string) — Arguments arrive as text unless you pass a typed value like
${42}or add a type hint.
Key terms
- Libraryconcept
- Import a Python module so its functions become keywords.
- keyword namingconcept
snake_casePython names read asTitle Casekeywords.