Skip to content

User keywords and RETURN

Chapter lab: Variables and user keywordsch04-variables-keywords.zip · 3 KB

This lesson continues in the same lab as the lesson before it. If you don’t have it yet, download it here.

Download ch04-variables-keywords.zip

Or fetch and unpack it from a terminal in VS Code:

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch04-variables-keywords.zip && unzip -q ch04-variables-keywords.zip && rm ch04-variables-keywords.zip

Open the folder in VS Code with code rfe-ch04-variables-keywords (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-ch04-variables-keywords/
    • Directory.vscode/
      • extensions.json
    • Directorychecks/
      • imports.robot
    • Directoryresources/
      • common.resource
    • .gitignore
    • .python-version
    • LAB.md
    • pyproject.toml
    • README.md
    • requirements.txt
    • uv.lock

A user keyword packages a sequence of steps under a readable name. It can take arguments, document itself, and hand a value back with RETURN. Good keywords read as action phrases — Discounted Price, not dp — so the tests that call them read as sentences.

Task 1 — Write a user keyword that returns a value

Section titled “Task 1 — Write a user keyword that returns a value”
Edit

Goal: in checks/keywords.robot, write a Discounted Price keyword that takes a price and an optional discount percent and returns the discounted price, plus tests that call it with and without the discount.

Here is a keyword of the same shape over different arithmetic — adding tax. Adapt its structure; the math is yours to change.

Worked example — adapt it, don’t copy it

*** Test Cases ***
Adds Ten Percent Tax
${total}= With Tax 100 10
Should Be Equal As Numbers ${total} 110
*** Keywords ***
With Tax
[Documentation] Add a percentage tax to an amount.
[Arguments] ${amount} ${percent}
${result}= Evaluate ${amount} * (1 + ${percent} / 100)
RETURN ${result}

Done when

checks/keywords.robot defines the user keyword Discounted Price, and calling it with a discount passes.

Hint 1

In *** Keywords ***, declare Discounted Price with [Arguments] ${price} ${percent}=0 — the =0 makes the discount optional.

Hint 2

Compute the result with Evaluate ${price} * (1 - ${percent} / 100) and hand it back with RETURN ${result}.

Hint 3

Call it two ways: Discounted Price 100 (uses the default 0) and Discounted Price 100 50 (half off).

Solution
*** Test Cases ***
Full Price With No Discount
${price}= Discounted Price 100
Should Be Equal As Numbers ${price} 100
Half Price
${price}= Discounted Price 100 50
Should Be Equal As Numbers ${price} 50
*** Keywords ***
Discounted Price
[Documentation] Apply a percentage discount to a price.
[Arguments] ${price} ${percent}=0
${result}= Evaluate ${price} * (1 - ${percent} / 100)
RETURN ${result}

Task 2 — Call it with and without the optional argument

Section titled “Task 2 — Call it with and without the optional argument”
Run

Run it:

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

One test uses the default (0), one passes 50.

Done when

the test Half Price passes.

Check your understanding

Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.

  1. How does a user keyword hand a value back to its caller?

    Show the answer

    Answer: The RETURN statement — RETURN ${value} returns from a user keyword (and can end it early).

  2. A good user-keyword name is…

    Show the answer

    Answer: a readable action phrase, like Discounted Price — Keywords read as sentences; name them as clear action phrases in Title Case.

Key terms

RETURNkeyword
Returns a value from a user keyword; can also end it early.
keyword namingconcept
Readable action phrases in Title Case — keywords read as sentences.

All terms in the glossary →