6

This may have already been brought up, a stupid question perhaps.

Anyway, I have been looking into SpecFlow and wondering how I can delete my seed data for the feature. This seed data are shared across different scenarios. Is there an elegant way of doing this? Perhaps the ability to inject an event when testRunner.OnFeatureEnd() in invoked?

Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
Leo
  • 63
  • 1
  • 4
  • I had to mention that if you're loading data into the database then what you're doing is integration testing, not unit testing. A unit test would mock the database so that it's only testing a single class (unit) at a time. – Martin Owen Dec 11 '10 at 09:32
  • Yes, this is not unit tests but what I am intending to use SpecFlow is really for acceptance testing that would require actual background setup to some data store. – Leo Mar 08 '11 at 14:17
  • Transaction Scope is your friend. – Chad Dec 13 '11 at 18:53

1 Answers1

8

You can use hooks.

Hooks are methods that are executed at certain times during execution of Gherkin features.

There are several hooks for different events during the execution.

In SpecFlow you define hooks in your [Binding] classes with an special attribute. The following hooks are available:

[BeforeTestRun], [AfterTestRun], [BeforeFeature], [AfterFeature] [BeforeScenario], [AfterScenario], [BeforeScenarioBlock], [AfterScenarioBlock], [BeforeStep], [AfterStep]

Examples:

https://github.com/techtalk/SpecFlow-Examples/blob/master/ASP.NET-MVC/BookShop/BookShop.AcceptanceTests.Selenium/StepDefinitions/BookSteps.cs

https://github.com/techtalk/SpecFlow/blob/master/Tests/FeatureTests/BeforeAfterHooks/BeforeAfterHooksSteps.cs

jbandi
  • 17,499
  • 9
  • 69
  • 81
  • Note that it wasn't until version 1.4.0 of SpecFlow that you could scope the step definitions: https://github.com/techtalk/SpecFlow/blob/master/Tests/FeatureTests/ScopedSteps/ScopedStepsBindings.cs Up until that point they were global, and all Before/After steps would run before every Scenario/Feature regardless of the Binding class in which they were defined. – Martin Owen Dec 11 '10 at 09:14