During a recent BizTalk project, one of the customer requirements was that system tests were to be written using Specflow.
Specflow is an open-source framework which allows automated tests to be defined as Behaviour Driven Design (BDD) style specifications. Test scenarios are written using the Gherkin syntax which uses special keywords such as “Given”, “When” and “Then” and enables the scenarios to be written in a non-technical format that is easily understood by developers, testers and business users.
In this blog I will demonstrate
how to install Specflow and outline the steps that were used throughout the project to create the tests.
Specflow integrates easily with Visual Studio and requires the Specflow extension to be installed.
Open Visual Studio and select
Tools|Extensions and Updates, select
Online on the left hand side and then enter
Specflow in the search box on the right. Click the download button and then click on install:
(1) Create a Unit Test project
The first step is to add a Unit Test project to the solution and then use NuGet to download and install the Specflow package. There are two ways to do this. The first is to right-click the project and select
Manage NuGet Packages for Solution and then click
Alternatively, the package can be installed by selecting
Tools|Nuget Package Manager|Package Manager Console and using the following command:
PM> Install-Package Specflow
In order to use the MsTest test provider the generated App.config file has to be modified as follows:
(2) Create feature files
Specflow uses feature files to describe the feature that is being tested along with the test scenarios. Each scenario consists of a number of steps written in the Gherkin syntax. To add a feature file to the project, right-click it and select
Add|New Item and then select
Specflow Feature file. Here is an example:
(3) Create step definitions
Next, create step definitions that bind the steps in the feature files to automation code. There are two ways to generate them:
Here is an example of a step definition file:
Note: Right-clicking a step in the feature file and selecting
Go To Definition will result in the corresponding step definition being displayed.
Once the steps and step definitions have been created they can be reused in other scenarios.
(4) Create hooks
Hooks enable additional code to be run during the processing of feature and scenarios. There are a number available such as [BeforeTestRun], [BeforeFeature], [BeforeScenario] and they are run by adding the relevant tag to a feature or scenario. They can be useful in preparing an environment before test execution as shown by the example below:
(5) Run the tests
The final step in the process is to execute the tests. Ensure the solution builds without error and then open the Test Explorer window. Click
Run All to run the tests:
I found Specflow to be a useful framework for creating automated tests. The Gherkin syntax is easy to understand by both technical and non-technical members of the project team. The feature file steps and their associated step definitions can be reused so reduce the amount of time needed to create different test scenarios. Finally, combining Specflow with an open source generator like Pickles, means that living documentation can be created. Pickles uses the Specflow feature files to produce HTML files that are easy to read and navigable and on the customer project this was included as part of the build process.