liveBook · Manning

Seeded data pattern

As explained in Effective Behavior-Driven Development

A pattern that starts scenarios with a selected initial dataset to simplify setup or improve execution speed.

Scenarios run against a real database need a dependable starting state. An empty database keeps scenarios isolated, but repeatedly creating the same menu items can slow execution and clutter setup. Seeded data starts each scenario with a small, deliberately chosen dataset. section 18.0 section 18.4

Seeded Data is an optimization of Clean Start, not a shortcut to using an arbitrary test database or production snapshot. Choose only data useful to many scenarios. A smaller baseline is less likely to conflict with a scenario’s own setup. section 18.4

A shared restaurant menu

A reset can seed Margherita, Pepperoni, and Capricciosa menu items for scenarios that need a menu. A promotion scenario, however, states its menu explicitly so readers can see exactly which prices its expected discounts depend on. section 18.4

When introducing a seed:

  • Identify scenarios whose required starting state conflicts with it. Undo the seed for those scenarios, or exclude them from seeding during reset. section 18.4
  • Consider a scenario tag when only some scenarios should skip a particular seed; keep the seed selective rather than adding every commonly used entity. section 18.4
  • Measure before adding more baseline data: extra seeding may not simplify setup enough to justify its cost. section 18.4
Listing 18.3 Seeding menu items during database reset
*** Support/Hooks.cs ***
[Binding]
public class Hooks(IDatabaseDriver databaseDriver, 
    MenuBackdoorDriver menuBackdoorDriver)
{
  ...
  [BeforeScenario(Order = 0)]
  public async Task ResetDatabase()
  {
    await databaseDriver.UpgradeSchemaIfNeeded();
    await databaseDriver.EmptyDatabase();
    SeedMenuData();    #A
  }

  private async Task SeedMenuData()    #B
  {
    menuBackdoorDriver.SetMenuItems(
      [
        new MenuItemData { Name = "Margherita", Price = 7.99m, 
                           Calories = 900, Vegetarian = true },
        new MenuItemData { Name = "Pepperoni", Price = 9.99m, 
                           Calories = 1200 },
        new MenuItemData { Name = "Capricciosa", Price = 8.99m, 
                           Calories = 1100 }
      ]);
  }
  ...
}

The reset hook empties the database and then adds the shared menu, making the selected starting state visible in one place. section 18.4

How Effective Behavior-Driven Development covers it

How deep it goes
Shows reset-hook seeding and tag-based exclusions, explains collision handling, and recommends measuring performance before expanding the baseline.
Examples
Uses a seeded restaurant menu and a promotion scenario with explicit menu prices and expected discounts.
What it assumes
Assumes familiarity with scenarios and database resets; explains the pattern through its relationship to an empty Clean Start.
What you can do after
Operate/configure it by selecting a small baseline, seeding it during reset, and excluding scenarios that conflict with it.
How it connects
Connects Clean Start and database resets with scenario isolation, conditional hooks, and integration testing.

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.