Getting Started with Behavioral Driven Development(BDD) and Cucumber - All that you need to know!

Getting Started with Behavioral Driven Development(BDD) and Cucumber - All that you need to know!


Past few years have seen an increase in adoption of Agile and DevOps practices across the organizations for software development to deliver quality products at a high speed. These trends have brought in many new practices in software development life cycle which were not part of traditional SDLC models- all of which aims to accelerate the process of delivery with respect to time to deliver low cost products without compromising on quality. Behavior Driven Development (BDD) is one such practice which has gained popularity in the agile and DevOps world owing to its effectiveness in delivering quality software. In this article, we will be discussing what BDD is all about and the very famous tool which supports BDD- Cucumber.

What is Behavior Driven Development all about and why is it needed?

When you are moving to agile/DevOps models for delivering software, there is a time restriction and you are required to ship your end products in a very short span of time. In such cases, you cannot afford to waste time on reworking on the code for issues discovered late in the development process. In a traditional model, testing usually happened only after developing the complete code and the issues will be discovered only at this point of time. So, to avoid this, it demands you to have a shift left approach for testing. That is testing has to be done earlier in the development lifecycle.
This need led to the introduction of a software development technique called Test Driven Development (TDD) where automated tests are written before writing the code and developers use those tests to drive the development process. So, developers write a test and then write the code to pass the test. The advantage of this technique is that issues can be caught at the time of writing code itself rather than discovering them later during the testing phase.

TDD image
Behavior Driven Development is an extension of TDD and has the same principles and flavor except that here the tests are written based on the behavior of the application to be developed and uses a natural language to describe those tests. The tests being written in English-ish language enables all the stakeholders to understand the tests easily and clearly even if they have no technical background. So BDD is designed to bring in more collaboration between the various stakeholders in the development process- the Product owner, Business analysts, Developers and Testers.
The BDD development process starts with the various stake holders (Product owner, BA, Dev and QA) eliciting the requirements together and the developer or QA writes the acceptance test scenarios for the same. These acceptance test scenarios are used by the developers for driving the development process and the same test scenarios can be used by testers as the basis for their tests. This entire process makes sure that the entire team has better understanding of the requirement and helps in minimizing the rework caused by misunderstood or vague requirements.

What is Cucumber?

Cucumber is a tool that supports Behavior Driven Development and enables you to automate your acceptance tests which can be written in a readable and understandable format like plain English. One important thing you need to note about BDD is that it is not a testing technique, but a development technique which is driven by the tests. It is the process of approaching your development process thinking about the final outcome before you code. In this approach you need to write tests before you code. Many people use cucumber to write tests after code is written. This is not really BDD, because the tests do not drive the implementation when they are written later. This kind of practice tends to bring down the purpose of the tool. I happened to read a blog from the creator of Cucumber 'The world's most misunderstood collaboration tool', which explains clearly the purpose of the tool and the idea behind developing the tool. In his opinion, it is a collaboration tool which helps to bring the product owner, developer and tester on the same page. It is a good read if you have been considering cucumber just as a testing tool.
Cucumber reads the acceptance tests as executable specifications written in plain text and validates that the software conforms to the specifications. It also generates a report indicating success or failure of each scenario. So, now let’s have a look at how Cucumber works.

How Cucumber Works?

Cucumber reads your specifications from plain language text files called features, understands the scenarios, and runs the scenarios to validate your specification. Feature files can be considered to be the entry point to your Cucumber tests where you describe your tests. Feature files are houses your tests and are saved with the extension ‘.feature’. Features are made of scenarios which is equivalent to a user story or a test case. Scenarios consist of Steps which are the tests steps for a given scenario or test case. A feature file may have one or more scenarios. Basically, a feature file describe a feature of the software which will have a group of related scenarios and each scenario will have number Steps for Cucumber to work through. Features, Scenarios and Steps are written in the plain English like language called Gherkin which follows a set of rules and describes the test in a specific format (Given When Then statements) Given below is a simple example of a feature file.
Feature: Login Action Scenario: Successful login with valid credentials Given User is on Home Page When User navigate to Login Page And User enters UserName and Password Then display message Successfully Logged In
The feature file is called the business facing component of the cucumber stack. As you can see in the example above, it uses a readable language anyone can understand. So, it serves as a document that BAs, QAs and developers use to define the requirements and drive the development/testing. Now, in order for the business component to work, you need some technical code that executes the tests in a browser or a mobile device or an emulator. That is where Step Definitions come in which is the technology part of Cucumber stack.
Step definitions map the readable language of each step into code that can be executed to carry out the action described by the step. So, whenever Cucumber executes a step in a scenario, it will look for a matching Step definition to execute. The step definition code will be in any language that cucumber supports like Java, JS or Ruby. Normally, step definitions will involve using an automation library or framework to automate the steps in a browser or mobile application. Cucumber can be integrated with automation libraries like Selenium, Capybara, Watir, etc. Given below is a Step definition for the feature file example we used before. The code is written in Java using as the Selenium WebDriver as the automation library.

public class Test_Steps {
public static WebDriver driver;
@Given("^User is on Home Page$")
public void user_is_on_Home_Page() throws Throwable {
          driver = new FirefoxDriver();
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
           driver.get("http://www.store.demoqa.com");
	}

@When("^User Navigate to LogIn Page$")
public void user_Navigate_to_LogIn_Page() throws Throwable {
	driver.findElement(By.xpath(".//*[@id='account']/a")).click();
	}

       @When("^User enters UserName and Password$")
        public void user_enters_UserName_and_Password() throws Throwable {
                 driver.findElement(By.id("log")).sendKeys("testuser_1"); 	 
        driver.findElement(By.id("pwd")).sendKeys("Test@123");
        driver.findElement(By.id("login")).click();
	}
           @Then("^Message displayed Login 
           Successfully$")
            public void 
           message_displayed_Login_Successfully() throws 
            Throwable {
          System.out.println("Successfully Logged in");
}

So, to summarize on how Cucumber works – you need two files feature file (which has the business readable tests) and step definition file (technical code) to execute cucumber test and the mapping between these two files is the key to execution.

Why Cucumber?

Cucumber is a great tool which aims in improving the communication between the stake holders including business owners as it allows you to write acceptance tests in a natural language. The fact that Cucumber supports popular automation frameworks like Selenium, Appium, Calabash, Watir etc makes it a wonderful choice for teams who want to implement BDD. And as you know, the BDD approach is highly recommended for projects using agile methodology which is considered to be one of the efficient delivery models currently.
The article was just a high level description of what is BDD and how Cucumber works. If you want to dig in more and start learning BDD and Cucumber, there are many awesome tutorials and courses available online. Here is a list of links to get started with Cucumber!
Tutorials:

Online Courses

Hope the article helps you get started with BDD and Cucumber!



How much is a great User Experience worth to you?


Browsee helps you understand your user's behaviour on your site. It's the next best thing to talking to them.

Browsee Product