What does a Background section in a feature file do to the scenarios below it?
answer
- Shared context, stated once
- Re-runs, not run once
- Context steps only, never outcomes
- Reader of scenario eighteen scrolls up
- One added line touches every scenario
basics
~10 sA Background holds context steps that run again before every scenario in that file, so shared setup is written once. Every scenario inherits those steps, whether or not it needs them.
solid answer
~50 sA **Background** lifts context steps that are true for every scenario in a feature file out of the scenarios and states them once, above them. It is not a one-time fixture: the steps re-run before **each** scenario, and before each expanded row of a Scenario Outline, so scenarios stay independent and order-free — and so the setup cost is paid once per scenario, not once per file. It should carry context only; the action under test and the assertion belong in the scenario that names them. The cost is hidden coupling: a reader of the eighteenth scenario has to scroll to the top to know what state it starts in, and adding one line up there silently changes every scenario in the file. Keep it to a couple of steps that are genuinely universal, and push anything only some scenarios need back down into those scenarios.
code
pseudocode · 13 linesFeature: Rate distribution to booking channels
Background:
Given the property has 62 bookable rooms
Scenario: A negative nightly rate is rejected
When a nightly rate of -18.00 is submitted
Then the submission is rejected as invalid
Scenario: An acknowledged rate plan is pushed to a channel
Given the wholesale channel has acknowledged the current rate plan
When the rate plan is pushed to the wholesale channel
Then the channel reports the plan as currentgo deeper
Recall two facts and you pass this one: a Background holds shared context steps, and those steps run again before every scenario rather than once for the file. Be able to say what belongs there and what does not.
Explain the mechanics: re-execution per scenario keeps scenarios order-independent but multiplies setup cost, and expanded Examples rows each pay it too. Be ready to say why an action or an assertion in a Background is a design error rather than a syntax one.
Show the production judgment. Talk about blast radius when a shared setup step depends on something intermittently unavailable, and about the readability cost of a scenario whose starting state lives fifty lines above it. Have a rule for what earns a place in a Background.
Own the trade. Duplication in specification text versus hidden coupling is a house-style decision you set, and it is at odds with the instinct to remove repetition. Be able to defend deliberately repeating context lines, and to say where technical setup should live instead of the prose.
### What a Background block is A **Background** is a block of context steps placed once, near the top of a feature file, above the scenarios. Its steps are written in exactly the same language as a scenario's own context steps, and they describe state that is true before every scenario in that file begins. The intent is deduplication of *narrative*, not of *code*: if eleven scenarios all open with the same two lines of context, those two lines stop carrying information and start being noise, and the Background lifts them out so each scenario's own text is only what makes that scenario different. ### The execution semantics people get wrong The single most common misunderstanding is that a Background runs **once per file**. It does not. It runs **again before every scenario in the file**, from a clean starting point, so that scenarios stay independent and order-free. If a Scenario Outline sits in the file, the Background runs again for every row of its Examples table too, because each row is expanded into its own scenario. That has two consequences worth stating out loud in an interview: * **Isolation is preserved.** No scenario inherits the residue of the one before it; each gets the same freshly established context. * **The cost is multiplied.** A Background step that takes four seconds costs four seconds per scenario, not four seconds per file. In a 340-case regression pack, a single careless setup step in a widely used Background is the difference between a pack that finishes over coffee and one that does not finish at all. A Background is also restricted in what it should contain: **context only**. Putting the action under test there means every scenario in the file exercises an event that none of them names, and the scenarios stop being readable specifications. Putting an assertion there means a failure is reported against a scenario that never claimed to check that thing. Most parsers will happily accept those keywords in a Background; the constraint is a design rule, not a syntax rule, which is exactly why it is worth asking about. ### The coupling it hides Consider a feature file for a **hotel booking channel manager** — the component that pushes room rates and availability out to distribution channels and pulls reservations back. Someone writes: ``` Feature: Rate and availability distribution Background: Given the property has 62 bookable rooms And the property is connected to 4 distribution channels And each channel has acknowledged the current rate plan ``` Every scenario below now begins in a world with four connected channels and an acknowledged rate plan. A scenario about *rejecting a negative nightly rate* does not care about any of that, but it pays for it and, worse, depends on it. Three things follow. **Action at a distance.** A reader who lands on the eighteenth scenario cannot judge whether its outcome is correct without scrolling back to the top of the file. The scenario is no longer a self-contained example; it is a fragment that only means something in context. That is the readability payoff of the whole format being spent, not earned. **A shared blast radius.** The acknowledgement step calls out to four channel connectors. When one of them develops **an intermittent timeout**, the failure does not land on the scenarios about channel acknowledgement — it lands on *every scenario in the file*, including the pure rate-validation ones. A triage engineer reading the report sees eighteen unrelated red scenarios and no signal about the real fault. **Edit pressure.** When a new scenario needs a fifth channel, the tempting fix is a fourth Background step. It is one line, and it silently changes the starting state of every existing scenario in the file. Backgrounds decay in exactly this direction: each addition is locally cheap and globally expensive. ### How to keep one healthy * Keep it to a couple of steps that are genuinely true for **every** scenario in the file, and that a reader would take for granted rather than need to know. * If a step is needed by some scenarios but not others, it belongs **in those scenarios**, however much duplication that creates. Duplication in specification text is much cheaper than a hidden precondition. * If the Background is long because the file covers several capabilities, the file is the problem — split it, or group the scenarios under separate rule blocks where the dialect supports rule-scoped context. * Slow, purely technical setup that no business reader needs (seeding a datastore, starting a stub of an external service) is better handled in the automation layer's setup hooks than written as prose nobody reads. * Do not attempt to *store* state in the Background for later scenarios. It re-runs, and anything it created is expected to be re-created; a suite that depends on leftovers from a previous scenario is order-dependent by construction. A useful review heuristic: read a scenario in the middle of the file on its own. If you cannot tell whether the Then line is right, the Background is carrying information the scenario needed to state itself.
- Does a Background run once for the whole feature file, or once for each scenario?Once for each scenario, from a clean start — and that includes every row of a Scenario Outline, since each row becomes its own scenario. That is what keeps scenarios independent of run order, and it is also why a slow step there is expensive: across a 340-case pack, four seconds of shared setup is roughly twenty-two minutes of wall clock spent re-establishing the same state.
- When would you delete a Background and repeat those Given steps inside the scenarios instead?When only some scenarios need the steps, or when the precondition is one a reader must see to judge the outcome. Repetition in specification text is cheap; a hidden starting state is not. I would also split the file if the Background is long because it is covering more than one capability.
- Is it valid to put a When or a Then step in a Background?Parsers generally accept the keywords, so it is not a syntax error, but it is a design error. An action there means every scenario exercises an event none of them names, and an assertion there reports a failure against a scenario that never claimed to check it. Background is for context that a reader takes for granted.
It is the opening paragraph of a chapter: useful when every section that follows really does assume it, and a trap when section eight quietly depends on a sentence the reader skimmed twelve pages ago.
saying these in an interview costs you the question
- Says a Background runs once per feature file
- Puts the action under test in the Background
- Adds a Background step for one scenario's benefit
- Expects state created in a Background to persist between scenarios
- Treats Background as a dumping ground for every precondition