Get in touch

Get in touch with Binary

Binary Studio website uses cookies to enhance your browsing experience. By continuing to use our site, you agree to our Privacy Policy and use of cookies.

Learn more more arrow
Kirill Buga JS developer 29.04.2014

BDD. SpecFlow & Watin. Part II

Meet the second part of BDD tutorial, if you missed the first one, check it out right here

To start working with SpecFlow you should set up your IDE. To install SpecFlow plugin in VS go to Tools and open Extension and Updates, load and install SpecFlow and reload IDE. After installation is completed, create Class Library project. Using NuGet install SpecFlow.NUnit. Now specflow files are available and you can add .feature file to the project:

BDD Solution Getting Started.png

We are ready. Let’s imagine that our Product Owner asked to implement Calculator. Development team made the draft interface of Calculator

Test context is:

All we should do now is to write very simple test using TestFlow for testing addition of 2 numbers.

Test contains scenario and implementation’s steps. Steps always start with keywords Given, When, Then.

Given – precondition,

When — event, that triggers scenario,

Then — awaited result.

There is also term “And” available and it is duplication of above described construction provided for better readability of scenario.

For example:

Given I have logged as admin

And I have opened a page “My Page”

Is equal to

Given I have logged as admin

Given I have opened a page “My Page”

But how are we supposed to translate natural language into syntax construction. To do this you should call context-menu and select Generate Step Definitions; in the shown window, we choose Generate Steps and after that, we get something similar to code shown below.

As you’ve probably noticed, Specflow makes use of the special attributes for matching methods with scenarios steps. Possibility of this action is checked with regular expression. Thus we get implemented blocks that can be used in other test scenarios and automated tests that can be constructed from existing blocks by Testers. Only missing functionality-pieces need to be implemented by Developers. “5”, “6” and “11” are parameters for this scenario and they can be changed according to current test case.  Let’s write down now real implementation instead of the dummy one. First of all we are going to create constructor that uses implementation context as argument. SpecFlow supports injection into constructor, that’s why no null reference exception occurs. Scenario steps can be represented as follows:

To start test run call context menu on .feature file and select “Run SpecFlow Scenarios”. Well-known Unit Test Sessions (NUnit) window will be shown with all tests’ results included.

Unit test session.png

Report contains steps performed by scenario and result. Still SpecFlow is able to do much more. For example it can populate whole structure in one step as  it is described in following case: we need to complete customer’s information with valid data.

As you can see from example, SpecFlow uses such tables to pass named parameters into scenario. After performing generation, we get following result:

Now all data was passed into method using Table. To get their value, call createinstance <T> and use instead of T class to which you data is mapped. In our case it’s User.

The data list we can get alike.

The only difference is that we are calling CreateSet:

We have examined the simplest tests scenarios. But what should we do with testcases that can use different input arguments? We are supposed to use Scenario Outline.

Scenario will execute for each passed case (TestCase analogue in NUnit). Report will include all testcases with their arguments and result of execution.


We should also mention here precondition. Precondition is such a method that will be executed before/after scenario or before/after each testcase. It can be useful for setting initial test context before scenario or testcase. This can be in feature file or in code, with the help of BeforeStep, BeforeFeature, BeforeTestRun, AfterScenario, AfterFeature and other attributes. Here is an example.

SpecFlow is powerful instrument for writing tests in BDD development process. It allows you to write test using plain text which will be understandable for other non-technical persons. It is connecting link between business-analytics, users and developers of software product. But it can be used not only for desktop application. In the next article I’ll show you usage example of SpecFlow for Web applications with the help of Watin framework.

1.     http://www.specflow.org/getting-started/

2.     http://habrahabr.ru/post/182032/