skip to content

What does Gherkin's Rule keyword group, and how does a Background under a Rule differ from one at Feature level?

level: middleimportance: should knowfreq 42%

answer

  1. A grouping level between Feature and Scenario
  2. One business rule, several worked examples
  3. Two Background scopes, not one
  4. Feature-level setup runs before rule-level setup

basics

~20 s

Rule groups the scenarios that illustrate one business rule, sitting between Feature and its scenarios. A Rule may carry its own Background, which applies only to that Rule's scenarios and runs after the feature-level Background, never instead of it.

solid answer

~40 s

`Rule` names a business rule and gathers the `Example` (or `Scenario`) blocks that illustrate it, so one feature file can hold several rules without splitting up or encoding the grouping in scenario titles. It may carry its own description, its own tags, and crucially its own `Background`. The two `Background` scopes compose rather than compete: a feature-level `Background` applies to every scenario in the file **including** those inside rules, and its steps run first; a rule-level `Background` applies only to scenarios inside that rule and runs after. A rule-level `Background` is never shared with a sibling rule, so setup two rules need is either duplicated or promoted to feature level. Beyond that grouping and scoping, `Rule` changes nothing about execution - no isolation, no shared state, no reordering.

code

gherkin · 20 lines
gherkin
Feature: School meal ordering

  Background:
    Given the kitchen roster is loaded for 318 pupils

  Rule: Orders close at the daily cut-off

    Background:
      Given the cut-off is set to 09:47

    Example: An order two minutes before the cut-off
      When a parent orders at 09:45
      Then the order is accepted

  Rule: Free-meal pupils are never charged

    Example: An eligible pupil orders a meal
      Given pupil 4417 is eligible for free meals
      When the pupil orders a meal
      Then the charge is 0.00

go deeper

for a junior

Recognise Rule when you see it and know it groups the examples of one business rule between Feature and its scenarios. Knowing that Example and Scenario are the same keyword under a rule is enough at this level.

for a middle

Explain the two Background scopes precisely: which scenarios each covers, that the feature-level steps run first, and that a rule-level Background is not shared with sibling rules. Be able to say what Rule does not change about execution.

for a senior

Show the design judgement: when introducing rules shrinks an overgrown shared Background, when a rule with twenty examples is really several rules, and how you check that a reporting pipeline actually renders rules before a team commits to them.

for a principal

Own the convention across teams. Decide whether files are organised by rule or split per rule, how much duplication between rule-level Backgrounds is acceptable before the file splits, and how that structure serves the domain experts reading the output.

## What `Rule` groups `Rule` is a Gherkin keyword that sits **between `Feature` and the scenarios**. It names one business rule and gathers the concrete examples that illustrate it, so a feature file can hold several rules without splitting into several files or falling back on naming conventions in scenario titles. The nesting is strict: ``` Feature: <- one per file Background: <- optional, applies to everything below Rule: <- zero or more Background: <- optional, applies only inside this Rule Example: <- or Scenario:, same keyword Scenario Outline: Scenario: <- scenarios may also sit directly under Feature ``` Under a `Rule` you write `Example:` or `Scenario:` — they are synonyms, and `Example` is the reading that pairs naturally with a rule ("this rule, these examples"). A `Rule` may carry its own free-form description lines, its own tags, and its own `Background`. It may not nest inside another `Rule`. ## Two `Background` scopes, not one This is the part interviewers actually probe, because a file can now have two `Background` blocks and candidates guess at how they compose. | | `Background` under `Feature` | `Background` under `Rule` | |---|---|---| | Applies to | every scenario in the file, **including** those inside every `Rule` | only the scenarios inside that one `Rule` | | How many | at most one per file | at most one per `Rule` | | Position | before the first scenario **and** before the first `Rule` | before the first scenario in that `Rule` | | Order for a scenario inside a `Rule` | runs **first** | runs **after** the feature-level one | | Shared with sibling rules | yes | no — each `Rule` needs its own copy | So for a scenario inside a `Rule` that has its own `Background`, the executed sequence is: feature-level `Background` steps, then rule-level `Background` steps, then the scenario's own steps. For a scenario written directly under `Feature`, only the feature-level `Background` runs. There is no inheritance in the other direction: a rule-level `Background` never leaks out to sibling rules, and it never *replaces* the feature-level one. ## What `Rule` does not change `Rule` is a grouping and a scoping construct for `Background` — nothing more. It does **not**: - run its scenarios in isolation, in a separate process, or in a shared state scope; - make its scenarios share one instance of anything; - reorder or repeat scenarios; - change how steps bind to step definitions. Two things it *does* affect are worth knowing. Tags placed on a `Rule` line are inherited by the scenarios beneath it, so a tag expression that selects a tag written on the `Rule` selects all of them. And reporting output carries the rule as structure, so generated documentation can group the examples under the rule's name rather than presenting a flat list of scenarios. ## Using it on a suite two teams edit Take a school-meal ordering service whose feature file covers ordering. Two genuinely different rules live there: *orders close at the daily cut-off*, and *free-meal pupils are never charged*. Written flat, the file has one `Background` that has to satisfy both, and it drifts: the cut-off clock is set up for scenarios that do not care about the clock, and the eligibility roster is loaded for scenarios that never look at it. Every scenario then pays for setup it does not use, and each team's additions push the shared `Background` further toward being a union of everything. With `Rule`, the file-level `Background` shrinks to what is genuinely universal — the kitchen roster is loaded — and each rule carries the setup only its own examples need. The two teams stop editing the same block. When a scenario is added under the wrong rule the mismatch is visible in review, because the rule states the behaviour the example is supposed to illustrate. Two cautions. First, resist making `Rule` a folder: if a rule accumulates twenty examples, that is a sign the rule is really several rules, or that the examples are variations that belong in an outline. Second, a rule-level `Background` is not shared, so setup needed by two rules is either duplicated in both or promoted to the feature level — and promoting it means every scenario in the file pays for it. That tension is the actual design decision `Rule` puts in front of you.

  • Can a Rule line carry tags, and what do they apply to?
    Yes. A tag written above a `Rule` is inherited by the scenarios inside it, so a tag expression selecting that tag selects every example under the rule. That makes `Rule` a convenient unit for slicing a suite, but remember the inheritance is one-way: a tag on one scenario says nothing about its siblings.
  • Two rules in the same file need the same setup step. Where do you put it?
    There is no sharing between rule-level Backgrounds, so you either repeat the step in both, or promote it to the feature-level Background. Promoting makes every scenario in the file pay for it, including scenarios that do not need it. Repetition is often the honest choice; if it keeps growing, the rules probably belong in separate files.
  • Does putting scenarios under a Rule change how they execute?
    No. `Rule` groups scenarios and scopes a `Background`; it does not isolate them, share state between them, reorder them, repeat them, or change how their steps bind to step definitions. Each example still runs as an independent scenario. What it does change is structure in reports and the tags its scenarios inherit.

saying these in an interview costs you the question

  • Thinks a rule-level Background replaces the feature-level one
  • Believes Rule isolates or reruns the scenarios beneath it
  • Writes a Background after the first scenario in a Rule
  • Thinks Rule is decoration with no meaning to the parser
  • Expects one Rule's Background to apply to sibling rules