SpecFlow Integration With Visual Studio Express Edition
SpecFlow Integration With Visual Studio Express Edition
Visual Studio Express Edition does not support SpecFlow integration for BDD. This tutorial will help you tweak Visual Studio to integrate with SpecFlow.
Join the DZone community and get the full member experience.Join For Free
Learn more about how CareerBuilder was able to resolve customer issues 5x faster by using Scalyr, the fastest log management tool on the market.
BDD (Behavior Driven Development) has become a buzzword in the software development lifecycle. Tools like SpecFlow and Cucumber are playing a pivotal role implementing BDD in Java and.NET stack projects.
Visual Studio Express (VSE) users want to integrate SpecFlow for BDD development, but VSE Edition does not support the SpecFlow integration for BDD.
To resolve this problem, we need to seed or tweak VSE to identify the SpecFlow Feature file and its associated Feature Glue code and Step Definition file to write and execute the test. The steps below will help to resolve the problem.
- Visual Studio Express 2015/2013 – installed with.Net Framework 4.5.
- Basic knowledge of Visual Studio, C#, BDD concepts, and the SpecFlow tool.
Step 1: Install Required Artifacts From Nuget Packages for Specflow Integration
- Create a new Visual Studio unit test project and name as appropriate.
- Delete the Unit Test file listed in the project.
- Install required NuGet packages for the project.
- NUnit Framework
NuGet Package installation and integration with the project:
- Right-click the Project in Solution Explorer
- Select “Manage NuGet Packages” from menu (NuGet Package manager screen will be displayed) Click the “Browse” Tab => Search for Required package
- Install the Packages. (After installation, the installed packages will map appropriate binaries under a newly created folder named "packages" in the project solution location. The associated dlls of the installed packages will be listed under “References” of VSE Solution Explorer.
Step 2: Map SpecFlow Templates With Visual Studio Express Edition
When a user creates a new Visual Studio project, after SpecFlow package installation, the default SpecFlow templates like SpecFlow.Feature, SpecFlow.StepDefinition and SpecFlow.EventDefinition will be listed in the New Item dialog of Visual Studio, except in Express edition. So, we have to make Visual Studio Express Edition to read/identify the SpecFlow files by adding SpecFlow templates in the Visual Studio Template folder. To do that:
- Download the SpecFlow Templates for Visual Studio from here.
- Unzip and keep the SpecFlow folder under the Visual Studio Templates folder (Template folder location: D:\Users\<username>\Documents\Visual Studio 2015\Templates\ItemTemplates\Visual C#\).
- Restart Visual Studio Express to get the newly added templates to list in the project.
Step 3: Create SpecFlow Feature With Visual Studio Express
- Right click the Project from Solution Explorer.
- Select Add => New Item => SpecFlow Feature. The Add New Item Dialog will pop up.
- Select SpecFlow feature file from the Add New Item dialog to create the feature file. Name the Feature file as appropriate.
Step 4: Generate Glue Code for the Feature
When a user creates a SpecFlow feature, it automatically generates the appropriate Glue code along with the feature file. This doesn’t work with Visual Studio Express Edition. So, we have to generate the glue code separately for the feature using the specflow.exe file.
As a permanent solution for the VisualStudio Express Edition, create a batch file and use the .bat file as an external tool. To develop a.bat file to generate glue code for.feature file:
- Create a batch file to generate Feature Glue code.
- Create a batch file with the below script.
echo Starting to generate SpecFlow unit tests SpecFlow generateAll %1 /force /verbose echo Finished generating SpecFlow unit tests
- Name the batch file generateSpecFlowGlue.bat (or any other indefinable name).
- Drop the batch file in the project file location and execute the.bat by double clicking the same or through command prompt.
- Once the bat file executes without any error, the glue code will be generated for the available features. It will generate .cs file(s), with the feature name prefixed, e.g. AddTwoNumbers.feature.cs.
- Add an external tool command in the Visual Studio Express.
- Select Tools => External Tools => Add (Refer attached image ExternalToolsConfig).
- Click – Ok. The newly created tool command will be listed as “generateSpecFlowGlue” in the “Tools” menu. Click the menu item to generate the Feature Glue code. The Glue code will be generated on the Feature location. Refer to Alister's blog for more details.
- Open the generated Glue code, which works as a synchronizer for the associated feature.
- Add Glue Code to the Project
- Right click the project from Solution Explorer.
- Select “Add Existing Item” and Select the newly created glue code.
Warning: Don’t change anything from this Glue code. This .cs file is structured based on the Feature file. Any change in the file may create compiler issue, need to debug or regenerate the glue code to get back to the normal shape.
Step 5: Create StepDefinition File
Step definition is a real script which will do the actual test as defined in the feature scenario. To create a step definition:
- Right click the project from the Solution Explorer.
- Add a New Item and select SpecFlow StepDefinition.
- Name it feature-StepDef (Eg.AddTwoNumbers-StepDef).
- Write the steps to execute the steps.
Step 6: Update the App.Config With Test Adapter
Once the StepDefintion is framed, we need to instruct Visual Studio about the UnitTest Provider. To instruct Visual Studio, update the app.config file with UnitTest Provider in the configurations.
<specFlow> <unitTestProvider name="MsTest" /> </specFlow>
Step 7: Execute the Test
- Clean and rebuild the project.
- Select the listed test from the Test Explorer (to view Test Explorer, select Test => Windows => Test Explorer).
- Run the test. The selected test will be executed and will display execution results in the Test Explorer. Refer snapshot of the Test results.
Opinions expressed by DZone contributors are their own.