Cucumber Tutorial for Beginners

Cucumber is a software testing tool that is used to do acceptance testing throughout the development stage of software. Behaviour-Driven Development is a strategy for testing a software product during the development stage. This article will teach you how to use the cucumber testing tool. This tool is used to do user acceptability testing throughout the software development lifecycle.

Rating: 5
  
 
1782

In this tutorial, you will learn about the cucumber testing tool. This tool is used for conducting user acceptance testing on every progression of software development. Due to its user-friendly interface, even a non-technical person can use this tool to test the software product.

In This Cucumber Tutorial, You Will Learn

What is cucumber in testing?

Cucumber is a software testing tool used for performing acceptance testing while the software is in the development stage. The technique of testing a software product when it is in the development stage is known as Behaviour-Driven Development.

Behaviour Driven Development (BDD) Framework

Behaviour Driven Development also known as BDD Framework is introduced to minimize the errors in the software testing process. This approach tells us to test every step in the development stage.

Behaviour Driven Development allows us to create test scripts that are useful to determine whether the software Product is Successful or not. The test scripts are generated from both end-users and developers' point-of-view. The test scripts and the code are developed concurrently.

BDD Example

If we are creating a registration form, then the following test cases can be created, they are:

  • The user can submit the registration form after filling the critical fields.
  • The user cannot submit the registration form without filling in the required fields.

When the code is ready, the tester will see whether the code passes these tests or not. The code is deployed only after executing the test cases successfully.

So, Cucumber is the tool that supports the Behaviour-Driven Development Framework.

Cucumber is a testing framework that reads the code written in simple English language and executes the test scripts.

MindMajix Youtube Channel

What is Gherkin?

Cucumber executes the test scripts which are defined in a feature file. The language which is used for defining the test scripts is known as Gherkin. Gherkin assists the Cucumber in interpreting and executing the test scripts.

We know that BDD integrates different views while developing test scripts. To integrate different views, we require people from different groups, like developers, product owners, testers, project managers. As these people are not part of the same group, there is a possibility that they can use different terminology while developing the test scripts, 

so, to avoid these problems, a common language called “Gherkin” is introduced. Gherkin gives a standard collection of Keywords, which is used by people from different communities, and still, they will develop the same test scripts.

After learning about the Gherkin Language, now let us see some important terms present in it.

Cucumber Features

A Feature is defined as an Independent unit of a product or a project. For example, let us take Gmail, and we will see the features of this website.

  • Creating and removing the mail accounts of the users.
  • Users login feature for Gmail.
  • Mail sending and receiving feature.
  • File sharing feature through google drive.
  • Users logout feature.

We can observe that the features mentioned above are independent of each other. In Cucumber, before developing the test scripts, first, we have to identify the features to be tested. 

A feature includes the test cases under test for that feature. The feature file is used to save the feature, explanation of the feature and test cases of the feature under test.

It is suggested to write the description of the feature below the feature title so that it can be used for future purposes.

For every feature file, the extension is ‘.feature’.

Feature Files of Gmail Website

  • For the Account Creation Feature, we will use the “CreateAccount.feature”.
  • For the Account Removal Feature, we will use the “RemoveAccount.feature”.
  • For Email Sending Feature we will use the “send an email. feature”.
  • For Email Receiving Feature we will use the “receive email. feature”.

The important keywords included in the Feature File are as follows:

  • Feature: This keyword is used to indicate the feature under test.
  • Test Case: This keyword is used to indicate the name of the test case.
  • Given: Requirements for test case execution.
  • When: Conditions that are to be satisfied with the successful execution of test cases.
  • Then: What happens When the condition Present in the “WHEN” Clause is Satisfied.

Now let us study the scenario concept. The scenario gives us in-depth knowledge required for testing a particular functionality of a software product. 

Scenario:

The Scenario is one of the essential parts of the Gherkins Structure. Each Scenario starts with the “Scenario” Keyword. Every feature will have a number of scenarios and every Scenario will have a number of steps.

Example

Scenario: Visit the About us page of a company’s website

The user will go to the company’s website.

He will click on the About us link, and he will get complete information about the company on that page.

For example, if we take the Netflix website, and we have to test the login functionality or feature of that website. But the problem here is, we have to see whether the login function is working for all types of subscribers. To do that we have to test the login functionality multiple times. For testing login functionality multiple times, we have to copy-paste the same steps multiple times, which is not a suitable method.

For achieving this, Gherkin introduced another structure called “Scenario outline”. The Scenario outline allows us to execute the same test scenario multiple times but with different input values. So, the login functionality is tested repeatedly by giving different inputs for “username” and “Password”. The cucumber executes the test repeatedly by changing the values of the input variables.

To handle multiple scenarios, another concept called “Tags” is introduced

Tags

In real-time, we may have several scenarios in a single feature. To handle such situations, Cucumber introduced the “Tags” concept. In the feature file, each Scenario will have a tag based on the purpose of the Scenario. So, we will select the Scenario based on the tag, and we will tell the Cucumber to test that Scenario.

While testing multiple scenarios, we will face a problem called code redundancy. To overcome that problem, cucumber adopted the “Hooks” practice.

Hooks

Hook enables us to handle the code workflow and assists us in reducing the code sacking. When we are executing multiple scenarios, we will set up and cleanup webdriver for every statement. To make the web driver set up and clean up only once, we will use hooks.

To expand its reach, cucumber introduced the Junit framework.

Junit

Junit is a freeware testing framework used for testing the java code. Junit is essential in test-driven development, and it is a part of the unit testing framework. JUnit allows any non-technical person to execute the test cases smoothly. The execution flow in Junit will be as follows:

  • Stakeholders will develop the feature file.
  • According to the feature file, the step definition file is created.
  • Junit runner class is specified for implementing the collection of test cases.
  • Junit will integrate the test case result.

For software testing, Reporting and Documentation are essential.

Report

Cucumber does not have any separate format for test reports. To get the format, we have to integrate the Cucumber with other freeware tools like Junit/Ant. We will take Junit because it supports java programs.

Pretty Format

The pretty format creates the test reports in HTML format. The HTML format reports are more readable. After preparing the test reports, you have to store them in the following locations:

  • Local Directory: We can save the test report in the directory in which we are executing the test scenarios.
  • Server Directory: We can store the test report in any directory which is accessible by everyone. 

To reduce the flaws in the testing process, cucumber does debugging of test scripts 

Debugging

In Cucumber, sometimes tests can fail due to unidentified reasons. The process of identifying the reasons for test failure is known as debugging. Debugging is done by installing Eclipse.

Advantages of Cucumber Framework

  • It is an open-source testing tool.
  • Plugins present in Cucumber work faster.
  • Both developers and testers develop test Scripts with a mutual understanding.
  • Cucumber Tool Supports programming Languages like Java, Ruby, Groovy, etc.
  • It Supports Continuous Integration.

Conclusion

Cucumber is a freeware(open-source) testing tool used to conduct acceptance testing of the software code. It supports Behaviour-Driven Development to eradicate the failures in the software testing process. The cucumber tool can be used by a non-technical person to perform software testing. The Gherkin language used in the cucumber tool helps us accelerate the formation of test scenarios. 

Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
Ruby Cucumber Training Apr 27 to May 12View Details
Ruby Cucumber Training Apr 30 to May 15View Details
Ruby Cucumber Training May 04 to May 19View Details
Ruby Cucumber Training May 07 to May 22View Details
Last updated: 08 Jan 2024
About Author

Ravindra Savaram is a Technical Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.

read more
Recommended Courses

1 / 15