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
