DEV Community

Cover image for Getting started with SpecFlow
Shish Singh
Shish Singh

Posted on • Edited on

Getting started with SpecFlow

SpecFlow is a test automation solution for .NET built upon the BDD paradigm. Use SpecFlow to define, manage and automatically execute human-readable acceptance tests in .NET projects (Full Framework and .NET Core).

SpecFlow tests are written using Gherkin, which allows you to write test cases using natural languages. SpecFlow uses the official Gherkin parser, which supports over 70 languages. These tests are then tied to your application code using so-called bindings, allowing you to execute the tests using the testing framework of your choice. You can also execute your tests using SpecFlow’s own dedicated test runner, SpecFlow+ Runner.

Prerequisites

  • Basic understanding of C#
  • Visual Studio 2017 or later

What is BDD

BDD is BDD (Behavior-driven development) Testing is a technique of agile software development and is as an extension of TDD. The idea is to describe how the application should behave in a very simple user/business-focused language.

What is Gherkin

Gherkin is a structured language it follows some syntax let us first see a simple scenario described in gherkin.

Feature: Search feature for users This feature is very important because it will allow users to filter products

Scenario: When a user searches, without spelling mistake, for a product name present in inventory. All the products with similar name should be displayed

Given User is on the main page of www.myshopingsite.com

When User searches for laptops

Then search page should be updated with the lists of laptops

Gherkin contains a set of keywords which define different premise of the scenario. As we can see above the colored parts are the keywords. We will discuss about the gherkin test structure in details later but the key points to note are:

  • The test is written in plain English which is common to all the domains of your project team.

  • This test is structured that makes it capable of being read in an automated way. There by creating automation tests at the same time while describing the scenario.

Setting up SpecFlow

  • Create a new project using the visual studio
    File -> New -> Project

  • Select windows from the templates option and then select
    class library.

  • This will create a blank project with a class library having
    a plain class file.

  • Now click on the tools option on the top menu bar of the VS.
    Select extension and updates.

  • Search "SpecFlow for visual studio" latest option from
    online repository.

  • Post this please restart your visual studio instance to let
    the extension added to the VS.

  • Now open the class file and add nuget reference of
    "SpecFlow" by installing it. Last step would be to add NUnit
    Adapter and NUnit Framework from nuget.

Adding SpecFlow feature

Following are the steps which will help in adding the feature file of our specflow.

  • Right-click on the solution file and select add followed by
    new item.

  • Now select specflow feature file and create a new feature
    file. While providing the name of file make sure the
    extension is ".feature".

  • Please verify a dummy feature file getting created. It
    primarily holds two parts. First, holds feature tag. This
    defines the need and reason of this feature file. Second, is
    the scenario tag. This holds the setups required to satisfy
    the test scenario. This will also be mapped to another file
    call step definition file. Each statement will be mapped
    with the specific step definition.

  • Right-click on your feature file in the code editor and
    select Generate Step Definitions from the popup menu.

  • This displays a Pop Up window, which will ask to select the
    statements for which Step Definition file is to be created.
    Select all and click on Generate button.

  • It will ask to specify the folder path to save the Step
    Definition file, let it be a default project folder and hit
    the Save button. All the statements will change the color
    now, it means these Feature statements are linked with Step
    Definitions.

  • To have a look at the attached definitions, click on any
    statement and press F12 button. This will open up the linked
    definition file and the cursor will be pointing to the
    linked definition.

Executing test files

  • Now to run go to Test -> Windows -> Test Explorer.

  • Notice, at the left side a new window is appeared called
    Test Explorer. One test is also displayed in the Test
    Explorer window. Now to run a Feature Test, Right-click on
    the test in the Test Explorer window and select Run Selected
    Tests. This will run the selected test and display the
    output in the console window.

Note: We can also use XUnit instead of NUnit. Also, Always use names easily coinciding with the purpose for which we are creating it.

Connects

Check out my other blogs:
Travel/Geo Blogs
Subscribe to my channel:
Youtube Channel
Instagram:
Destination Hideout

References

Tutorials

Official documentation

Top comments (0)