Q7 of 21 · BDD / Cucumber

What are data tables in Gherkin and how are they different from Scenario Outline Examples?

BDD / CucumberJuniorbddgherkindata-tablesdatatable

Short answer

Short answer: A data table is an inline table passed as an argument to a single step — it's part of that step's data, not a template for repeating the whole scenario. Scenario Outline Examples repeat the whole scenario; data tables pass structured data into one step.

Detail

Data table in a step:

Scenario: Bulk product import
  Given the following products exist in the catalogue:
    | name    | price | stock |
    | Widget  | 9.99  | 100   |
    | Gadget  | 19.99 | 50    |
    | Doohick | 4.99  | 0     |
  When  the user searches for "Widget"
  Then  one result is displayed

In the step definition (Java):

@Given("the following products exist in the catalogue:")
public void productsExist(DataTable dataTable) {
    List<Map<String, String>> rows = dataTable.asMaps();
    for (Map<String, String> row : rows) {
        db.insertProduct(row.get("name"), row.get("price"), row.get("stock"));
    }
}

In JavaScript:

Given('the following products exist in the catalogue:', function(dataTable) {
  const rows = dataTable.hashes(); // [{name:'Widget', price:'9.99', stock:'100'}, ...]
  for (const row of rows) {
    db.insertProduct(row.name, row.price, row.stock);
  }
});

Key difference from Scenario Outline:

  • Data table — one scenario, one step gets a list of rows.
  • Scenario Outline — one scenario template, many whole-scenario repetitions.

Use data tables when a step naturally takes a collection of values (setup multiple records, verify a list of results). Use Scenario Outline when you want to test the same behaviour with different inputs.

// EXAMPLE

Scenario: Order confirmation shows correct items
  Given an order is placed with the following items:
    | product  | qty | unit_price |
    | Widget   | 2   | 9.99       |
    | Gadget   | 1   | 19.99      |
  Then the order total is £39.97

// WHAT INTERVIEWERS LOOK FOR

Clear distinction from Scenario Outline. How to consume a data table in step def code (asMaps/hashes). A concrete use case (seeding multiple rows, asserting a list).