Skip to content

Functions become keywords

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

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:

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

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

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}"
Run

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

Run it:

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

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

  1. When you import a Python module with Library, its functions become…

    Show the answer

    Answer: keywords — Each function in the module becomes a keyword; order_reference is called as Order Reference.

  2. By default, an argument from a suite reaches your function as…

    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_case Python names read as Title Case keywords.

All terms in the glossary →