tags:

views:

430

answers:

4

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?

A: 

I originally assumed that a step file was associated with a particular feature file. Once I realized this was not true it helped me to improve all my SpecFlow code and feature files. The language of my feature files is now less context depended, which has resulted in more reusable step definitions and less code duplication. Now I organize my step files according to general similarities and not according to which feature they are for. As far as I know there is no way to associate a step with a particular feature, but I am not a SpecFlow expert so don't take my word for it.

If you still would like to associate your step files with a particular feature file, just give them similar names. There is no need for it to be forced to only work for that feature even if the step code only makes sense for that feature. This is because even if you happen to create a duplicate step for a different feature, it will detect this as an ambiguous match. The behavior for ambiguous matches can be specified in an App.config file. See http://cloud.github.com/downloads/techtalk/SpecFlow/SpecFlow%20Guide.pdf for more details the about App.config file. By default ambiguous matches are detected and reported as an error.

[edit]: Actually there is a problem with working this way (having step files associated with feature files in your mind only). The problem comes when you add or modify a .feature file and use the same wording you have used before, and you forget to add a step for it, but you don't notice this because you already created a step for that wording once before, and it was written in a context sensitive manner. Also I am no longer convinced of the usefulness of not associating step files with feature files. I don't think most clients would be very good at writing the specification in a context independent manner. That is not how we normally write or talk or think.

INTPnerd
+3  A: 

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:

jbandi
+2  A: 

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(new[] {"WatiN"})]
public void BeforeScenario()
{
  ...
}

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

mfloryan
A: 

Check this out (new feature in SpecFlow 1.4): http://github.com/techtalk/SpecFlow/blob/master/Tests/FeatureTests/ScopedSteps/ScopedSteps.feature

Captain JiNX

related questions