elllo
elllo

Reputation: 79

What am I doing wrong with specflow 3 and nunit?

I'm trying to setup a basic project in C# with Visual Studio 2017 that runs tests using specflow 3 and nunit. I had previously worked with specflow version 2.4.1 and the setup and running the tests was easy. However I keep getting errors while trying to setup specflow 3 with nunit.

I tried following the steps here: https://specflow.org/getting-started/

The furthest I have gotten is the below error.

[SpecFlow] System.Exception: Unit test Provider already set.

Also my code behind is NOT generating, but I have installed the nuget package SpecFlow.Tools.MsBuild.Generation which is supposed to generate the backend for the feature files.

Anyone know how I can get a C# project going with Specflow 3 and Nunit?

Upvotes: 1

Views: 7781

Answers (6)

Jakub Šenk
Jakub Šenk

Reputation: 1

I had the very same issue yesterday on VS 2022. I tried many stuff from this forum and other sources, but only the following steps helped me...

I have done:

  1. Remove all NuGet packages related to the SpecFlow from the project
  2. Remove SpecFlow extensions from VS
  3. Started again adding nuget packages according to the official documentation

Since I am using C#12 /.Net8, I had to install the newest extension from the official repository, which has not been released so far (for some strange reason):

https://github.com/SpecFlowOSS/SpecFlow/issues/2726#issuecomment-1831457636

The old extension version in nuget is not compatible with .Net8 so far.

After these steps, the solution starts to work.

Upvotes: 0

Deepak86
Deepak86

Reputation: 71

@Nkosi - I ran into the same issue as you have described with the same error message. However in my case, I was using MSTest unittest and SpecRun.SpecFlow. This is how I solved the problem in my case.

  1. Since we use MSTest we had the following nuget packages installed pertaining to MSTest (MSTest.TestAdapter & MSTest.TestFramework ) and also had SpecFlow.MsTest. I went ahead and uninstalled all these nugetpackages from nuget package manager and its dependencies.
  2. Now install SpecRun.Runner package alone which will inturn pull its other dependencies. As highlighted below, it has the SpecRun.NUnit packages as well. enter image description here

Now re-build the project and you should be able to see the Specflow tests written in your test explorer window.

Upvotes: 3

fayaz mohammed
fayaz mohammed

Reputation: 9

@Nkosi You might have already have more than one unit test provider package installed. Delete any specflow related like specflow.specrun, specrun.runner packages and Make sure you have installed the following packages if already exists re-install it again.

  • NUnit
  • NUnit3TestAdapter
  • SpecFlow
  • SpecFlow.Nunit
  • SpecFlow.Tools.MsBuild.Generation

And remove the Remove “SpecFlowSingleFileGenerator” from the Custom Tool field in the Properties of your feature files.

Add this target to the project file as per the spcflow documentation https://specflow.org/2019/updating-to-specflow-3/

Target xml code snippet

You can then able to see the tests in test explorer pane.I have run into the similar issues but I did re-installed the packages as I mentioned above that fixed the issues.Hope that helps!.

Upvotes: 0

GeniusCodeMonkey
GeniusCodeMonkey

Reputation: 156

SpecFlow 3 no longer uses the app.config. You need to use a specflow.json file which is mandatory.

Make sure you have the following Nuget libraries included:

  • Microsoft.NET.Test.Sdk
  • NUnit
  • SpecFlow
  • SpecFlow.NUnit
  • SpecFlow.Tools.MsBuild.Generation

Make sure you created a basic specflow.json file ...

{ "bindingCulture": { "language" :"en-us" }, "language": { "feature": "en-us" }, "plugins": [] }

... and you should be good to go.

Upvotes: 0

RaymundS
RaymundS

Reputation: 187

Check your app.config and remove nodes referring to specflow and nunit. Also go to your existing .feature files, right-click, Properties. Leave Custom Tool blank.

Upvotes: 0

fubaar
fubaar

Reputation: 2519

I hit this problem when adding both the specflow.xunit nuget package and the specflow+ runner nuget package. Removing the specflow+ runner package enabled me to build and run the tests with xunit.

Upvotes: 2

Related Questions