Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 36 ยท Data-driven Testing

External test data: CSV, JSON and YAML

Load test data from CSV, JSON and YAML files in Robot Framework: DataDriver, variable files, Python helpers and a runnable stand-in that reads the data.

Rows in a .robot file are fine for a handful of cases. When data grows to hundreds of rows, or when testers and analysts maintain it in a spreadsheet, you want it outside the test file. Robot Framework offers several ways: variable files, the built-in library keywords, and the DataDriver library for CSV and Excel.

This lesson compares the options, shows the real syntax read-only, and uses Python stand-ins (real Pyodide) to read CSV, JSON and a simple YAML-like format so you can see the processing.

Option 1: variable files

A variable file is a Python file (or YAML or JSON) whose names become Robot variables. Import it in Settings:

TEXT
*** Settings ***
Variables    users.yaml
YAML
# users.yaml
ADMIN:
  name: ada
  password: s3cret
GUEST:
  name: guest
  password: guest

Inside tests, ${ADMIN}[name] reads a value. YAML needs pip install pyyaml. A .py variable file can compute values or read environment variables, which suits secrets:

Python
# env.py
import os
BASE_URL = os.environ.get("BASE_URL", "https://staging.example.test")

You can also pick one at run time: robot --variablefile envs/staging.py tests/.

Option 2: read a file inside a test

For a CSV of rows, read it with a keyword and loop. A custom Python keyword (see custom libraries) is the cleanest way:

Python
# CsvData.py
import csv

def read_csv_rows(path):
    with open(path, newline="", encoding="utf-8") as f:
        return list(csv.DictReader(f))
TEXT
*** Settings ***
Library    CsvData.py

*** Test Cases ***
Check All Products
    ${rows}=    Read Csv Rows    data/products.csv
    FOR    ${row}    IN    @{rows}
        Product Should Be Listed    ${row}[name]    ${row}[price]
    END

The downside: it is one test case. If row 3 fails, the loop stops there and you only see the first failure.

Option 3: DataDriver

DataDriver (pip install robotframework-datadriver) turns every CSV row into its own test case, using a template keyword you write once:

TEXT
*** Settings ***
Library          DataDriver    file=data/logins.csv
Test Template    Login Should Give Result
TEXT
*** Test Cases ***
Login Scenarios    ${user}    ${password}    ${result}

with a CSV like:

TEXT
*** Test Cases ***,${user},${password},${result}
Valid login,demo,s3cret,Welcome
Wrong password,demo,nope,Invalid password
Empty user,,s3cret,Username required

The first column is the test name; the following columns fill the variables named in the header. Excel files work the same way. DataDriver has more settings (a different delimiter, encodings, filtering by tag), which are in its documentation.

Where does the data live?

In the test Few rows, templates.
Variable file Config and named data sets.
CSV or Excel Many rows, shared with others.
Database or API Data that changes daily.
1 / 2

Reading data in Python: stand-ins

The real keywords above need files on disk. These stand-ins read data from strings, so they run anywhere. First, CSV: csv.DictReader turns each row into a dictionary keyed by the header, which is exactly what ${row}[name] uses in Robot.

Python Stand-in: CSV rows as dictionaries

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice every value arrives as a string. Robot is the same: CSV cells are text, so convert with Convert To Number or Convert To Integer, or do the conversion in your Python helper.

JSON data and nested values

JSON suits nested data and is easy for APIs to produce. This stand-in loads a document and flattens it into test cases, showing how one file can describe inputs and expected results together.

Python Stand-in: JSON test cases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Do not put real customer data, passwords or tokens in test data files that are committed to version control. Use made-up data, and load secrets from environment variables or a secret store.

Designing good test data

  • Each row is one idea. A row called "empty username" should change only the username.
  • Include expected results next to the input.
  • Cover boundaries: 0, 1, the maximum, one over the maximum.
  • Keep data and code apart so a tester can add a row without touching keywords.
  • Name the columns clearly, the way you name variables.
  • Avoid dependence on live data that changes (today's date, a product that may be sold out). Create what you need in the setup.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does DataDriver do with each CSV row?

    DataDriver generates one test case per row and runs the template keyword for each.
  2. 2. What type are the values read from a CSV cell?

    CSV stores text, so convert numbers explicitly.
  3. 3. Why is a FOR loop over CSV rows in one test a weaker choice than DataDriver?

    In a single test the first failing row ends the test; DataDriver gives every row its own result.
  4. 4. Where is a good place to keep a secret password for tests?

    Secrets must stay out of version control; pass them in at run time.

Summary

  • Variable files hold named data and per-environment settings.
  • A custom keyword can read a CSV; a FOR loop makes one test with a single failure point.
  • DataDriver turns every CSV or Excel row into its own test case.
  • CSV values are strings; convert them when you need numbers.
  • Keep data free of real secrets and make each row test one idea.

Next: Generating test data and test matrices