Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpecFlow - ordering of multiple BeforeScenario methods

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?

like image 655
marc_s Avatar asked Jul 26 '12 08:07

marc_s


1 Answers

I'm pretty sure that you can (and probably shouldn't) order the execution order of your scenarios.

But you could use some other hooks such as BeforeFeature and maybe get around it that way.

Another way is to simply have a flag that check if the general stuff has been set in the specific stuff, calling into the SetupDbContext method (or preferably the thing that SetupDbContext calls in turn).

Use the ScenarioContext.Current dictionary to store your flags.

I hope you found this helpful

like image 67
Marcus Hammarberg Avatar answered Sep 22 '22 01:09

Marcus Hammarberg