liveBook · Manning

Restore before pattern

As explained in Effective Behavior-Driven Development

A pattern that resets state to a known baseline before each scenario, regardless of how the previous scenario ended.

A scenario passes on its own but fails in the nightly integration run. The previous scenario may have left records in the database. If it timed out or was stopped, its cleanup never ran. Restore Before avoids depending on that cleanup: reset the service to a known baseline before each scenario begins. section 18.2 section 18.7

Consider a team replacing payment-gateway and database stubs with real services in a nightly build. The integration scenarios give them a dress rehearsal, but only if one scenario’s changes cannot alter the next scenario’s result. Resetting before execution makes each scenario start from a predictable state, regardless of how its predecessor ended. section 18.0 section 18.7

Why reset before rather than clean up afterward?

  • An after-scenario hook may not run when a pipeline kills a test process because of a timeout or resource limit. A later run can then encounter leftover data and report a misleading failure. section 18.2
  • Leaving the database untouched immediately after a failure preserves clues for debugging. The next scenario resets it before proceeding. section 18.2
  • A reset has a cost. The choice of an empty, seeded or snapshot baseline affects setup work and feedback speed; parallel execution also complicates isolation. section 18.7
Listing 18.1 Hook that resets the database before the scenario execution
*** Support/Hooks.cs ***
[Binding]
public class Hooks(IDatabaseDriver databaseDriver)
{
  ...
  [BeforeScenario(Order = 0)]    #A
  public async Task ResetDatabase()
  {
    await databaseDriver.RecreateDatabase();    #B
  }
}

A before-scenario hook calls a database driver to recreate the database before the scenario runs. The reset belongs at the start, not in cleanup that depends on the previous scenario finishing. section 18.2

The practical rule is to prepare the next scenario’s starting state rather than rely on the last scenario to tidy up. This supports repeatable integration results while leaving failed-state evidence available until the next reset. section 18.2 section 18.7

How Effective Behavior-Driven Development covers it

How deep it goes
Explains why before-scenario resets survive missed cleanup and shows a database hook; leaves baseline strategy dependent on the application.
Examples
Uses a nightly run against real services, a terminated test process, and a database-reset hook.
What it assumes
Assumes automated scenarios, integration tests, and access to a service’s state; introduces the before-scenario hook as the reset mechanism.
What you can do after
Operate/configure it with a before-scenario database reset that establishes a baseline without requiring the previous scenario’s cleanup to run.
How it connects
Connects scenario isolation with continuous integration, after-scenario hooks, baseline data, and failure diagnosis.

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.