Behavior-driven development (or BDD) is an agile software development technique that encourages collaboration between developers, QA and non-technical or business participants in a software project.
What is the need for BDD?
BDD focuses on why the code should be created, rather than the technical details, and minimizes translation between the technical language in which the code is written and the domain language spoken by the business, users, stakeholders, project management, etc.
It extends TDD by writing test cases in a natural language that non-programmers can read. Behavior-driven developers use their native language in combination with the ubiquitous language of domain-driven design to describe the purpose and benefit of their code.
The Business application would begin by having stakeholders offer concrete examples of behavior they want to see from the E-commerce application. These examples are gleaned from stake holders and converted in to acceptance criteria.
Features of BDD
- Bridges communication between Stake Holders, Business Analysts, QA Analyst and Development team.
- Easy to Understand.
- Driven by business requirements.
- Framework like Cucumber or specflow works as a link between use cases and technical language.
What is Cucumber?
Cucumber is a testing framework which supports Behavior Driven Development (BDD). It lets us define application behavior in plain meaningful English text using a simple grammar defined by a language called Gherkin.
What is Gherkin?
Different teams will understand the requirements in different ways. So there should be a universal language to describe the requirements. Gherkin is the universal language . It fills the gap between the business owners and other team members.
Gherkin is the language that Cucumber understands. It is a Business Readable, Domain Specific Language that lets you describe software’s behavior without detailing how that behavior is implemented.
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.
Structural Syntax of Gherkin
Feature: Search Functionality in an ecommerce website
This will bring up products related to the search
Scenario: When a customer searches for a product, all the products related to the input given should be displayed
Given Customer is on the main page of www.shopping.com
When Customer searches for Mobile Phones
Then search page should display the list of mobile phones.
Given,When and Then are the keywords in Gherkin
Steps
Step 1: Install Cucumber plugin for Eclipse
Step 2: Download Cucumber jar files from https://search.maven.org
- cucumber-core
- cucumber-java
- cucumber-junit
- cucumber-jvm-deps
- cucumber-reporting
- gherkin
- junit
- mockito-all
- cobertura
Step 3: Create new project called Shopping and add a new package called Sample
Shopping->src->Sample
Step 4: Create a another package called StepDefinition
Step 5: Create a new folder called Samplefeature
Step 6: Then create a class file in the Sample package as Sample_Test. Add your selenium script in Sample_Test class file.
Step 7: Run the class file as Java application.
Step 8: Create a feature file called sampletest under folder samplefeature which has your test scenarios in plain English language.You can add one or n number of scenarios according to your need. File should have extension as . feature.
Step 9: Create a class sampleRunner under sample package. This class just needs annotations to understand that cucumber features would be run through it and you can specify feature files to be picked up plus the steps package location.
Sample Code
Run this class as junit application and the console window will have message displayed according to your test scenarios given in feature file.
Sample message
Step 10: In stepDefinition package, create a class file called SampleTest_steps and paste the message displayed in the console window when running sampleRunner class.
Step 11: Now in the class file SampleTest_steps add the selenium code in the required area from the Sample_test class.
Step 12: Now run the sampleRunner class as junit application.
Cucumber will start executing the feature file steps. When Cucumber is at first step i.e. @Given statement, it search for the statement in the stepDefinition . When it finds the statement, it runs the code inside the function.
As we have seen, with the help of Gherkin language cucumber helps facilitate the discovery and use of a ubiquitous language within the team.
Tests written in cucumber directly interact with the development code, but the tests are written in a language that is quite easy to understand by the business stakeholders. Cucumber test removes many misunderstandings long before they create any ambiguities in to the code.
References
https://pythonhosted.org/behave/philosophy.html
https://github.com/cucumber/cucumber/wiki/Gherkin
http://toolsqa.com/cucumber/behavior-driven-development/
Image Reference
http://agilecoach.typepad.com/.a/6a00e54ee21bf28834016302ae39ee970d-pi