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
