Generating Step Definition Skeletons

You can generate step definition method declarations (aka step definition skeletons) automatically in Visual Studio:

  1. Right-click in your feature file, and select Generate Step Definitions from the context menu.
  2. A dialog is displayed that guides you through the process of creating a new binding class with step definitions for the selected unbound steps. The most common parameter usage patterns (quotes, apostrophes, numbers) are detected so SpecFlow generates the methods and the annotation with the these parameters.

You can also choose to copy the definition to the clipboard, rather than create a new file.

The same skeleton generator engine is used by the SpecFlow runtime (when you execute a scenario with unbound steps) and by the "Go To Definition" command. You cannot select a step definition style here, so a default style is used (see below).

Specifying default style

The default step definition style is taken from your configuration or uses the "Regular expressions in attributes" if not specified. The following example changes the default style to underscored method names (available options: RegexAttribute, MethodNameUnderscores, MethodNamePascalCase, MethodNameRegex)

<specFlow>
  <trace stepDefinitionSkeletonStyle="MethodNameUnderscores" />
</specFlow>

Customizing Step Definition Skeleton Templates

If you want to include a custom using statement or a base class declaration in your generated step definition skeletons, you can customize the templates. To do so, add a text file called SkeletonTemplates.sftemplate to the SpecFlow folder inside your local app data (e.g. C:\Users\me\AppData\Local\SpecFlow\SkeletonTemplates.sftemplate). This overrides the default templates.

Template files contain sections, one for each template. You can override one of more of these sections in your custom template files. For all other sections, the default template is used. The sections are separated with the >>> line prefix followed by the identifier of the section. You can refer to different parameters with the {param} syntax.

The following example overrides the class generation skeleton for C# and specifies a custom comment in the file header:

>>>CSharp/StepDefinitionClass
/****************************
 *    SpecFlow Rocks!
 ****************************/
using System;
using TechTalk.SpecFlow;

namespace {namespace}
{
    [Binding]
    public class {className}
    {
{bindings}
    }
}

For the full list of available sections, parameters and for examples, check the default template.