Resetting a database before every scenario keeps tests independent, but repeating an expensive reset after a scenario that only read data wastes time. The Skippable Reset pattern skips that work when the preceding scenario made no relevant changes. It can also skip a partial reset, such as clearing and reseeding one table while resetting others. section 18.5 section 18.7
A menu that might have changed
Suppose scenarios share seeded menu records. A first attempt marks the MENU table as changed whenever a menu-setup step runs; the next scenario resets and reseeds MENU only if that flag is set. But menu-administration scenarios can change the same table through the application without running that step. The flag can therefore say “unchanged” when a reset is needed. section 18.5
The key question is not whether a particular test step ran, but whether the relevant data changed. Test automation, application code, stored procedures, triggers, and cascading deletes can all make that answer harder to determine. Change detection must be reliable: skipping a needed reset can cause unexpected failures in later scenarios that are difficult to debug. section 18.5
Choose a detection method that covers the changes your system can make:
- Check an existing last-updated timestamp for records changed since the last reset. section 18.5
- Use table-modification tracking to decide whether to clear and reseed a table, then reset the tracking for the next scenario. section 18.5
- If no dependable method is available, keep resetting rather than trade execution time for unreliable tests. section 18.5
Listing 18.7 Entry point of the trigger-based tracking solution
*** Support/Hooks.cs ***
[Binding]
public class Hooks(IDatabaseDriver databaseDriver, ...)
{
[BeforeScenario(Order = 0)]
public async Task ResetDatabase()
{
await databaseDriver.UpgradeSchemaIfNeeded();
bool wasMenuModified = await databaseDriver
.WasTableModified("MENU"); #A
await databaseDriver.EmptyDatabase( #B
exceptTables: !wasMenuModified ? ["MENU"] : []);
if (wasMenuModified) #C
{
SeedMenuData();
}
await databaseDriver.ResetTableModificationTracking(); #D
}
...
}The trigger-based example checks whether MENU was modified, excludes it from clearing when unchanged, reseeds it when changed, and resets modification tracking. section 18.5
