Practical .NET

SpecFlow 2, Part Two: Gherkin Steps

In part 2, Jason will map the business-readable tests we created last time to test automation code.

Last time, I introduced SpecFlow 2 and showed how to use it with the Gherkin business-readable, domain-specific language. I also offered up an example scenario (see Listing 1) to show how tests can be written in a higher-level, business-readable way. I'll take it another step and show how these high-level scenarios map to test automation code.

Creating Step Definitions
The steps in a scenario map to C# methods. It's inside these C# methods that the automation code is written. The "Add two numbers" scenario in Listing 1 has steps, each step (line) in the scenario will map to a separate method (though C# methods can be shared across steps to reduce maintenance overheads).

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

One of the features of the Visual Studio IDE integration is the ability to generate an initial steps definition class from a feature file. To do this, right-click in the feature file and in the context menu that appears, choose Generate Step Definitions, as shown in Figure 1.

[Click on image for larger view.] Figure 1. Generating Step Definitions

Choosing this option will bring up a dialog box with a number of options, as shown in Figure 2.

[Click on image for larger view.] Figure 2. Generate Steps Definition Dialog Box

Clicking the generate button will create a new C# class file that will be added to the Visual Studio project. This file (Listing 2) has the methods that map to the scenario steps in the feature file.

Listing 2: Generated Step Definition File
using System;
using TechTalk.SpecFlow;

namespace SpecFlow2Demo
{
  [Binding]
  public class CalculatorSteps
  {
    [Given(@"The calculator is reset")]
    public void GivenTheCalculatorIsReset()
    {
      ScenarioContext.Current.Pending();
    }
        
    [When(@"I enter (.*)")]
    public void WhenIEnter(int p0)
    {
      ScenarioContext.Current.Pending();
    }
        
    [When(@"I Add (.*)")]
    public void WhenIAdd(int p0)
    {
      ScenarioContext.Current.Pending();
    }
        
    [Then(@"The value should be (.*)")]
    public void ThenTheValueShouldBe(int p0)
    {
      ScenarioContext.Current.Pending();
    }
  }
}

There are a number of different ways that scenario steps map to C# methods. In Listing 2 the method being used is the attribute binding style. The [Given] [When] and [Then] attributes take a regular expression as a parameter; if this regular expression matches the text in the scenario then the method will be executed when that step executes.

Parameterized Step Definition Methods
In Listing 2 the When steps are parameterized. The regular expression contains "(.*)" that will match a value from the step text and parse it into a value that gets supplied to the parameter in the C# method. For example, taking the step text "When I enter 40," the "40" will be extracted by the regular expression in the "[When(@"I enter (.*)")]" step. SpecFlow will parse this and provide it to the p0 parameter in the "public void WhenIEnter(int p0)" method.

Adding Test Automation Code
Now that there are matching step definitions, the automation code can be written.

Listing 3 shows the Calculator class that's being tested.

Listing 3: Testing Calculator Class
namespace SpecFlow2Demo
{
  public class Calculator
  {
    public int Value { get; set; }

    public void Add(int number)
    {
      Value += number;
    }
  }
}

Listing 4 shows the completed step definitions with automation code added. Notice that the parameter names in the methods have been renamed from the default "p0" to something more readable.

Listing 4: Test Automation Code Added
using System;
using TechTalk.SpecFlow;
using Xunit;

namespace SpecFlow2Demo
{
  [Binding]
  public class CalculatorSteps
  {
    private Calculator _calculator;

    [Given(@"The calculator is reset")]
    public void GivenTheCalculatorIsReset()
    {
      _calculator = new Calculator();
    }
        
    [When(@"I enter (.*)")]
    public void WhenIEnter(int numberToEnter)
    {
      _calculator.Value = numberToEnter;
    }
        
    [When(@"I Add (.*)")]
    public void WhenIAdd(int numberToAdd)
    {
      _calculator.Add(numberToAdd);
    }
        
    [Then(@"The value should be (.*)")]
    public void ThenTheValueShouldBe(int expectedResult)
    {
      Assert.Equal(expectedResult, _calculator.Value);
    }
  }
}

Executing Tests with Visual Studio Test Explorer

Now that the test automation code is written, the scenario can be executed. The Visual Studio Test Explorer can be used to execute the tests (once the relevant test runner NuGet package has been installed) or by third-party commercial runners such as ReSharper or the SpecFlow+ Runner.

Once the NuGet runner package has been installed (for example, the "xunit.runner.visualstudio" package) the project can be built and the scenarios executed in the Test Explorer window, as shown in Figure 3.

[Click on image for larger view.] Figure 3. Executing Scenarios Using Visual Studio Test Explorer

Executing the AddTwoNumbers test will execute each of the bound Given, When and Then step definitions in turn. If the "Assert.Equal(expectedResult, _calculator.Value);" in the Then step passes, then the test will pass and change to green Test Explorer.

About the Author

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

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube