Writing the First Acceptance Test with SpecFlow

/, Testing/Writing the First Acceptance Test with SpecFlow
This entry is part 2 of 3 in the series SpecFlow & Selenium

To write acceptance tests using SpecFlow, we need to properly set up the environment and a testing project.

At first, we need to install a SpecFlow extension for Visual Studio. We need it because SpecFlow integrates into Visual Studio allowing us to easily format features and scenarios, generate corresponding code and other things. The installation process is straightforward. The only thing is that it will require from you to restart Visual Studio.

Let’s assume that I have a class which implements the TicTacToe (Crosses and Noughts) game with the following API:

public class Game
{
    public void MakeMove(int index);
    public Winner GetWinner();
}

To write an acceptance test, create a separate project (class library). Install the SpecFlow NuGet package. Apart from that, we need to install a NuGet package for integration of SpecFlow and a unit testing framework which will be used to run tests and write assertions. In my case, I’ll install a package for integrating SpecFlow with NUnit. This package automatically installs the NUnit package to the project since it depends on it. Now we are ready to go.

At first, we need to add a spec flow file. Go to the project and add a new item, search for SpecFlow section, select it and select the “Feature File in English”. It’s hard to write high-level business requirements and scenarios for such a simple game, so we will pretend a bit.

Let’s describe a feature.

Feature: TicTacToe Play Game

       In order to play a TicTacToe game

       As a gamer

       I want to be able to put crosses or zeroes and determine a winner

Let’s describe at least one scenario.

Scenario: Play game
       Given I started a new game
       When I put three crosses in a row       
       Then I should win

This is the simplest form of a scenario. We can pass tables of values or constants to code from scenarios. SpecFlow supports many cool features.  To execute such a description, you need to generate C# code and implement the test. To generate the code, click on the right mouse button somewhere in the feature file and click on the “generate step definitions”.  In the popped up window, click “generate a file”. The following file will be generated:

[Binding]
public class TicTacToePlayGameSteps
{
    [Given("@I started a new game")]
    public void GivenIStartedANewGame()
    {
    }

    [When(@"I put three crosses in a row")]
    public void WhenIPutThreeCrossesInARow()
    {
    }

    [Then(@"I should win")]
    public void ThenIShouldWin()
    {
    }
}

Here you can see the generated testing class. SpecFlow applied special attributes like Binding and Given, When and Then for the steps.

Let’s implement the test.

Given, when, and then steps directly map to the corresponding arrange-act-assert triplet. So, at the given step we need to arrange the object under test. Let’s do this.

[Binding]
public class TicTacToePlayGameSteps
{
    private Game _game;
    [Given("@I started a new game")]
    public void GivenIStartedANewGame()
    {
        _game = new Game();
    }
}

At the When step, we should perform actions. Let’s implement it.

[Binding]
public class TicTacToePlayGameSteps
{
    private Game _game;
    [When(@"I put three crosses in a row")]
    public void WhenIPutThreeCrossesInARow()
    {
        _game.MakeMove(1); //1
        _game.MakeMove(4);
        _game.MakeMove(2); //2
        _game.MakeMove(5);
        _game.MakeMove(3); //3
    }
}

The combination of crosses put in the fields indexed first, two and three should lead to winning. Let’s verify this fact at the Then step.

[Binding]
public class TicTacToePlayGameSteps
{
    private Game _game;

    [Then(@"I should win")]
    public void ThenIShouldWin()
    {
        Assert.AreEqual(Winner.Crosses, _game.GetWinner());
    }
}

After rebuilding, Visual Studio should detect the acceptance test and allow to run it as a regular unit test. This is how you can create executable documentation easily with the power of SpecFlow. I bet you see the difference between acceptance testing a unit testing. Acceptance tests are understandable for domain experts, while unit tests are not. Want to fill in the gap between domain experts and programmers, then learn SpecFlow and implement use cases in high-level business-readable acceptance tests.

I have a video course:
“Automate Application with SpecFlow and Selenium WebDriver in C#”.

You, as a reader of my blog, are eligible for taking a maximum possible discount.

Series Navigation<< Acceptance and UI Testing with SpecFlow and SeleniumIntroduction to Selenium >>
By |2018-11-12T00:05:17+00:00October 22nd, 2018|.NET, Testing|0 Comments

About the Author:

I'm thankful enough for that I love what I do. I began my career as a postgraduate student participating in Microsoft ImagineCup contest. I've been working with .NET platform since 2003. I've been professionally architecting and implementing software for nearly 7 years, primarily based on .NET platform. I'm passionate about building rich and powerful applications using modern technologies. I'm a certified specialist in Windows Applications and Service Communication Applications by Microsoft. "If it's work, we try to do less. If it's art, we try to do more." - Seth Godin. What I can say is that software is my art.