Blog Post

Starting up with specflow

Tuesday, October 4, 2016 7:01 AM

Starting up with specflow

You will need couple things to install into your visual studio to get get up and running. In order to get your specflow running add plugin into your Visual Studio using tools. This will add additional context menus for your test projects.


Setting up unit test project with specflow

At first lets install nuget package will be installed into your test project.

To do that we need to open Nuget Package Manager and execute

Install-Package SpecRun.SpecFlow

Also we can manage that by using add nuget package option on our test project.


Next we need to do is add feature file

 Feature file has extension .feature

Lets call our feature calculator.feature and insert some default content into it.


Setting up spec file

Now create a file with ending with spec (where specification for the feature will be stored) ie: calculator.spec.cs 

Now lets go into calculator.feature file and Generate the Step Definitions

Generate step definition using specflow image

Using this step you will be able to generate a new file into which all generated items will be inserted or you can paste it into clipboard and insert it yourself.


Let's describe the anatomy of the class


A attribute that bind classes and methods to the step definitions

The binding classes and methods can be defined in the SpecFlow project or in external assemblies



Initial skeleton generated for steps

The bindings (step definitions, hooks, etc.) provide the connection between the free-text specification and the application interfaces (see Bindings). For better reusability, the step definitions can be parametrised. This means that it is not necessary to define a new step definition for each permutation of test.

E.g. the steps

When I perform a simple search on 'Domain'


When I perform a simple search on 'Communication'

can be automated with a single step definition.

The following example shows a simple step definition that matches to the step:

When I perform a simple search on 'Domain' mentioned earlier.


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

Here the method is annotated with the [When] attribute, providing the regular expression to be used to match the step text. The match groups ((…)) of the regular expression define the parameters for the method. The step definitions can be also specified without regular expressions. Check the step definition styles page for details.

Although the step definitions are usually invoked by the SpecFlow runtime, you can also call steps from other step definitions. Check the Calling Steps from Step Definitions page for details.


  • [Given(regex)] or [Given] - TechTalk.SpecFlow.GivenAttribute
  • [When(regex)] or [When] - TechTalk.SpecFlow.WhenAttribute
  • [Then(regex)] or [Then] - TechTalk.SpecFlow.ThenAttribute
  • [StepDefinition(regex)] or [StepDefinition] - TechTalk.SpecFlow.StepDefinitionAttribute, matches for given, when or then attributes (from v1.8)

You can annotate a single method with multiple attributes in order to support different phrasings in the feature file for the same automation logic.

[When(@"I perform a simple search on '(.*)'")]
[When(@"I search for '(.*)'")]
public void WhenIPerformASimpleSearchOn(string searchTerm)


  • Must be in a public class, marked with the [Binding] attribute.
  • Must be a public method.
  • Can be either a static or an instance method. If it is an instance method, the containing class will be instantiated once for every scenario.
  • Cannot have out or ref parameters.
  • Cannot have a return type.


  • The rules depend on the step definition style you use. Check the step definition styles page for the exact rules.


  • Step definitions can specify parameters. These will match to the parameters of the step definition method.
  • The method parameter type can be string or other .NET type. In the later case configurable conversion is applied.
  • With regular expressions
    • The match groups ((…)) of the regular expression define the arguments for the method based on the order (the match result of the first group becomes the first argument, etc.).
    • You can use non-capturing groups (?:regex) in order to use groups without a method argument.
  • With method name matching
    • You can refer to the method parameters with either the parameter name (ALL-CAPS) or the parameter index (zero-based) with the P prefix, e.g. P0.


If the step definition method should match for steps having table or multi-line text arguments, additional Table and/orstring parameters have to be defined in the method signature to be able to receive these arguments. If both table and multi-line text argument are used for the step, the multi-line text argument is provided first.

Given the following books
  |Author        |Title                          |
  |Martin Fowler |Analysis Patterns              |
  |Gojko Adzic   |Bridging the Communication Gap |
[Given(@"the following books")]
public void GivenTheFollowingBooks(Table table)


after finishing this step if we open Test Explorer, we are able to see all tests we have generated according our tests spec.


The official project is located here