38
votes

Is it possible to "force" regenerate step definitions for a specflow feature file? I have created this feature file but cannot generate all steps. Am getting a message that all steps are already bound but when I run the test, I get an error that the same steps are not bound. I have cleared everything and recreated both feature and step definition but now I cant event generate ALL the steps because specflow says they are bound already.

11
Please mark the correct answer as resolved... Marcus' answer does exactly what you're asking forRiegardt Steyn
@Heliac, sometimes even rerunning the custom tool won't solve the problem. I've provided an alternate, more dependable solution below.Ryan Lundy

11 Answers

110
votes

SpecFlow creates a .cache file in your Temp folder that holds these bindings to speed things up. If that file gets out of whack, you can delete it:

  1. Exit Visual Studio.
  2. Open Windows Explorer.
  3. In the address bar, type %TEMP% and hit Enter to go to your temp folder.
  4. Find the files whose names start with "specflow-stepmap-YourProjectName" with a .cache extension.
  5. Delete those files.
  6. Start Visual Studio again.

If you do this, it'll take a little time for SpecFlow to regenerate the bindings. Until this is done, if you right-click on a step and select "Go To Step Definition", Visual Studio will reply "Step bindings are still being analyzed. Please wait."

I most commonly use this for the opposite case, where SpecFlow shows that nothing is bound even though the steps exist. But I think it's likely to work for your case as well.

13
votes

Right-click the .feature file and chose "Run Custom Tool". That will rerun the SpecFlowSingleFileGenerator that generates the code-behind in the test framework of your choice.

1
votes
  • Close Visual Studio
  • Open the .csproj in an text editor like NotePad ++
  • Assuming you are using SpecFlow 2.2.0
  • Assuming your features are in ./Features
  • In the <Project> area you should see some <Import> commands, add this:

  <Import Project="..\packages\SpecFlow.2.2.0\tools\TechTalk.SpecFlow.targets" Condition="Exists('..\packages\SpecFlow.2.2.0\tools\TechTalk.SpecFlow.targets')" />
  <!--<ItemGroup>
    -->
  <!-- include all feature files from the folder "FeatureFiles" -->
  <!--
    <None Include="Features\**\*.feature" /> 
  </ItemGroup>
  <Target Name="AfterUpdateFeatureFilesInProject">
      -->
  <!-- include any files that specflow generated into the compilation of the project -->
  <!--
      <ItemGroup>
          <Compile Include="@(SpecFlowGeneratedFiles)" />
      </ItemGroup>
  </Target>-->

Insert this snippet just inside the <Project></Project> part of you .csproj, next to the other <Import>s if you can. The commented part is important, it will regenerate the .feature.cs files

  • Re-open Visual Studio
  • Everytime you Build the .feature.cs files will be renegerated
  • You may want to add *.feature.cs to your .gitignore after this
1
votes

You can regenerate all feature file test classes using the SpecFlow-provided tool specflow.exe.

Assuming that SpecFlow has been installed through NuGet and is in the ./packages directory relative to your current directory, you can run, in the terminal, the command

.\packages\SpecFlow.X.Y.Z\tools\specflow.exe generateall path\to\myfile.csproj

which will regenerate all feature file unit tests. As the linked page states, you can add the /force flag to force regeneration of all tests.

0
votes

If you are using the SpecFlow plugin in Visual Studio, you can see which lines have bindings as opposed to the ones that don't due to the highlighting. In addition you can also press F12 to go to the items that have Bindings, generate a binding that can be pasted in where it doesn't exist.

Also I've not seen this process of generating all the steps in one go as you describe above. To be honest it seems a little counter intuitive as SpecFlow already specifically has warning conditions for when the steps don't exist.

0
votes

I have encountered the same problem. Restarting Visual Studio will get the job done - SpecFlow is caching the bindings somewhere.

This looks like a bug to me. It can be reproduced by moving the generated step class. I believe that when you click the "Generate Step Definitions" the engine hits the cache and does not check for the actual file.

I am testing this on Visual Sudio 2013 with SpecFlow plugin in a plain simple dll project.

0
votes

Restarting VS is cumbersome especially if you have big project.

We changed code of specflow plugin (https://github.com/techtalk/SpecFlow). Added command that reinitializes scope (refer to method EnsureInitialization) and cleans chached file.

Now we just click on button when bindings are changed.

0
votes

If you've been having problems with Specflow bindings such as:

  • steps are not recognized
  • no bindings could be found

and restarting Visual Studio or regenerating the bindings doesn't work, then the SpecFlow Troubleshooting Visual Studio Integration has various solutions for Visual Studio 2013 and 2015.

0
votes

I had to restart VS and it worked for me. Didn't need to delete any temp files or anything else.

yeah it's a little inconvenient but the lesser of all evils I think.

0
votes

Another one to check that fooled me is have you left the pending placeholder code in place (opps):

ScenarioContext.Current.Pending();

will flag the test as not implemented.

0
votes

I had a issue which is not updating the designer file of the feature file. I was able to fix that issue by doing the below workaround and now my feature file designers are getting updated. Tools -> SpecFlow -> General -> And Enabled the specflow single file generator.

Specflow version - 2.2.1 Visual studio 2017 and 2019