Skip to content

A custom parser

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

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:

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

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.

Edit

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 csv
from 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 suite

Done 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 csv
from 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 suite
Run

Point Robot Framework at the data file, with your parser attached:

Terminal window
uv run robot --parser ext/OrdersParser.py data/orders.csv
Terminal window
robot --parser ext/OrdersParser.py data/orders.csv
Terminal window
poetry run robot --parser ext/OrdersParser.py data/orders.csv

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

  1. A custom parser lets Robot Framework…

    Show the answer

    Answer: read a non-.robot format and build tests from it — A parser implements parse(source) returning a TestSuite, so data like a CSV becomes tests.

  2. How does a parser declare which files it handles?

    Show the answer

    Answer: an EXTENSION attribute (e.g. "csv") — EXTENSION = "csv" tells Robot Framework to use the parser for .csv sources.

Key terms

parser APIconcept
parse(source) → TestSuite; EXTENSION declares the file type.
--parsercommand
Attach a parser so robot can read a custom format.

All terms in the glossary →