English 中文(简体)
Feature-scoped step definitions with SpecFlow?
原标题:
  • 时间:2010-05-31 12:50:01
  •  标签:
  • 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?

最佳回答

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.

问题回答

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:

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

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.

Solution for this is to implement Tags & Scoped Binding with the test scenario which is related to Web or related to Controller / Core logic in code.

And drill down the scope for each scenario to any of the below mentioned Before / After execution

BeforeTestRunScenario 
    BeforeFeature
        BeforeScenario
            BeforeScenarioBlock
                BeforeStep
                AfterStep
            AfterScenarioBlock
        AfterScenario
    AfterFeature
AfterTestRunScenario 

Also consider using implementation-agnostic DSL along with implementation-specific step definitions. For example, use

When I search for Barbados

instead of

`When I type Barbados in the search field and push the Search button

By implementing multiple step definition assemblies, the same Scenario can execute through different interfaces. We use this approach to test UI s, API s, etc. using the same Scenario.





相关问题
How to run SpecFlow tests in Visual Studio 2010?

Trying to get SpecFlow running with a fresh VS2010 Professional install. Created a new console application and added references to NUnit and SpecFlow. Created a SpecFlow feature. The .feature with the ...

App.config for SpecFlow not recognized by NUnit GUI runner

How do I get my App.config file to be recognized/used by the NUnit GUI runner? I have tried placing it in the top folder of my project and in the same folder as my feature files. Here are the contents ...

Feature-scoped step definitions with 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, ...

SpecFlow vs Cuke4Nuke vs Cucumber+IR

Having a look at BDD frameworks and I can t help but wonder which one would suit us better. I like Cucumber because, they have been there doing BDD for a good while(since early 2008) and I like Ruby, ...

热门标签