Skip to content

Templates

Chapter lab: Specification stylesch05-specification-styles.zip · 3 KB

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:

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch05-specification-styles.zip && unzip -q ch05-specification-styles.zip && rm ch05-specification-styles.zip

Open 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:

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

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 AGE
Ada Ada 31
Grace Grace 45
*** Keywords ***
Age Is Adult
[Arguments] ${name} ${age}
Should Be True ${age} >= 18

Now do the same for prices in checks/prices.robot:

  1. Set a Test Template in *** Settings ***.
  2. Name the columns after the *** Test Cases *** header, one row per product.
  3. 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 PRICE
Backpack backpack 79.00
Lamp lamp 34.50
Mug mug 19.90
Notebook notebook 12.00
*** Keywords ***
Price Should Be Positive
[Arguments] ${product} ${price}
Should Be True ${price} > 0
Run

Run it:

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

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

  1. A test template turns a suite of near-identical tests into…

    Show the answer

    Answer: one keyword run once per data row — Test Template runs one keyword for each test's data row — data-driven testing.

  2. Where do you set a suite-wide template?

    Show the answer

    Answer: Test Template in *** 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.

All terms in the glossary →