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.

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.

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:

  • Given
  • When
  • Then
  • And
  • But

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.

[Click on image for larger view.] Figure 1. Installed SpecFlow Extension

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, 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.

About the Author

Jason Roberts is a Microsoft C# MVP with over 15 years experience. He writes a blog at, has produced numerous Pluralsight courses, and can be found on Twitter as @robertsjason.

comments powered by Disqus


  • Blazor's Future: gRPC Is Key

    Blazor guru Steve Sanderson detailed what Microsoft is thinking about the future of the revolutionary project that enables .NET-based web development using C# instead of JavaScript, explaining how gRPC is key, along with a new way of testing and a scheme for installable desktop apps.

  • Don't Do It All Yourself: Exploiting gRPC Well Known Types in .NET Core

    If you're creating business services that send dates and decimal data then you may be concerned that gRPC services don't support the relevant data types. Don't Panic! There are solutions. Here's how to use them.

  • Sign

    Microsoft Points Blazor to Native Mobile Apps

    Blazor, the red-hot Microsoft project that lets .NET developers use C# for web development instead of JavaScript, is now being pointed toward the mobile realm, targeting native iOS and Android apps.

  • Circl

    Implementing State in .NET Core gRPC Messages with oneof

    In the real world, you've been dealing with the State pattern every time you designed a set of database tables. The Protocol Buffers specification lets you do the same thing when you define the messages you send and receive from your gRPC Web Service.

  • C#, .NET and SQL Server Make List of Top In-Demand Programming Skills

    Microsoft-centric technologies are featured prominently in a new examination of the top in-demand programming skills published by careers site

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events