User keywords and RETURN
Chapter lab: Variables and user keywords
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:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch04-variables-keywords.zip && unzip -q ch04-variables-keywords.zip && rm ch04-variables-keywords.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch04-variables-keywords.zip -OutFile ch04-variables-keywords.zip; Expand-Archive ch04-variables-keywords.zip -DestinationPath .; Remove-Item ch04-variables-keywords.zipOpen 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:
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-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”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 it:
uv run robot checksrobot checkspoetry run robot checksOne 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.
How does a user keyword hand a value back to its caller?
RETURN ${value}returns from a user keyword (and can end it early).Show the answer
Answer: The
RETURNstatement —RETURN ${value}returns from a user keyword (and can end it early).A good user-keyword name is…
Keywords read as sentences; name them as clear action phrases in Title Case.
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.