Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Feature-scoped step definitions with SpecFlow?

Tags:

specflow

I'm using SpecFlow to do some BDD-style testing. Some of my features are UI tests, so they use WatiN. Some aren't UI tests, so they don't.

At the moment, I have a single StepDefinitions.cs file, covering all of my features. I have a BeforeScenario step that initializes WatiN. This means that all of my tests start up Internet Explorer, whether they need it or not.

Is there any way in SpecFlow to have a particular feature file associated with a particular set of step definitions? Or am I approaching this from the wrong angle?

like image 307
Roger Lipscombe Avatar asked May 31 '10 12:05

Roger Lipscombe


People also ask

How do you link a feature file with step definitions in SpecFlow?

Right-click on any step of the Feature File, then click on Generate Step Definitions option. In the Generate Step Definition Skeleton pop-up, check the steps for which we want to generate the implementation. Add a Class Name, then click on the Generate button.

How do you generate step definitions in SpecFlow?

Right-click in the editor and select Generate Step Definitions from the menu. A dialog is displayed with a list of the steps in your feature file. Use the check boxes to determine which steps to generate skeleton code for.

How do you generate step definitions from a feature file?

Place the caret at a step in your . feature file and press Alt+Enter . The list of suggested intention actions opens. Select Create step definition to create a definition only for one step, or select Create all step definitions to add definitions for all steps in a scenario.

What is scope binding in SpecFlow?

Scoping is one of the most important feature of specflow and it allows you to add a lot of flexibility to your test organization and execution by using tags for the tests, and restrict bindings to be applied to a Feature or Scenario or Feature/Scenario having some tags.


3 Answers

There is a simple solution to your problem if you use tags.

First tag you feature file to indicate that a particular feature needs WatiN like that:

Feature: Save Proportion Of Sample Pool Required
  As an <User> 
  I want to <Configure size of the Sample required> 
  so that <I can advise the deployment team of resourcing requirments>.

  @WatiN
  Scenario: Save valid sample size mid range
  Given the user enters 10 as sample size
  When the user selects save
  Then the value is stored

And then decorate the BeforeScenario binding with an attribute that indicates the tag:

[BeforeScenario("WatiN")]
public void BeforeScenario()
{
  ...
}

This BeforeScenario method will then only be called for the features that use WatiN.

like image 145
mfloryan Avatar answered Oct 02 '22 20:10

mfloryan


Currently (in SpecFlow 1.3) step-definitions are global and cannot be scoped to particular features.

This is by design to have the same behavior as Cucumber.

I asked the same question on the cucumber group:

http://groups.google.com/group/cukes/browse_thread/thread/20cd7e1db0a4bdaf/fd668f7346984df9#fd668f7346984df9

The baseline is, that the language defined by all the feature files should also be global (one global behavior of the whole application). Therefore scoping definitions to features should be avoided. Personally I am not yet fully convinced about this ...

However your problem with starting WatiN only for scenarios that need UI-Integration can be solved in two different ways:

  • Tags and tagged hooks: You can tag your scenarios (i.e with @web) and define ina BeforeScenario-Hook that only should run for scenarios with a certain tag (i.e. [BeforeScenario("web")]). See the Selenium integration in our BookShop example: http://github.com/techtalk/SpecFlow-Examples/blob/master/ASP.NET-MVC/BookShop/BookShop.AcceptanceTests.Selenium/Support/SeleniumSupport.cs

  • We often completely separate scenarios that are bound to the UI and scenarios that are bound to a programmatic API (i.e controller, view-model ...) into different projects. We tried to illustrate this in our BookShop example: http://github.com/techtalk/SpecFlow-Examples/tree/master/ASP.NET-MVC/BookShop/ .

like image 32
jbandi Avatar answered Oct 02 '22 19:10

jbandi


Check this out (new feature in SpecFlow 1.4): https://github.com/techtalk/SpecFlow/wiki/Scoped-Bindings

like image 28
Captain JiNX Avatar answered Oct 02 '22 19:10

Captain JiNX