SpecFlow – Basics

Before we talk about Spec Flow, we should know Cucumber

 

  • Testing Frame work
  • Support BDD
  • Uses Gherkins
  • Cucumber is written in Ruby but test code can be written in c#, Java , Python etc.

What is Spec Flow?

So before going further a brief idea about BDD and Gherkins

BDD

  • Can be considered as a refinement of TDD (Test Driven Development) and ATDD (Acceptance Test Driven Development). [Definition not available in the scope of this study]
  • Thinking “from the outside in”. Should Test Behavior Not test implementation.
  • Apply 5 Why’s
    • Used in Toyota Company (Source Wiki)
      • Problem – The vehicle will not start
        • Why? – The battery is dead. (first why)
        • Why? – The alternator is not functioning. (second why)
        • Why? – The alternator belt has broken. (third why)
        • Why? – The alternator belt was well beyond its useful service life and not replaced. (fourth why)
        • Why? – The vehicle was not maintained according to the recommended service schedule. (fifth why, a root cause)
    • Why we are using 5Why’s ???? To get the root cause
  • is understandable by different stake holders (BA, QA, Devops) to improve communication
  • driven by Business Value

Why BDD Framework ?

Hopefully through this pic we all understand the problems in software development / testing.
The big hurdle for SW development / testing is Communication
We can overcome such problems through Gherkins

Gherkins

  • Language that Cucumber / Spec Flow understand
  • Ubiquitous  language – means normal language Eg: English
  • Directly interact with the development code but tests are written in a language that is quite easy to understand by the business stakeholders. [a practical example will give you more understanding of this]
  • Removes any misunderstandings / confusion in Communication.
  • Not necessarily used to write automated tests.
  • Primarily used to write structured tests which can be used as project documentation.
  • The property of being structured gives us the ability to automate them 🙂
  • This automation is done by Spec Flow / Cucumber

Example of Gherkins / Cucumber / Spec Flow / BDD

Feature : Create A New Facebook Account
Scenario : Successful sign up
Once successfully signed in, a confirmation E-mail and a  personal greetings is sent
Given I have chosen to sign up
When I entered the details such as Name, Email, Phone Number, DOB and Gender
Then I should receive a confirmation E-mail
And I should see personal greeting mail
Scenario : Duplicate Email
To create an account with an existing E-mail in FB
Given I have chosen to sign up
When I entered the details such as Name, Email, Phone Number, DOB and Gender
Then an error message is shown that E-mail already registered
And I should get the option to claim the E-mail address.
When we look at the above example – we can see that any business users can understand what the test is going to do.
  • Stake holders can visualize what the test is intended to do
  • Can give feedback [Just visualize – how the test we write in POM / Facade can be read by Stake holders. It’s very difficult for person with out programming knowledge]
A simple Practical Example in Git Hub.
Feature File is like body of your car, engine is different.

Key Words

FeatureDefines what feature you will be testing in the tests below

 

 

    • Each Gherkin file begins with a Feature keyword.
    • Feature defines the logical test functionality
    • e.g if you are testing a payment gateway your Feature will become Payment Gateway 
    • If you are testing the LogIn functionality then the Feature will become Login
    • The idea of having a feature file is to put down a summary of what you will be testing.
    • serve as the documentation for your tests
    •  good point to start for a new team member

 

Backgroundused to define steps which are common to all the tests in the feature file
  • Eg: Think of Login, which is common to all tests. 
ScenarioEach Feature will contain some number of tests to test the feature. Each test is called a Scenario
  • Eg: add product to basket

  • A scenario is equivalent to a test in our regular development process. Each scenario/test can be basically broken down into three parts:
    • Precondition to the test, which represent with (Given) keyword
    • Test step execution, which represent with (When) keyword
    • Verification of the output with expected result, which represent with (Then)
GivenTells the pre-condition of the test. For example in an E-commerce website, if you want to add a product to the basket, then the pre-condition is that you should be on the Products Page
  • Eg: user is on the Home Page / product page
When: Defines the action of the test
  • Eg: User add the first product to basket. 
ThenStates the post condition. You can say that it is expected result of the test
  • Eg: Then the product should be added. 
AndDefines additional conditions of the test
Eg: Given User is on Home Page
     When User Navigate to LogIn Page
     And User enters UserName and Password
     Then Message displayed Login Successfully
Butused to add negative type comments
Eg: Given User is on Home Page

 When User Navigate to LogIn Page
     And User enters UserName and Password
     But The user credentials are wrong
     Then Message displayed Wrong UserName & Password

 

* keyword – 

  • whole purpose of having Given, When, Then and all the other keywords
  • Cucumber doesn’t care about what Keyword you use
  •  it cares about what code it needs to execute for each step [Step Definition] 
  • all the keywords can be replaced by the * keyword 
Eg: Scenario: Successful Login with Valid Credentials

     * User is on Home Page
     * User Navigate to LogIn Page
     * User enters UserName and Password
     * Message displayed Login Successfully

 

I had created a Spec Flow code for Google search in Git Hub. https://github.com/fasteningcode/SpecFlow_googleSearch/