40

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.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Ibexy I
  • 1,123
  • 6
  • 16
  • 29
  • 2
    Please mark the correct answer as resolved... Marcus' answer does exactly what you're asking for – Riegardt Steyn Oct 18 '13 at 11:13
  • @Heliac, sometimes even rerunning the custom tool won't solve the problem. I've provided an alternate, more dependable solution below. – Ryan Lundy Feb 03 '14 at 19:49

13 Answers13

119

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.

Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211
13

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.

Marcus Hammarberg
  • 4,762
  • 1
  • 31
  • 37
  • 1
    The latest version of SpecFlow (1.9.0) also seems to detect out of date code generation and ask if you'd like to regenerate, which is nice! – Henry Wilson Oct 28 '13 at 15:52
  • I tried it - nothing happens if your code behind is moved in an upper folder. ( Visual Studio 2013 with SpecFlow plugin with SpecFlow 1.9.0.7 ). Some sort of a bug I think. Restarting VS helpes out. – Ognyan Dimitrov Jan 23 '14 at 11:30
  • 2
    Didn't work. The issue for me was binding cache is the trusted source but was wrong. Had to delete cache as described in Kyralessa's 6 steps. – Brantley Blanchard Aug 11 '14 at 16:26
  • @BrantleyBlanchard try closing & reopening VS, and then running this step. I changed my testing framework, was prompted to regenerate the bindings and said no. So I tried this, without closing VS, and it didn't resolve the problem. Closed VS, reopened, the bindings were still out, but this time, choosing "Run Custom Tool" resolved it. I was going to run [Kyralessa's answer](http://stackoverflow.com/a/21536348/734790) next if this failed. – Damon Sep 25 '14 at 11:26
1
  • 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
jmbmage
  • 2,487
  • 3
  • 27
  • 44
1

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.

Chris Midgley
  • 1,452
  • 2
  • 18
  • 29
1

Found bastard! Visual studio 2022 and restarting visual even windows didn't help.

but removing roslyn cache helped! AppData/Local/Microsoft/VisualStudio/Roslyn

0

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.

AlSki
  • 6,868
  • 1
  • 26
  • 39
0

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.

Ognyan Dimitrov
  • 6,026
  • 1
  • 48
  • 70
0

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

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.

Ralph Willgoss
  • 11,750
  • 4
  • 64
  • 67
0

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.

K-Dawg
  • 3,013
  • 2
  • 34
  • 52
0

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.

Ben
  • 623
  • 3
  • 12
0

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

0

In my case this was my first feature, just installed the Extension and nuget packages. All I had to do to get rid of the error was build the solution.

upizs
  • 89
  • 8