I am happy to announce a new series of blog posts dedicated to Specflow. In the first article, I am going to introduce to you the Specflow framework. You will be able to write and execute business-readable specification after 10 minutes. Keep reading!

Definition SpecFlow acceptance tests follow the BDD paradigm: define specifications using examples understandable to business users as well as developers and testers.

Installation and Setup

1. Install SpecFlow for Visual Studio (Open Extensions and Updates)



2. Install SpecFlow NuGet



3. Install SpecFlow for MSTest NuGet



4. Add Feature File



5. Create Your Scenarios



Before we proceed with the more technical stuff, I am going to explain what use case I am going to automate. Imagine that you are the nuclear engineer Guenter and you need to convert some metrics for example Kilowatt-hours to Newton-meters (you know nuclear stuff, can't I have some fun?).

Use Case

1. Navigate to Metric Conversions.



2. Navigate to Energy and Power Kilowatt-hours to Newton-meters conversion



3. Type Kilowatt-hours

4. Assert Newton-meters

Getting Started with Specflow in C#

In the feature file, I first describe what the feature needs to do. In my particular case, I want to use the scientific calculator to convert between different metrics. This is the power of SpecFlow it not only powers our tests but becomes application documentation too. Every scenario represents a different test or use case of the feature.



When the steps are displayed in purple, this means that we are not ready. To be able to execute the scenario you need to define the so-called bindings for each step. We need to create step definitions that bind the statements in the test scenario to the application code. I will show you in a bit how to generate them automatically or create them manually. But first, let's create the necessary page objects that will drive our test.

Create Page Objects

You can read more about this page objects in my article- Page Objects That Make Code More Maintainable. We need two pages- home page and Kilowatt-hours to Newton-meters conversion page. Below you can find the code of the home page.



The Kilowatt-hours to Newton-meters conversion page contains Asserter class too.



Except for the URL, the another important part is the convert method that types the kilowatt-hours to the input and waits for the answer field to show up.



Noting so special about the element map. As a fan of XPath, I find the links by their inner text.



Generate Bindings

There are a few ways to autogenerate bindings. Once you click insider the specification file and open the context menu, you will find the 'Generate Step Definitions' item.



Generate Regular Expressions in Attributes Bindings



The default option of generation is through regular expressions. A new file with the following methods will be generated.



The steps are found by SpecFlow through the regex expressions present in the different steps attributes. Also, the class should be marked with the Binding attribute. The (.*) parts of the expressions are injected automatically on test execution to the methods parameters. The automatic generation is not so smart all of the times so sometimes you need to fix the regex expressions manually.

Generate Method Underscores Bindings



The following file will be automatically generated.



Here the parameters are part of the name (P0, P1...PN). During test execution, they are again injected automatically to the methods parameters . If you want to rename the parameter, you need to change its name to the method's name as well.

Generate Pascal Case Bindings



The following file will be generated.



The file is almost identical to the previous one except the different words are not separated by underscores. Underscores are used only when the method accepts parameters.

Copy Bindings to Clipboard



Once you have the binding file but want to add some new step you can copy its content to the clipboard and add it manually to your file.

Populate Binding Methods

Here is how the binding class will look like using the already created pages.



Execute SpecFlow Tests

Once we build the project, our test/scenario will show up in the tests explorer.



Test Outcome

The generated output from the test execution is nice. You can observe each executed step and its execution time.

