0
votes

I am using SpecFlow with C# for running some BDD test scenarios. I have defined a scenario outline and I need a method to run after each scenario outline example. The attribute [AfterScenario] is present on that method. However, I can see that the [AfterScenario] method is execute twice after each scenario outline example.

Don't know if it makes any difference, but at one point I had the same [AfterScenario] method in another class by mistake and both of them were run. However, I have deleted one of them and rebuilt the project. Maybe something was left somewhere?

I have seen some examples where people complain about AfterScenario and BeforeScenario hooks running twice, but that's when tags are present. I am not using any tags.

2
When you are using scenario outline, it takes each row of your examples as a scenario of it's own, therefore if you have 3 rows it will be executed 3 times. - Shlomi Bazel

2 Answers

0
votes
  1. Specify scenario tags inside [AfterScenario] attribute. -> [AfterScenario("scenarioTagName")]. If you don't use tags, methods with [AfterScenario] attribute will execute after each scenario. There are of course situations where you would use [AfterScenario] attributes without tags, but as i can understand you don't want that every method with [AfterScenario] attribute comes to an execution after every scenario execution.
  2. About execution of 2nd method with [AfterScenario] attribute... It does not matter in which class methods with [AfterScenario] attribute are defined as long as classes have [Binding] attribute. With [Binding] attribute assigned to classes, specFlow is enabled to perform mechanism under the hood.

If you may want to add tag to your Scenario Outline, try this in your .feature file:

@scenarioTagName
Scenario Outline:...
0
votes

Maybe you are calling your method, which present in the After Scenario in some other method. I think you hadn't cleared all places, where it can be called.