A custom parser
Chapter lab: Shaping data and results
This lesson continues in the same lab as the lesson before it. If you don’t have it yet, download it here.
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
Robot Framework reads .robot files with its own parser. A custom parser
teaches it to read another format — a CSV, a spreadsheet export, a domain file
— and turn it into tests. Now non-programmers can drive tests from data your team
already has.
A parser is a class that declares the extension it handles (EXTENSION = "csv")
and implements parse(self, source), returning a TestSuite built from the
file. You attach it with --parser, then point robot at the data file.
Task 1 — Turn a CSV into tests
Section titled “Task 1 — Turn a CSV into tests”Goal: create ext/OrdersParser.py — a parser for csv files that builds one
test per row of data/orders.csv, each test logging the row’s product.
Here is the same parser over a different column mapping. Adapt it to use the
name and product columns.
Worked example — adapt it, don’t copy it
import csvfrom robot.api import TestSuite
class ProductsParser: EXTENSION = "csv"
def parse(self, source, defaults=None): suite = TestSuite(name="Products From CSV") with open(source, encoding="utf-8") as handle: for row in csv.DictReader(handle): test = suite.tests.create(name=row["title"]) test.body.create_keyword("Log", args=[row["sku"]]) return suiteDone when
ext/OrdersParser.py defines an OrdersParser class.
Hint 1
Declare the extension and start a suite: EXTENSION = "csv" on the class, and in
parse, suite = TestSuite(name="Orders From CSV").
Hint 2
Read the rows and make a test each: for row in csv.DictReader(handle):, then
test = suite.tests.create(name=row["name"]) and
test.body.create_keyword("Log", args=[row["product"]]).
Solution
import csvfrom robot.api import TestSuite
class OrdersParser: EXTENSION = "csv"
def parse(self, source, defaults=None): suite = TestSuite(name="Orders From CSV") with open(source, encoding="utf-8") as handle: for row in csv.DictReader(handle): test = suite.tests.create(name=row["name"]) test.body.create_keyword("Log", args=[row["product"]]) return suiteTask 2 — Run tests from the CSV
Section titled “Task 2 — Run tests from the CSV”Point Robot Framework at the data file, with your parser attached:
uv run robot --parser ext/OrdersParser.py data/orders.csvrobot --parser ext/OrdersParser.py data/orders.csvpoetry run robot --parser ext/OrdersParser.py data/orders.csvEach row of data/orders.csv became a test.
Done when
the parser turns each CSV row into a passing test.
Check your understanding
Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.
A custom parser lets Robot Framework…
A parser implements
parse(source)returning aTestSuite, so data like a CSV becomes tests.Show the answer
Answer: read a non-
.robotformat and build tests from it — A parser implementsparse(source)returning aTestSuite, so data like a CSV becomes tests.How does a parser declare which files it handles?
EXTENSION = "csv"tells Robot Framework to use the parser for.csvsources.Show the answer
Answer: an
EXTENSIONattribute (e.g."csv") —EXTENSION = "csv"tells Robot Framework to use the parser for.csvsources.
Key terms
- parser APIconcept
parse(source)→TestSuite;EXTENSIONdeclares the file type.- --parsercommand
- Attach a parser so
robotcan read a custom format.