Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 36 ยท Data-driven Testing

Template tests for data-driven testing

Write data-driven tests in Robot Framework with [Template] and Test Template: one keyword, many rows of data, clear failures per row and a runnable stand-in.

Suppose you must check a login form with twenty combinations of username and password. Writing twenty near-identical test cases is boring and hard to maintain. Data-driven testing separates the steps (what the test does) from the data (what values it uses). Robot Framework supports it directly with templates.

A template test case names one keyword, and every row in the test body is the arguments for one call of that keyword. This lesson shows [Template], the suite-wide Test Template, how failures are reported, and when rows should be separate tests. The real Robot syntax is read-only; the runnable examples are Python stand-ins that mimic what Robot does with the rows.

The idea in one example

TEXT
*** Test Cases ***
Login With Invalid Credentials
    [Template]    Login Should Fail
    demo          wrong        Invalid password
    unknown       s3cret       Unknown user
    ${EMPTY}      s3cret       Username required
    demo          ${EMPTY}     Password required

*** Keywords ***
Login Should Fail
    [Arguments]    ${user}    ${password}    ${message}
    Input Text        id:username    ${user}
    Input Password    id:password    ${password}
    Click Button      id:login
    Page Should Contain    ${message}

There is one test case in the file. Robot runs the template keyword four times, once per row, with the cells as arguments. Rows are separated from the template line by nothing special: the [Template] setting just turns every following row into a call.

Steps and data apart

Keyword The steps, written once.
Rows One row, one set of values.
Template Robot calls the keyword per row.

Adding a case means adding a line, not a test.

1 / 2

Rows keep going after a failure

An important detail: with a template, if row 2 fails, rows 3 and 4 still run. The test fails at the end, and its message reports each failing row. That is different from a normal test case, where the first failing keyword stops everything. For data-driven checks that is exactly what you want: one bad value does not hide the others.

Test Template for a whole file

If every test in a file uses a template, set it once in the settings:

TEXT
*** Settings ***
Test Template    Calculate Should Give

*** Test Cases ***              A      B      Expected
Add small numbers               1      2      3
Add negatives                   -4     -6     -10
Add zero                        0      9      9

Each test case now has a name (good for reports) and one row of data. This layout, one test per row, is the most popular: failures show up under a meaningful name, and you can run a single case with --test "Add zero". The columns of a header-like row are ignored, so the aligned A B Expected heading above is only documentation.

A test can override the suite template with its own [Template], and [Template] NONE switches it off for that test.

Template or separate tests?

Use a template when Use separate tests when
The steps are identical, only values change The steps differ
You want every row to run even after a failure A failure should stop the rest
Cases are many and small Each case needs its own setup
Rows are easy to read as a table You want a distinct name and tags per case

A template runner you can run

This stand-in shows precisely what Robot does: call one function per row, catch a failure, continue, and fail the test at the end with a summary. It is Python, not Robot, but the behavior matches.

Python Stand-in: run a keyword per row

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.

Both bad rows are reported, and the good rows still ran. That is the template behavior.

Name each case instead

The second layout, one named test per row, is easy to model too. Here each case becomes its own result, so you can see which names fail. Notice the same keyword function is reused.

Python Stand-in: one named test per row

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.

Good habits

  • Make the template keyword read like a sentence: Login Should Fail, Calculate Should Give.
  • Keep expected values in the row, so the data tells the whole story.
  • Use ${EMPTY} for an empty cell and ${SPACE} for a space; a plain empty cell is treated as the end of the argument list.
  • Put data in a file when rows grow: see external test data.
  • Combine with tags such as smoke or negative to select groups of data-driven tests.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does [Template] Login Should Fail do to the rows below it?

    A template test case calls the named keyword once per row, using the cells as arguments.
  2. 2. What happens when row 2 of a template test fails?

    Template rows all run, then Robot reports every failing row.
  3. 3. How do you pass an empty argument in a row?

    A blank cell would end the argument list, so ${EMPTY} is the explicit empty string.
  4. 4. Which layout gives each data row its own named test in the report?

    With Test Template, every test case has a name and one row of data.

Summary

  • [Template] makes each row a call to one keyword; Test Template does it for a whole file.
  • With one template test, all rows run and failures are summed up.
  • With one test per row, each case has its own name and result.
  • Use ${EMPTY} and ${SPACE} for empty and space values.
  • Choose templates when steps are identical and only data changes.

Next: External test data: CSV, JSON and YAML