SpecFlow Integration in VS Code — Single IDE Across Different Platforms

Raj Uppadhyay
5 min readAug 29, 2021

--

SpecFlow is an awesome tool when we want to write and run BDD (Behaviour Driven Development) tests on .Net platform. SpecFlow allows you to separate test automation from specifications and provides additional tools to structure your code.

There are multiple IDE integration support available for developing, debugging and running SpecFlow tests using C# across platforms. However, with Mac machine or Linux, we struggle to manage our expectation with commonly available free IDEs e.g. VS for Mac as main features of tool like navigating from feature file to step definitions, generating step definitions and hence running the tests becomes a hardship.

As an alternative, either we have to try paid tools like “JetBrains Rider”. And as an additional overhead we have to switch between different IDEs when we develop on different platforms e.g. Windows/Mac or Linux.

Our teams were having hard-time in managing these concerns as we have to run and develop our solutions across platforms. After investigation, web searches and hit and trial with multiple IDEs. We came up with solution for using VSCode. However; working in VSCode is not straight forward but recent support on SpecFlow and few tricks we managed to work on VSCode as single IDE across different platforms.

In this post I will be sharing few tips + tricks to configure and run SpecFlow tests on VS Code. The great benefit which we get from this approach is not only cost saving but it makes easier to run the same tests across Windows/Linux or OSX in same IDE instead of switching between IDEs.

So without delaying further, lets start the journey…

Prerequisites:
1. Download and install VS Code

2. Prior working knowledge in C# projects and SpecFlow basics.

3. Install following extensions in VS Code:

4. Install .Net core framework on Machine.

Configuring the sample project:

For this post we will be using one of sample project (https://github.com/SpecFlowOSS/SpecFlow-Examples.git), shared by SpecFlow team on their website.

  1. Open VS Code and navigate to “Source Control” tab. Here select the option “Clone Repository”:

2. Enter the repo URL: https://github.com/SpecFlowOSS/SpecFlow-Examples.git and press enter

3. Provide a folder path on your machine (try to provide a path under your user just to avoid additional security/access related issues). After that click on “Select Repository Location”, it will clone the repo under selected folder.

4. Once done, it will show the success message with option to open the repo, click on “Open Folder” and select “OutputAPI/CalculatorSelenium” folder.

Select “CalculatorSelenium” folder and click Open button

5. Right click on the project and click on “Build .Net Core Project” menu option to build the project. After building the project, you can notice the tests in project at the left-bottom of VS Code window and/or clicking the “Test” tab:

Tests appearing on VS Code UI

Configuring the Feature file to Steps Definition Navigation:

  1. Open the settings screen by press “Command + ,(comma)” keys.
  2. Here search for “Cucumber” and look for “Cucumberautocomplete: Steps” option. Click on the “Edit in settings.json” link associated with this:
Click on the “Edit in settings.json” link

3. As an outcome of step#2 above, it will open the “settings.json” file. In this file replace the content with following (make sure text in bold i.e. Steps, this has to be exactly same as folder containing your step definitions CS files):

{
"workbench.colorTheme": "Visual Studio 2019 Light", "cucumberautocomplete.steps": ["/**/Steps/*.cs"],"cucumberautocomplete.customParameters": [
{
"parameter": "\"(.*)\"",
"value": ".*"
},
{
"parameter": "\"\"(.*)\"\"",
"value": ".*"
}]
}

Updating settings.json file

4. Open the feature file located in “Features” folder, and hold “Command” key and click on any step. It will start searching for the step definition (please wait for the operation to complete).

5. After the process, holding command key and clicking on any step will navigates you to the associated function in the steps definition file. Or alternatively you can click on any step and press F12 key or similar to “Visual Studio” right click on step and select “Go to Definition” option.

Writing a new Feature file and Generating the Step Definition file:

  1. Right click on “Features” folder and select “New File” and name it as “VSCode.feature”

2. Add some new steps in the file e.g. add following steps and you noticed all steps are appearing as underline with waved lines (as there is no step definition is available)

Missing steps definiton

3. Build the project again and run following command in terminal window (if terminal window is not opened then select “Terminal=> New Terminal” open from top menu. (Please note “newtest” is the tag name in above feature file)

4. Now if we scroll in the Terminal window we can find the binding class code (as shown below) please copy the code except the name space:

Binding code generation

5. Now right click on Steps folder and create a C# class and paste this code. Update the steps as required:

6. Build the project again and check if you are able to navigate to steps definition from feature file i.e. either holding command key then clicking the step or clicking the step and then pressing the F12 key

Hope this should help. Happy Testing!
Please do share your feedback and/or any improvements or any topics for future purpose.

--

--

Raj Uppadhyay

Testing professional with 17+ years of IT experience, specialized in the domain of Software Quality, Test Automation and Performance Architecture.