What is Acceptance Test Driven Development?

ATDD is development methodology which promote good collaboration between business and technology group. There are many similar methodologies which works more or less same way like Behavior Driven Development (BDD), Example Driven Development (EDD) and Specification by Examples etc.

ATDD is extended on TDD that emphasis on developers, testers and business collaboration. ATDD encompasses acceptance testing, but highlights writing acceptance tests before writing code. Acceptance Test Driven Development (ATDD) is a test first approach.

Why Acceptance Test Driven Development?

One of the main purpose of ATDD is to remove ambiguity from requirement by writing examples through collaboration of 3 amigos. Why writing examples by collaboration of 3 amigos? Because these amigos usually think differently about same requirement and in order to align their thought better to write examples.

3-amigos-for-atdd

Why Gherkin to write examples?

Gherkin is the language that many tools understands like Cucumber, SpecFlow and Behave etc. It is a Business Readable, Domain Specific Language that lets you describe software’s behavior without detailing how that behavior is implemented. You can read more about Gherkin here.

Gherkin serves two purposes — documentation and automated tests. The third is a bonus feature — when it yells in red it’s talking to you, telling you what code you should write.

There are 10 main keywords that helps in writing features and acceptance tests/scenarios/examples.

  1. Feature – To describe feature name and functionality
  2. Scenario – To describe scenarios of feature
  3. Given – Pre-condition for a test/examples
  4. When – Action to be performed for test/examples
  5. Then – Expected outcome for a test/examples
  6. And – Use to write multiples given/when/then
  7. But – Similar to “And” to describe examples/test
  8. Background – To write repeated steps for scenario
  9. Scenario Outline – Used with “Examples” to write multiple scenarios together by having placeholders
  10. Examples – Multiples scenarios for scenario outline

Writing feature files for an application

Let’s assume we wanted to develop a Carpool application to share our rides with other people within organization in order to reduce traffic and save money.

We need following features in our application.

carpool-goalAbove features looks big we need to further split those to have smaller items for faster delivery and feedback. There are various ways to split big features into smaller releasable functionalities like splitting based on WORKFLOW, OPERATION, BUSINESS RULES, DATA TYPES and SCENARIOS etc. I will not be covering those here but will take one examples to explain ATDD here.

Let’s assume, we need below functionalities to complete “Share Ride” feature.

share-ride-splittedBut still it looks big but there are possibilities to split those further like below for “Publish New Ride

  1. Quick Post – Posting new ride with only with minimum set of requirement
  2. Detailed Post – Having detailed like car type, car number, route map etc.
  3. Re-Post – Wanted to post same what I have done yesterday
  4. Recurring Post – Wanted to post same ride for next 5 days
  5. Auto-Post – Automatically picking up my location

Let’s take 1st functionality as an example to write our 1st feature.

Feature: Quick Post

In order to share my car

As a car owner

I want to publish it on intranet with details like source, destination, time, email, phone no

Scenario: Employee enter valid ride details to share

Given Employee “Naveen” wanted to submit source, destination and time as “Kammanahalli”, “Hebbal” and “5.30pm” And “Naveen” email is “naveenhome@gmail.com” and phone no is “+91 9810547500”

When “Naveen” click on “Share”

Then Ride get added and “Naveen” get a message “Ride is shared.” with “OK” button

And “Naveen” get redirected to home page on clicking on “OK” button

And “Naveen” can see his ride on top on “Home” page

Scenario: Employee enter ride details to share without email id

Given Employee “Naveen” wanted to submit source, destination and time as “Kammanahalli”, “Hebbal” and “5.30pm”

And “Naveen” email is ” ” and phone no is “+91 9810547500”

When “Naveen” click on “Share”

Then “Naveen” get a message “Please add valid email Id”

And Ride not get saved

Practicing ATDD using SpecFlow, Nunit and C#

I will be using Visual Studio 2015 with C# and will use class library project to start with. Will demonstrate how to practice while creating MVC application with Selenium and Coded UI in upcoming blogs.

Step 1 – Install SpecFlow

Open Visual Studio and go to Tools -> Extensions and Updates and search “SpecFlow for Visual Studio 2015”

specflow-download

After installation, restart visual studio and create your project by clicking “New Project”. I am going to name it “Carpool”.

Step 2 – Add SpecFlow and Nunit references.

Add SpecFlow and Nunit reference to your project via “Manage NuGet Packages” like below.

specflow-and-nunit-references

Step 3 – Create feature file

Add first feature file to Project -> Add -> New Items and you will see option to create new feature file like below and name it QuickPost.feature. Remove all default content from feature file and copy above written feature.

quickpostfeature

Step 4 – Generate Step definition

Right click on feature file and you will get option “Generate Step Definitions”. You will get below screen and click “Generate” here. It will ask you to save your step file on default location (same folder where you have feature file). Save it at default location and you will find a file name “QuickPostSteps.cs” in your solution explorer.

quickpoststeps

What is Step Definitions in SpecFlow?

In order to test our scenario, we need to create step definitions that bind the statements in the test scenario to the application code. SpecFlow automatically generate a skeleton for the automation code that we can extend as necessary. Steps are not one to one binding and it uses regular expression. That means if there are similar steps with same statement in another feature file or in another scenario then it will reuse.

Below ATDD lifecycle explains steps involved in practicing ATDD methodology wherein some steps required business involvement and rest can be performed by development team.

atdd-cycle

Step 5 – Run your first scenario

Go to Test -> Window -> Test Explorer and you will find your tests listed in test explorer. If your test not showing that means you need to install an extension called “Nunit 3 Test Adaptor”.

To install it go to Tools -> Extensions and Updates and search “Nunit 3 Test Adaptor”. After installation, restart visual studio and build your solution to see your tests in test explorer.

nunit-test-adaptor

Step 6 – Writing production code to pass our 1st scenario

I will use Nunit for assertion and will demonstrate how to pass scenario by adding minimum code just to demonstrate capability of SpecFlow.

Add a model class called “Ride.cs”

ride-class

 

Add another Business Logic class “RideBLL.cs”

ridebll-class

Make changes in steps file like below: –

steps-file

Step 7 – Run Tests again

Build your solution again, go to test explorer and click on run all. Test will pass successfully like below.

test-pass

Try out yourself. If stuck anywhere then reach out to me. I will be happy to help you.

2 Comments

  1. Nik-Reply
    August 9, 2017 at 7:59 pm

    How can we add another scenario to feature file?
    If I try to generate test scenario file it says “All steps are bound”

    • Naveen Kumar Singh-Reply
      August 16, 2017 at 2:54 pm

      This is because of scenario is having same syntax and only value is changing. SpecFlow uses regular expression so syntax is same then it will reuse steps instead of creating new steps. Try changing syntax then it will have different steps. Also not suggested to change syntax if scenario is same.

Leave A Comment