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:

[code lang=”csharp”] public class Game
{
public void MakeMove(int index);
public Winner GetWinner();
}[/code]

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:

[code lang=”csharp”] [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()
{
}
}[/code]

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.

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

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

[code lang=”csharp”] [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
}
}[/code]

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.

[code lang=”csharp”] [Binding] public class TicTacToePlayGameSteps
{
private Game _game;

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

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