A pre-run modifier
Chapter lab: Shaping data and results
This chapter works in a prepared project. Download it before the first task; it unpacks into its own folder.
Download ch21-shaping-data-and-results.zip
Or fetch and unpack it from a terminal in VS Code:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch21-shaping-data-and-results.zip && unzip -q ch21-shaping-data-and-results.zip && rm ch21-shaping-data-and-results.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch21-shaping-data-and-results.zip -OutFile ch21-shaping-data-and-results.zip; Expand-Archive ch21-shaping-data-and-results.zip -DestinationPath .; Remove-Item ch21-shaping-data-and-results.zipOpen the folder in VS Code with code rfe-ch21-shaping-data-and-results (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-ch21-shaping-data-and-results/
Directory.vscode/
- extensions.json
Directorychecks/
- orders.robot
Directorydata/
- orders.csv
Directoryext/
- README.md
- .gitignore
- .python-version
- LAB.md
- pyproject.toml
- README.md
- requirements.txt
- uv.lock
A listener observes a run. A pre-run modifier changes it — before any
test executes. It is a SuiteVisitor: Robot Framework walks your suites and
calls your visit methods, and you edit what is there. Select a subset, skip
tests, add tags, reorder — all decided from code, at run time.
You attach one with --prerunmodifier. Overriding start_suite(self, suite)
lets you rewrite suite.tests before the suite runs.
Task 1 — Keep only the smoke tests
Section titled “Task 1 — Keep only the smoke tests”Goal: create ext/SmokeOnly.py — a SuiteVisitor whose start_suite keeps
only the tests tagged smoke.
Here is the same base class doing a different edit — adding a tag. Adapt it to filter to smoke-tagged tests instead.
Worked example — adapt it, don’t copy it
from robot.api import SuiteVisitor
class TagOrders(SuiteVisitor): def start_suite(self, suite): for test in suite.tests: if "Order" in test.name: test.tags.add("orders")Done when
ext/SmokeOnly.py defines a SuiteVisitor.
Hint 1
Subclass the visitor and override start_suite:
class SmokeOnly(SuiteVisitor): with def start_suite(self, suite):.
Hint 2
Replace the suite’s tests with just the smoke-tagged ones:
suite.tests = [t for t in suite.tests if "smoke" in t.tags].
Solution
from robot.api import SuiteVisitor
class SmokeOnly(SuiteVisitor): def start_suite(self, suite): suite.tests = [test for test in suite.tests if "smoke" in test.tags]Task 2 — Run with your modifier
Section titled “Task 2 — Run with your modifier”The bundled checks/orders.robot has two smoke-tagged tests and two that are
not. Attach your modifier:
uv run robot --prerunmodifier ext/SmokeOnly.py checks/orders.robotrobot --prerunmodifier ext/SmokeOnly.py checks/orders.robotpoetry run robot --prerunmodifier ext/SmokeOnly.py checks/orders.robotOnly the two smoke tests run — the others were removed before execution.
Done when
with the modifier, only the smoke-tagged tests run.
Check your understanding
Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.
A pre-run modifier changes a run…
A
SuiteVisitorattached with--prerunmodifieredits the test data before execution.Show the answer
Answer: before any test executes — A
SuiteVisitorattached with--prerunmodifieredits the test data before execution.Which base class does a pre-run modifier extend?
It is a
SuiteVisitor; overridestart_suiteto rewritesuite.tests.Show the answer
Answer:
SuiteVisitor— It is aSuiteVisitor; overridestart_suiteto rewritesuite.tests.
Key terms
- SuiteVisitorconcept
- Walk the running model; a pre-run modifier subclasses it.
- --prerunmodifiercommand
- Attach a modifier that edits tests before they run.