Built tests are not added to the Visual Studio Test Explorer window - specflow

I set up the SpecFlow plug-in for the Visual Studio 2013.
I created new Feature Item and build it.
Result: built test is not shown in the Test Explorer. Why?
I do it according to this video

in order to configure it to generate MSTest tests you need to add this to your app.config:
<specFlow>
<unitTestProvider name="MSTest"></unitTestProvider>
</specFlow>

I experience this same problem, but the solution was different.
Following the advice on a SpecFlow Github issue, the following worked for me:
Close all Visual Studio instances
Go to %TEMP%\VisualStudioTestExplorerExtensions\
Delete all the folders in here
Try again

if the tests are not showing in the test explorer you need to install NUnit test adapter :
go to Tools->Extensions and updates ->Online->Visual Studio Gallery->Tools->Testing and select NUnit Test Adapter

Visual Studio can change the default processor architecture when it loads. While looking endlessly trying to figure out the issue, I finally figured it out, that it wasn't really Specflow causing the issue.
If you have references that require a special architecture, it will prevent the tests from showing up in the test explorer. Change the architecture to x86 or x64 based on your needs.
Another thing you can do to see the build issues is open the Output window and change the dropdown to Tests during build. It will highlight any information of things taking place during your build.
I felt somebody should mention it just in case.

I'm running SpecFlow tests with SpecRunner and had this same problem, a couple of times as a matter of fact.
Having tried all of the above suggestions, I found that the only solution to the problem was to uninstall and reinstall the SpecRun NuGet package. Next build, tests present in Test Explorer window as expected.

In my case the reason why tests weren't discovered were because I was using Resharper build.
As soon as I went to: Resharper -> Options -> Tools -> Build -> General: "Use ReSharper Build"(untick), then all tests were suddenly discovered.

In my case all the approaches above didn't work. My solution: I don't know why, but - I re-installed "Specflow for Visual Studio 2015" in "Tools => Extensions and Updates", then I've created a new feature file (old still didn't work), and this new feature-file works correctly.

For me, I changed from using direct DLL references to using the NuGet references of version 3.1.7.4 of SpecFlow and this solved the problem.
I needed to reference "SpecFlow", "SpecFlow.NUnit" and "SpecFlow.Tools.MsBuild.Generation".
and then clicked the "Show All Files" button in Visual Studio to see the code behind file at a sibling level to it associated Feature file (this used to be an associated child file of the feature file in older versions).
I also need to use the "Include In Project" context menu item on the code behind file, for tests to show up in Visual Studios "Test Explorer".
NOTE: I did try to unzip the Specflow required files from the NuGet package and reference them directly in my project to be consistent with other direct references in my project (non-NuGet references) but this doesn't seem to work and you have to use the NuGet reference. I found an closed issue in github, which states that we have to use the Nuget reference to get the tests to work in Test Explorer:
https://github.com/techtalk/SpecFlow/issues/1617

For me, the issue was the TestAssemblyPath in my srprofile file. It didn't match my actual dll name.

I tried installing the Nugget package
SpecFlow.Tools.MsBuild.Generation
with the same version than specflow and now I can see the test in TestExplorer, other ways not works for me.
I already had installed 1 from
Nugget Package Specflow
Specflow.NUnit
Nunit
Nunit3TestAdapter
and the extension for Visual Studio for Specflow a NUnitTestAdapter

Related

specflow generate step definitions missing from context menu

I followed the getting started instructions and the "generate step definitions" is missing from context menu.
I tried adding the additional specflow packages from nuget for mstest and still nothing.
I had the same problem. I was looking for this option in contextual menu of the feature file, but this option exists in the contextual menu of the content of the feature file. So, try open the feature file (*.feature) in the Visual Studio text editor and click right button of the mouse inside text editor. Example of the file:
Scenario: Add two numbers
Given I have entered 50 into the calculator
And I have entered 70 into the calculator
When I press add
Then the result should be 120 on the screen
Assuming you are bringing up the correct context menu by right clicking in the .feature file as opposed to anywhere else then you may find that the issue is actually caused by the TechTalk.SpecFlow.dll not being referenced in your project.
To add the reference manually (NuGet didn't do it for me) right-click on the References folder of your project and find the TechTalk.SpecFlow.dll which will likely be in '[project]/packages/SpecFlow.X.X.X/lib/netXX/' (X's are version numbers).
Here is the context menu when I don't have the correct reference in place:
And here it is once I do:
This was SpecFlow.2.1.0 and VS2010 which led me to another issue with incompatible .Net versions so I ended up using SpecFlow.1.9.0.
Try to install SpecRun.SpecFlow from the Nuget Packages, after installation completes you'll get the "Generate Step Definitions" on the context menu.
It worked for me.
Install specflow from Extensions and updates and then restart the visual studio, clean and rebuild the solution
I had the same problem. maybe it was related with fresh IDE installation or maybe it was something new... here is just short note about this issue and possible solution:
At the beginning of IDE/project configuration check or install packages for SpecFlow in the NuGet manager.
Check twice what exactly you will use in the project Xunit or Nunit and then install the related package Xunit or Nunit. In my case, I was working with Xunit and install it.
After installation, you will see new items in the References
and pop about re-generation feature files
After that Generation steps will be available
This is because you may not have the simply installing the Extension for SpecFlow at Solution level is not enough.
SpecRun.SpecFlow is an evaluation version unless you are prepared to play for a license key.
I added the NuGet package SpecFlow.xUnit to the Unit test Project within my solution where I held my feature files. This resolved the issue, I now see Generate step definitions.
This is achieved by Right-clicking on your feature file in the code editor and select Generate Step Definitions from the popup menu. A dialogue is then displayed.
This happened to me when I migrated an existing project to a new PC.
Turns out I need to do: Visual Studio 2019 > Extensions > Manage Extensions > select 'Online' in the left hand nav > then search for 'Specflow' in the search box > then download it and close Visual Studio. It auto installs then.
Finally reopen VS and it should work.
In Visual studio 2022 the context menu item "generate step definitions" renamed to "Define steps" as per below specflow documentation.
https://docs.specflow.org/projects/specflow/en/latest/visualstudio/Generating-Skeleton-Code.html

TFS nuget packages not being restored

I have a small solution containing three Visual Studio projects. I'm working in Visual Studio 2015 using TFS 2015.
I have implemented a gated check in, but for some reason the solution will not build on the TFS server. I'm referencing only 1 nuget package - Entity Framework. I am not checking my package folder into TFS, but my packages.config files are being included.
I have previously set up a different project on the same server using the same build definition and it works fine.
In order to restore packages prior to build, you will need to run the following command as part of your build process.
nuget.exe restore path\to\solution.sln
One way to do that is to add another project that is responsible for building your solutions and making sure that the packages get restored prior to your solutions being built.
Following write-up walks you through getting that set up: nuget docs
I managed to get it working, but I tripped into the fix and don't know what exactly solved the problem. This is the first time I've really had to handle TFS builds.
I know I only had one build definition defined and it was intended for a different solution - of which this code was also a part. I think when I was checking in this solution it was actually trying to build the other.
Apparently, I can't have my nuget packages set up different ways for code that is in two different solutions. Anyway, that's my best guess.

TFS won't compile string interpolation syntax

I updated to Visual Studio 2015 when it was released last week. Resharper suggested that I might like to update the following string using string.Format:
string filePath = HttpContext.Server.MapPath(string.Format("~/App_Data/{0}.xlsx", Guid.NewGuid()));
To use string interpolation:
string filePath = HttpContext.Server.MapPath($"~/App_Data/{Guid.NewGuid()}.xlsx");
I did this, and all is well.
However I've broken the build on TFS:
Controllers\MyController.cs (224):
Unexpected character '$'
So it seems like the new features in C# 6 can't be compiled by our older, non-updated version of TFS.
The project is still targeted at using the Framework 4.5 however, I have not targeted 4.6. I (wrongly) assumed that still targeting the old framework would mean it could be built by other team members still using VS2013 along with our TFS build server.
So it looks like our TFS needs an update of... something.
What do I need to install on the TFS server to have it compile this new syntax? The latest version of the .NET Framework or something else?
Install .NET Framework 4.6 and Build Tools 2015 on your build server.
Then override the ToolsVersion (/tv:14.0) in MSBuild arguments.
Unfortunately, there doesn't appear to be an easy way to get older versions of TFS working with C# 6.0 without installing VS2015 on the TFS build server. If you can do that, I would suggest that. If, for whatever reason, you cannot, then the following worked for me:
I followed the instructions of the other answer, but that alone didn't work. I also had to edit the default TFS build process template (which in my case is found in $//BuildProcessTemplates) and change the ToolPath properties of the "Run MSBuild for Project" nodes to the location of MSBuild 14.0 (which usually is C:\Program Files (x86)\MSBuild\14.0\Bin).
You will then need to update your build definitions to use the new build template (if you had copy/edited a new one vs editing the original one).
At this point, you should be able to build C# 6 projects with TFS, but if you're trying to do web deployment/packaging as well, then there are still a few more steps.
On the build server, at C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0, there should be a Typescript folder and nothing else. On your local machine with VS2015, go to the same path. There should be a Web and WebApplications folder. Copy those over to the server's folder. You should now be able to package/deploy web projects as well.
Please check to see if "Run MSBuild for Project" exists more than once within your TFS Build process template. In my case it existed twice and took me awhile to figure out why the change for ToolPath was not taking the desired effect.

BuildActivity ignores ToolsVersion

I have a Continuous Integration Build Running on a BuildAgent which I can not get working properly. I recently changed our codebase to C# 6.0 and changed the projects to ToolsVersion 14.
I did install Visual Studio 2015 on the Build Server (TFS 2012). If I do now check in the C# 6.0 code the build fails on the first project ("$" is unexpected or something")
The BuildTemplate is "DefaultTemplate.11.1"
If I check the log files the build calls the wrong csc.exe (C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Csc.exe)
if I do start the MSBuild/14.0/Bin/MSBuild executable with the project without any switches the Build succeeds. Why does the TFS BuildAgent not use the ToolsVersion and how can I get TFS 2012 to build my Visual Studio 2015 solution
If I add the MSBuildArgument /tv:14.0 it still fails. As it calls th Version 12 of MSBuild which is part of the .NET 4.6 RC Framework which ships with Visual Studio 2015.
Changing the ToolsPath of the MSBuild Activity does get the Build working but spits bullets if I try to run my UnitTest. I get aMissing dlls "Microsoft.VisualStudio.TestPlatform.Utilities.dll" exception. (VS2012 is installed as well on the Build Server).
UPDATE:
I do now get a different error with my unit tests:
TF900548: An error occurred publishing the Visual Studio test results.
Details: 'Method not found:
'System.Collections.Generic.IEnumerable<System.String>
Microsoft.VisualStudio.TestPlatform.Utilities.InferSettingsHelper.MergeRunSettingsAndFindCompatibleSources(Microsoft.VisualStudio.TestPlatform.ObjectModel.Architecture
ByRef,
Microsoft.VisualStudio.TestPlatform.ObjectModel.FrameworkVersion
ByRef, System.String, System.String,
System.Collections.Generic.IDictionary`2<System.String,System.Collections.Generic.KeyValuePair`2<Microsoft.VisualStudio.TestPlatform.ObjectModel.Architecture,Microsoft.VisualStudio.TestPlatform.ObjectModel.FrameworkVersion>>,
System.Xml.XPath.IXPathNavigable, System.String ByRef)'.'
For those of us not living inside build templates every day here's the steps that got it working for me:
First find the template associated with your build (if you already know where to find it skip a couple steps).
Just right click the build definition and select Edit build definition
Select the Process tab and you'll see the name of the template (quite likely to be DefaultTemplate if you haven't customized it). On the far right click Show details to see the path for the template.
Go to the Home tab under Team explorer and click Source control explorer
Find the template file and double click to edit it. It should come up in a graphical view like this. Scroll till you find Run MSBuild for Project
Click on this item which will immediately bring up properties
Click on the little icon on the right - don't try and type it in because it'll probably explode and crash Visual Studio.
Enter IN QUOTES the following "C:\Program Files (x86)\MSBuild\14.0\bin"
NOW FOR THE MOST IMPORTANT PART
For me the Run MSBuild for Project was in TWO places.
So I just repeated the above steps in both places, and then after I checked in the .xaml file my build FINALLY worked fine.
You may need to recycle the agent / controller from within the TFS tool.
Edit template change msbuild activity ToolPath to "MSBuild/14.0/Bin/MSBuild"
See answer by Marson.
Using the TfvcTemplate.12.xaml build process template with the /tv:14.0 Build argument worked for me.
use msbuild aruguments /p:VisualStudioVersion=14.0

Specflow Association with .feature Files

I have installed Specflow via Nuget, but .feature files do not appear to be associated with Specflow. The TechTalk.Specflow DLL is present in the references for the DLL containing my tests.
Right clicking on a feature file and selecting Run Tests results in a dialog No tests found in file.
Can anyone help me diagnose this?
The nuget package gives you everything you need to run SpecFlow tests, but there is a separate installation # The Visual Studio Gallery which gives you the design time integration includign file associations

Resources