Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps / testing

How-to: End to End Automated Testing with Specflow

4.00/5 (1 vote)
28 May 2014CPOL3 min read 34K  
End to end automated testing with Specflow

End-to-End testing is about testing a full “slice” of an IS, from user input to tiers (DB, service provider) and back to the system output.

For this concern, at Betclic, we use 2 tools:

Quick Behavior Driven Design Glossary

Spec (specification)
Description of how an information system should behave
Feature
Part of an Information System, that has its own behavior
Scenario
Part of a feature
Step
Atomic operation of a scenario

Setup

Visual Studio Extensions
Fisrt of all, install the Specflow VS extension.

specflow extension

Specflow extension for VS 2013
Nuget Packages
In your target test project, add the following packages:
  • PM> Install-Package SpecFlow
  • PM> Install-Package Selenium.WebDriver
  • PM> Install-Package NFluent
App.config
By default, Specflow uses NUnit. As the recommended test framework is MSTest, you have to tune the concerned config section.
XML
<specFlow>
    <!-- For additional details on SpecFlow configuration
         options see http://go.specflow.org/docun-config -->
    <unitTestProvider name="MsTest" />
</specFlow>
Files Organisation
At the of the Test project, add a “Specs” and a “Steps” folders.The first one will hold the Gherkin files, the second one will contain the associated transcription of spec into C#.

First test

Description

Let us say we want to test the navigation of our menu bar.
In a default MVC Website, I added a new “StaticContent” controller, and added a link to it in the layout’s menu.
We want to check if a click on this link browses to the target page.

Gherkin

First, we create a “specification” in Gherkin langage.

Right-click on the Specs folder and add a new SpecFlow Feature file:

Add a Feature File

Add a Feature File

And name it “Navigation”. This file will specify all the business navigation rules over your website.
Add the following content:

Feature: Navigation 	In order to browse the site 	As an user 	I want to use the naviagtion bar Scenario: Browse to StaticContent page 	Given I am on the home page 	When I click on StaticContent link in navigation bar 	Then I should land on StaticContent page

A feature and its first scenario

Pretty self-describing, isn’t it? The main advantage of the Gherkin language is that everybody, from Business to QA people have the same understanding of it.
By sharing a common language, we are able to efficiently communicate, and build a system that purely reflects the business needs. Even better, you can build those specifications with the business and QA.

Translate to Steps

By right-clicking in the feature file and selecting “Generate Step definition”, you will launch a wizard tool that will create the step file skeleton.

Target the “Steps” folder before generating the steps. ;-)

You should have a new test file with 3 blocks:

Default step file skeleton

Default step file skeleton

Our scenario implies the use of a browser. Here comes WebDriver, that allows us to abstract the communication with the browser.

For this, we start by sharing the same instance of browser execution between the different steps of our secnario:

Browser initialize and tear down

Browser initialize and tear down

Now we are able to “web-drive” our browser with an easy API:

Steps implementation

Steps implementation
It’s a simple transcription of the scenario:
Given: Initial state, load the default page of the website
When: Description of an action, here, the click on the StaticContent page’s link
Then: the expected state: i.e., do we have browse to the StaticContent page ?

Run the test: a “web-driven” Firefox window will be launched by WebDriver/Selenium, and perform few actions.

Test explorer result window

Test succeded!

Conclusion

In this article, we saw how to setup a functional environment for starting Behavior Driven Design on web application.

To achieve this, we rely on the pair Specflow / WebDriver.

More to come about WebDriver API, stay tuned!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)