2

I've recently started on an existing project that uses SpecFlow.

I've added a method with [BeforeScenario] in BaseSteps.cs that does some logging. BaseSteps.cs doesn't have a [Binding] attribute on its class, but the derived classes do have [Binding].

However, an example.feature can use steps from differentDerivedSteps.cs classes. In these case the [BeforeScenario] is being called multiple times in a single scenario from that feature.

Why is this happening? What is calling the BeforeScenario multiple times for a single scenario?

StuperUser
  • 10,555
  • 13
  • 78
  • 137

2 Answers2

3

Some code might help identify the issues, but it might be that the derived steps classes all have the method [BeforeScenario] (as they inherit it) and so specflow is calling once for each derived class.

In Specflow its usually not necessary to get involved with any inheritance as all steps are global and accessible from anywhere, so just move your [BeforeScenario]into its own class, whack a [Binding] attribute on it and Specflow will find it an use it.

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • `so specflow is calling once for each derived class` Do you know when? I've moved the attributes to the derived classes that call the method on the base class, so they're getting called once. However, I'm not sure whether it's been setup incorrectly by a predecessor or whether Specflow gathers all of the BeforeScenarios from the steps it will be running and execute them all at once. – StuperUser Apr 24 '15 at 15:34
  • 1
    I believe it works like this: Specflow finds all BeforeScenarios in all assemblies it knows about (in the current project and any externally defined assemblies) then for the current scenario it finds all BeforeScenario methods that match the current scenario tags and runs them all, in no particular order. Like I said its better to put your before scenario in a completely separate class, specflow will still find and use it. – Sam Holder Apr 24 '15 at 15:37
1

Avoid using inheritance in your Steps classes - I've found it sometimes causes weird "multiple matching bindings found"

First answer here explains why inheritance causes confusion with Specflow steps: https://stackoverflow.com/a/15508054/2213092

Without code it's difficult to determine why it's calling that specific BeforeScenario multiple times though. If you're still troubleshooting this, you can put a breakpoint on the BeforeScenario method, and look down the call stack to see where it's being fired from.

Community
  • 1
  • 1
lvanzyl
  • 143
  • 1
  • 8