Practical .NET
Business-to-Developer Bridge Building with SpecFlow 2
SpecFlow 2 makes sure developers deliver what businesses say they want. In this first of a two-part series, Jason looks at how to automate the process of communication through business-readable tests.
- By Jason Roberts
- 04/06/2016
Building the wrong thing -- something the business, client or customer doesn't want -- isn't only a waste of time and money, it can also be disheartening to the development team. This situation can also negatively affect the trust relationship between the development team (or individual developer) and the business.
This mis-delivery of features can be caused by a communications gap between the business and the development team. This means that the development team thinks they're building one set of features, while the business thinks they're getting a different set of features.
SpecFlow 2 allows the building of a bridge between the business and the development team by allowing the creation of business-readable tests.
Gherkin
SpecFlow represents tests at a higher level using natural language such as English (other languages are supported) in a business-readable, domain-specific language called Gherkin.
Gherkin is line-oriented (for example a "step" is on each line) that also allows comment lines to be created by prefixing the line with a # character.
Listing 1 shows an example of a feature file containing Gherkin. It consists of a feature name, followed by an optional feature description that can be any format -- in the listing the "In order to… As a… I Want…" format is being used.
Following the feature header are one or more scenarios ("tests"). A scenario starts with "Scenario: ", followed by the scenario name - in this case "Add two numbers". A scenario contains steps. A step is a description that ultimately maps to some test code that will be executed.
Listing 1: A Gherkin Feature
Feature: Calculator
In order to make it easier on my brain
As someone who is not good at mental arithmetic
I want to use the computer to do basic maths
Scenario: Add two numbers
Given The calculator is reset
When I enter 40
And I Add 20
Then The value should be 60
Gherkin scenario steps start with a number of keywords:
A scenario is divided into three logical phases or "blocks." The first block is the Given block. The Given block describes the starting state of the system before the main action(s) are taken. The actions occur in the When block, this is where the system is being manipulated to produce some response or ending state. It is the ending state that is examined in the Then block. The Then block describes what the resulting output or ending state should be after the steps in the When block have been executed.
There can be multiple Given steps, multiple When steps, and multiple Then steps in a scenario. To improve the fluentness of the scenario, the And and But steps can be used. For example, the scenario in Listing 1 could be written without the use of the And in the When block as shown in Listing 2. In this example the use of the And makes for a more readable, natural sounding scenario.
Listing 2: Repeated When Steps
Scenario: Add two numbers
Given The calculator is reset
When I enter 40
When I Add 20
Then The value should be 60
Installing SpecFlow 2 in Visual Studio 2015
The installation into Visual Studio 2015 consists of three distinct parts.
First is the "SpecFlow for Visual Studio 2015 Extension". This is installed via the Extensions and Updates dialog (as shown in Figure 1), which is accessed from the Visual Studio Tools | Extensions and Updates menu.
This extension provides IDE integration for SpecFlow feature files, including the ability to generate step definitions -- where the actual test automation code is written.
The second step is to install the SpecFlow 2 NuGet package. This can be done through the NuGet GUI or by using the command, Install-Package SpecFlow, in the Package Manager Console window.
The third piece is the testing framework that the test automation code will use (for example, to perform asserts in the Then phase). This could be NUnit or xUnit.net, for example. SpecFlow also provides "all in one" packages that will install the testing framework at the same time as the SpecFlow package, such as the SpecFlow.xUnit package or the SpecFlow.NUnit package.
Next time, I'll be looking at how the Gherkin steps map to actual automation code that gets executed.