Generate Tests From MsBuild


We strongly recommend using MSBuild to generate your code-behind files. To do so, you simply need to add the SpecFlow.Tools.MsBuild.Generation nuget package to your project.

Note: You will need at least VS2017/MSBuild 15 to use this package.

Classic Project System

  1. Add the NuGet package `SpecFlow.Tools.MsBuild.Generation` with the same version as SpecFlow to your project.
  2. Add the following MSBuild snippet to the end of your project file, but still within the <Project> tag:
    <Target Name="AfterUpdateFeatureFilesInProject">
        <!-- include any generated SpecFlow files in the compilation of the project if not included yet -->
            <Compile Include="**\*.feature.cs" Exclude="@(Compile)" />
  3. Remove all `SpecFlowSingleFileGenerator` custom tool entries from your feature files.

SDK Style project system

Please the SpecFlow 2.4.1 NuGet package or higher, as this version fixes an issue with previous versions (see Known Issues below)

  1. Add the NuGet package SpecFlow.Tools.MsBuild.Generation with the same version as SpecFlow to your project.
  2. Remove all SpecFlowSingleFileGenerator custom tool entries from your feature files.

Additional Options (Prior to SpecFlow 3)

The TechTalk.SpecFlow.targets file defines a number of default options in the following section:

    <ShowTrace Condition="'$(ShowTrace)'==''">false</ShowTrace>
    <OverwriteReadOnlyFiles Condition="'$(OverwriteReadOnlyFiles)'==''">false</OverwriteReadOnlyFiles>
    <ForceGeneration Condition="'$(ForceGeneration)'==''">false</ForceGeneration>
    <VerboseOutput Condition="'$(VerboseOutput)'==''">false</VerboseOutput>
  • ShowTrace: Set this to true to output trace information.
  • OverwriteReadOnlyFiles: Set this to true to overwrite any read-only files in the target directory. This can be useful if your feature files are read-only and part of your repository.
  • ForceGeneration: Set this to true to forces the code-behind files to be regenerated, even if the content of the feature has not changed.
  • VerboseOutput: Set to true to enable verbose output for troubleshooting.

To change these options, add the corresponding element to your project file before the <Import> element you added earlier.


<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\SpecFlow.2.2.0\tools\TechTalk.SpecFlow.tasks"  Condition="Exists('..\packages\SpecFlow.2.2.0\tools\TechTalk.SpecFlow.tasks')" />
<Import Project="..\packages\SpecFlow.2.2.0\tools\TechTalk.SpecFlow.targets" Condition="Exists('..\packages\SpecFlow.2.2.0\tools\TechTalk.SpecFlow.targets')" />

You can find an example here (project Bowling.SpecFlow)

Known Issues

SpecFlow prior to 2.4.1

When using SpecFlow NuGet packages prior to SpecFlow 2.4.1, Visual Studio sometimes does not recognize that a feature file has changed. To generate the code-behind file, you therefore need to rebuild your project. We recommend upgrading your SpecFlow NuGet package to 2.4.1 or higher, where this is no longer an issue.

Code-behind files not generating at compile time

When using the classic project system, the previous MSBuild target may no longer located at the end of your project. NuGet ignores entries added manually, and places the MSBuild imports at the end. However, the AfterUpdateFeatureFilesInProject target needs to be defined after the imports. Otherwise it will be overwritten with an empty definition. If this happens, your code-behind files are not compiled as part of the assembly.

Linked files are not included

If you link feature files into a project, no code-behind file is generated for them (see GitHub Issue 1295).