liveBook · Manning

Seed data

As explained in Effective Behavior-Driven Development

Carefully selected data pre-populated in a database before scenarios execute.

Seed data is a carefully chosen starting dataset loaded into a database before scenarios run. It can save scenarios from repeatedly creating common data, but scenarios that need custom data must replace or bypass the seeded records. section 18.4

Before sharing data across scenarios:

  • Check how often scenarios need it, and provide a way for scenarios with different data requirements to avoid conflicts. section 18.4
  • If custom-data scenarios use a common setup step, have that step replace the seeded data. If undoing the seed adds significant runtime, consider excluding it with a tag. section 18.4

A shared restaurant menu

A database reset seeds Margherita, Pepperoni, and Capricciosa. A promotion scenario that specifies its own menu replaces those items before checking which qualify for a discount. 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 }
      ]);
  }
  ...
}

See how a database reset hook seeds menu items before scenario execution. section 18.4

How Effective Behavior-Driven Development covers it

How deep it goes
Explains reset-hook seeding, custom-data conflicts, and tag-based alternatives, but not broader seeding strategies.
Examples
Uses seeded restaurant menu items and a promotion scenario that supplies its own menu.
What you can do after
Apply it by seeding shared menu data during database reset and replacing it for scenarios with custom requirements.
How it connects
Connects database reset hooks with scenario setup steps, data tables, and tags.

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.