Alex Rivera | Logout

SpecFlow - ordering of multiple BeforeScenario methods

Asked 2012-07-26T08:12:24.313
11

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?

Edit
Report

1 Answer

4

I had the same problem. I ended up using

if(ScenarioContext.Current.ScenarioInfo.Tags.Contains("needs_extra_setup"))

at the end of the [BeforeScenario] method. It is described on the page already linked to by Marcus: SpecFlow Hooks

answered 2012-12-06T10:22:35.147

Your Answer