问题
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?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/29850943/what-governs-when-beforescenario-is-run-in-specflow