Click here to Skip to main content
15,867,939 members
Articles / DevOps / Testing

How-to: End to End Automated Testing with Specflow

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
28 May 2014CPOL3 min read 33.4K   7   2
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)


Written By
Chief Technology Officer Betclic
France France
I am Head of Software Development at Betclic France. I manage the Paris Dev Team, consisting of 35+ talented people, in various technical and functional projects in the fields of sports betting, poker, casino or horse betting.

Check out our technical blog at https://techblog.betclicgroup.com
This is a Organisation

3 members

Comments and Discussions

 
QuestionSpecRun and outline scenario Pin
Member 1271127730-Aug-16 5:12
Member 1271127730-Aug-16 5:12 
QuestionAbout Selenium with Asp.net MVC and IIS Pin
deelll26-Mar-16 7:15
deelll26-Mar-16 7:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.