In my post Running code when a feature or scenario starts in SpecFlow I showed we can use hooks to run before the feature and scenario. However, what if we have, for example, a lot of separate scenario hooks but the order that they run in matters. Maybe we need to have the logging of the scenario title and this should run first.
The BeforeScenario has a property Order which we can assign a number like this
[BeforeScenario(Order = 1)] public static void BeforeScenario(ScenarioContext scenarioContext) { Debug.WriteLine($"Scenario starting: {scenarioContext.ScenarioInfo.Title}"); }
This will run before other scenarios, including those with no Order property.
Beware, if you set the [AfterScenario(Order = 1)] it would also be run first. Which you might not want in a logging situation, then (the only solutions I’ve found thuis far is) you’ll have to actually have an Order property in all AfterScenario attributes, i.e. explicitly state the order or all such hooks.