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
- Add the NuGet package `SpecFlow.Tools.MsBuild.Generation` with the same version as SpecFlow to your project.
- Add the following MSBuild snippet to the end of your project file, but still within the
- Remove all `SpecFlowSingleFileGenerator` custom tool entries from your feature files.
SDK Style project system
Please use at least SpecFlow 2.4.1, as this version fixes an issue with previous versions (see Known Issues below)
- Add the NuGet package
SpecFlow.Tools.MsBuild.Generationwith the same version as SpecFlow to your project.
- Remove all
SpecFlowSingleFileGeneratorcustom tool entries from your feature files.
Additional Options (Prior to SpecFlow 3)
TechTalk.SpecFlow.targets file defines a number of default options in the following section:
<PropertyGroup> <ShowTrace Condition="'$(ShowTrace)'==''">false</ShowTrace> <OverwriteReadOnlyFiles Condition="'$(OverwriteReadOnlyFiles)'==''">false</OverwriteReadOnlyFiles> <ForceGeneration Condition="'$(ForceGeneration)'==''">false</ForceGeneration> <VerboseOutput Condition="'$(VerboseOutput)'==''">false</VerboseOutput> </PropertyGroup>
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.
<PropertyGroup> <ShowTrace>true</ShowTrace> <VerboseOutput>true</VerboseOutput> </PropertyGroup> ... </ItemGroup> <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')" /> ... </Project>
You can find an example here (project Bowling.SpecFlow)
SpecFlow prior to 2.4.1
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 using SpecFlow 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).