Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 36 ยท Patterns and Extensions

BDD and Gherkin style tests

Write behavior-driven tests in Robot Framework with Given/When/Then keywords, embedded arguments, and the native .robot Gherkin style plus a runnable matcher.

Behavior-driven development (BDD) describes software through examples written in plain language that business people, testers and developers can all read. The classic structure is Given (the starting situation), When (the action), Then (the expected result). Robot Framework supports this style natively: you write test steps starting with those words, and Robot lets the keyword names ignore them.

This lesson shows the style, how embedded arguments put values inside keyword names, what is different from real Gherkin tools like Cucumber, and how to keep BDD tests tidy. The Robot syntax is read-only; the runnable examples are Python stand-ins that match step sentences to functions the way embedded arguments do.

Given, When, Then in a test

TEXT
*** Test Cases ***
Customer Buys A Book
    Given a customer with an empty cart
    When the customer adds "Clean Code" to the cart
    And the customer pays by card
    Then the order is confirmed
    And the cart is empty

Robot treats Given, When, Then, And and But (and * in a few versions) as optional prefixes: it removes them when looking for the keyword. So the keyword you define is simply named a customer with an empty cart, and the same keyword works whether a test writes Given or And in front.

The three parts

Given The starting situation.
When The action under test.
Then The expected outcome.

And and But continue the previous part.

1 / 2

Embedded arguments

Writing a keyword for every book title is silly. Embedded arguments place variables inside the keyword name:

TEXT
*** Keywords ***
the customer adds "${title}" to the cart
    Click Element    xpath://li[.='${title}']//button
    Wait Until Page Contains    ${title} added

the customer pays by ${method}
    Select From List By Label    id:payment    ${method}
    Click Button    id:pay

the cart is empty
    Element Should Contain    id:cart    0 items

When Robot sees the customer adds "Clean Code" to the cart, it matches the pattern and passes Clean Code as ${title}. Everything between the quotes is captured. By default ${method} matches any text; you can restrict it with a regular expression: ${method:card|paypal}.

A few rules help:

  • Keep quotes around values to make boundaries obvious.
  • Do not use the same embedded pattern twice in two keywords that could both match a sentence (Robot raises an error about multiple matches).
  • Use ${number:\d+} when only digits are valid; it also converts the value with type hints in recent versions.

A scenario with data

Gherkin tools use "Scenario Outline" with example tables. Robot gives you the same with templates:

TEXT
*** Test Cases ***
Discount For Order Size
    [Template]    Order of ${quantity} items costs ${total}
    1     10.00
    5     45.00
    10    80.00

Here the template keyword has embedded arguments, and each row fills them. See data-driven templates.

Writing good scenarios

Business-readable scenarios describe behavior, not clicks. Compare:

Weak (steps of the interface) Better (behavior)
Click Element id:u then Input Text id:u demo Given a registered customer "demo"
Click Button id:buy When the customer buys "Clean Code"
Page Should Contain Thank you Then the order is confirmed

The clicking lives inside the keywords, in a lower layer, so a UI change edits one keyword instead of fifty scenarios. That is the layering idea of the next lessons.

Other tips:

  • One scenario tests one behavior; keep it short (three to seven steps).
  • Make Given steps cheap: set up data through an API or database, not by clicking through ten screens.
  • Use only one When per scenario.
  • Do not let the steps contain technical words like "locator", "XPath" or "SQL".

Robot BDD versus Cucumber

Robot's approach is native: the test and its implementation both live in .robot files. Tools like Cucumber keep scenarios in .feature files, and step code is in another language. A plugin library exists to run .feature files in Robot (a Gherkin parser is in Robot itself from version 6.1: it can read .feature files and run steps defined as keywords). Choose Gherkin files if product owners write scenarios and need a separate file format; otherwise the native style is simpler.

A step matcher in Python

This stand-in registers patterns with ${...} placeholders and runs a scenario by matching each sentence, stripping Given/When/Then/And, and passing out the captured values. It follows the same idea as embedded arguments.

Python Stand-in: match sentences to steps

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.

The last line shows the real-world failure you will meet most: no keyword matches, because a sentence was misspelled. Robot reports it the same way.

Reusing steps across scenarios

A second stand-in shows why the same sentence is reusable. Two scenarios share Given and When steps, and only the Then differs.

Python Stand-in: shared steps, different outcomes

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.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens to the words Given, When and Then when Robot looks up a keyword?

    Given, When, Then, And and But are optional prefixes ignored when matching keywords.
  2. 2. What are embedded arguments?

    A keyword named with ${...} placeholders captures matching text from the calling sentence.
  3. 3. Which Given step is best?

    Scenarios should state behavior in business language; the clicks belong inside lower-level keywords.
  4. 4. How do you run one scenario with several data rows?

    A [Template] with embedded arguments fills the keyword from each row, like a scenario outline.

Summary

  • BDD scenarios use Given, When, Then, And and But; Robot ignores these prefixes when looking up keywords.
  • Embedded arguments put values in keyword names; restrict them with regular expressions.
  • Write behavior, not clicks; keep steps cheap and one When per scenario.
  • Templates give you scenario outlines.
  • Gherkin .feature files are possible, but the native style is simpler for most teams.

Next: Page objects and layered architecture