liveBook · Manning

Background block

As explained in Effective Behavior-Driven Development

A Gherkin section whose steps establish context shared by scenarios in a feature file.

A Background is a Gherkin block that sets context shared by scenarios in a feature file. Its steps run before every scenario, so shared setup need not be repeated. section 9.6 section 8.2.1

Listing 9.7 Scenarios sharing context using Background
Background:
  Given a restricted customer

Scenario: Restricted unauthenticated customer not offered
➥ pay-on-collection option
  Given they are unauthenticated
  When they place an order for customer-collection
  Then pay-on-collection should not be offered as a payment option

Scenario: Restricted authenticated customer not offered pay-on-collection
➥ option
  Given they are authenticated
  When they place an order for customer-collection
  Then pay-on-collection should not be offered as a payment option

The restricted-customer context moves into a Background, while each scenario keeps its own authentication condition and outcome. section 9.6

Removing repetition can make a feature file less readable: readers must remember the background while following each scenario. Prefer clear, meaningful scenarios over eliminating every duplicate step. section 9.6

How Effective Behavior-Driven Development covers it

How deep it goes
Defines Background as a block keyword and explains shared-context execution and readability trade-offs.
Examples
Shows restricted-customer scenarios sharing setup while differing in authentication status.
What you can do after
Evaluate whether shared setup improves readability enough to justify hiding repeated context.
How it connects
Connects Gherkin Background with feature files, scenarios, and shared context.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Effective Behavior-Driven Development. The section links point to the parts of the book it draws on.