Skip to content

A pre-run modifier

Chapter lab: Shaping data and resultsch21-shaping-data-and-results.zip · 4 KB

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:

Terminal window
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.zip

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

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

Where a pre-run modifier actspipeline diagram. --prerunmodifier runs before any test executesparsebuild the suite modelPreRunModifieredits suites before they runexecuteonly the kept tests runreportoutput.xml, log, report--prerunmodifier runs before any test executes
Edit

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]
Run

The bundled checks/orders.robot has two smoke-tagged tests and two that are not. Attach your modifier:

Terminal window
uv run robot --prerunmodifier ext/SmokeOnly.py checks/orders.robot
Terminal window
robot --prerunmodifier ext/SmokeOnly.py checks/orders.robot
Terminal window
poetry run robot --prerunmodifier ext/SmokeOnly.py checks/orders.robot

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

  1. A pre-run modifier changes a run…

    Show the answer

    Answer: before any test executes — A SuiteVisitor attached with --prerunmodifier edits the test data before execution.

  2. Which base class does a pre-run modifier extend?

    Show the answer

    Answer: SuiteVisitor — It is a SuiteVisitor; override start_suite to rewrite suite.tests.

Key terms

SuiteVisitorconcept
Walk the running model; a pre-run modifier subclasses it.
--prerunmodifiercommand
Attach a modifier that edits tests before they run.

All terms in the glossary →