My SpecFlow steps are not restricted by namespace

If your using SpecFlow you have probably realised that SpecFlow steps definitions are not restricted by namespace. This means it can be challenging to write steps that are generic but won’t conflict with other steps. For a while i thought this was just a limitation to SpecFlow. It turns out i didn’t know about the Scoped Step Binding feature of SpecFlow. This allows you to restrict a step definition by Feature, Scenario or Tag and seems to be a little known feature.  As the SpecFlow wiki guide suggests this can also be used to ignore something with an @manual tag by supplying it with empty methods.

Here’s an example from the SpecFlow wiki

[When(@"I perform a simple search on '(.*)'", StepScope(Tag = "controller"))]
public void WhenIPerformASimpleSearchOn(string searchTerm)
{
    var controller = new CatalogController();
    actionResult = controller.Search(searchTerm);
}

[When(@"I perform a simple search on '(.*)'"), StepScope(Tag = "web")]
public void PerformSimpleSearch(string title)
{
    selenium.GoToThePage("Home");
    selenium.Type("searchTerm", title);
    selenium.Click("searchButton");
}
Advertisements
  1. #1 by Donald Heppner on January 8, 2012 - 6:52 pm

    Thanks, this is exactly what I was looking for! As of SpecFlow 1.8 scope is its own attribute:

    [Given(“I have entered (\d+) into the calculator”), Scope(Tag = “calculator”)]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: