Templates
Chapter lab: Specification styles
This chapter works in a prepared project. Download it before the first task; it unpacks into its own folder.
Download ch05-specification-styles.zip
Or fetch and unpack it from a terminal in VS Code:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch05-specification-styles.zip && unzip -q ch05-specification-styles.zip && rm ch05-specification-styles.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch05-specification-styles.zip -OutFile ch05-specification-styles.zip; Expand-Archive ch05-specification-styles.zip -DestinationPath .; Remove-Item ch05-specification-styles.zipOpen the folder in VS Code with code rfe-ch05-specification-styles (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-ch05-specification-styles/
Directory.vscode/
- extensions.json
Directorychecks/
- prices.robot
- .gitignore
- .python-version
- LAB.md
- pyproject.toml
- README.md
- requirements.txt
- uv.lock
When several tests do the same thing with different data, a template
collapses them into one keyword run once per row. You set Test Template in
*** Settings ***, name the columns after the test-case header, and list one
data row per test.
This chapter’s workspace has checks/prices.robot — four near-identical price
tests. Download this chapter’s lab, then rewrite it as one template with four rows.
Task 1 — Collapse the repetitive suite into one template
Section titled “Task 1 — Collapse the repetitive suite into one template”Goal: rewrite checks/prices.robot as a single template that runs one
keyword per product, checking each price is positive.
Here is the same idea over different data — a template that checks a couple of people are adults. Adapt its shape; don’t copy it.
Worked example — adapt it, don’t copy it
*** Settings ***Test Template Age Is Adult
*** Test Cases *** NAME AGEAda Ada 31Grace Grace 45
*** Keywords ***Age Is Adult [Arguments] ${name} ${age} Should Be True ${age} >= 18Now do the same for prices in checks/prices.robot:
- Set a
Test Templatein*** Settings ***. - Name the columns after the
*** Test Cases ***header, one row per product. - Define the template keyword with
[Arguments]for the product and its price.
Done when
the tests in checks/prices.robot use a template, and all four pass.
Hint 1
Set Test Template Price Should Be Positive in the *** Settings ***
section — it applies to every test in the suite.
Hint 2
Under *** Test Cases ***, write the column header (e.g. PRODUCT PRICE),
then one row per product: a test name, the slug, and the price.
Hint 3
Define the keyword Price Should Be Positive with
[Arguments] ${product} ${price} and assert Should Be True ${price} > 0.
Solution
*** Settings ***Documentation Prices, checked with one template.Test Template Price Should Be Positive
*** Test Cases *** PRODUCT PRICEBackpack backpack 79.00Lamp lamp 34.50Mug mug 19.90Notebook notebook 12.00
*** Keywords ***Price Should Be Positive [Arguments] ${product} ${price} Should Be True ${price} > 0Task 2 — Run the templated tests green
Section titled “Task 2 — Run the templated tests green”Run it:
uv run robot checksrobot checkspoetry run robot checksFour tests run, one per row.
Done when
all four templated tests pass.
Check your understanding
Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.
A test template turns a suite of near-identical tests into…
Test Templateruns one keyword for each test's data row — data-driven testing.Show the answer
Answer: one keyword run once per data row —
Test Templateruns one keyword for each test's data row — data-driven testing.Where do you set a suite-wide template?
Test Template <Keyword>in Settings applies to every test;[Template]applies to one test.Show the answer
Answer:
Test Templatein*** Settings ***—Test Template <Keyword>in Settings applies to every test;[Template]applies to one test.
Key terms
- Test Templateconcept
- Runs one keyword per test row — data-driven style, set in Settings.
- [Template]concept
- Applies a template keyword to a single test case.