SpecFlow - ordering of multiple BeforeScenario methods

Viewed 8755

I'm getting my feet wet with SpecFlow, and I do really enjoy it.

Except for a few thorny issues... like feature and scenario setup code.

In one "general-purpose" file called InfrastructureSteps.cs, I have general setup code that should be run for every scenario - so my method looks something like this:

[BeforeScenario]
public void SetupDbContext()
{
    // define some basic stuff, set up a database transaction context etc.
}

This needs to be run before every scenario, and so far, it has worked just fine.

But now, I have two scenarios in a test-specific steps file that also need quite extensive setup before they can be run. So I marked their scenario in the .feature with a tag:

@needs_extra_setup
Scenario: .....
   Given .....
   When .....
   Then ......

and implemented a test-specific BeforeScenario setup method:

[BeforeScenario("needs_extra_setup")]
public void DoExtraSetupForMyScenario()
{
   // do stuff
}

It works - it gets called - but it gets called before the general-purpose [BeforeScenario] method gets called :-( and thus it fails - stuff being setup in that general-purpose setup method isn't present and causes my code to fail.

So is there any way in SpecFlow to order the [BeforeScenario] methods? Or can I tell a specific [BeforeScenario] method to first execute a "base" [BeforeScenario] method like calling a base method in a overriden method?

Of course I could call that "base" [BeforeScenario] method explicitly - but that seems a bit like a sledge-hammer approach.....

Any ideas? Thoughts? Pointers?

3 Answers
Related