Today, Novatesting would like to bring some knowledge of an interesting testing framework named Cucumber.
What is Cucumber?
Cucumber is a tool for behavior-driven development (BDD) that allows developers to write tests in a human-readable format. It was originally written in Ruby, but now supports a variety of programming languages including Java, C#, and Python. Cucumber is commonly used in automated testing to validate the behavior of an application.
The basic structure of a Cucumber test involves writing scenarios in plain text using the Gherkin syntax. Gherkin is a plain language format that is used to describe the behavior of an application. Each scenario consists of a series of steps, each of which is written in the form of “Given,” “When,” “Then,” etc. The steps are then matched with code written in a programming language, which defines the behavior of the application.
Why Cucumber?
Cucumber tests are highly readable and easy to maintain. This makes them an ideal choice for teams that want to involve stakeholders in the testing process, as the tests can be written in a way that is easy for everyone to understand. Cucumber also allows for tests to be written in a variety of languages, making it possible for developers who speak different languages to work together on the same project.
One of the benefits of using Cucumber in automated testing is that it encourages collaboration between developers, testers, and stakeholders. The ability to write tests in a human-readable format makes it possible for everyone to understand what is being tested and how the application is expected to behave. This helps to ensure that everyone is on the same page and that the final product meets the expectations of all stakeholders.
Cucumber also makes it easy to reuse steps in multiple scenarios. This can help to reduce the amount of duplicated code and make it easier to maintain the tests over time. By breaking down the steps into smaller, reusable pieces, Cucumber can also make it easier to write tests for complex applications.
Another benefit of using Cucumber in automated testing is that it provides a high level of abstraction. This abstraction makes it possible to write tests that are focused on the behavior of the application, rather than the implementation details. This can help to make the tests more robust and less prone to break when the implementation of the application changes.
Cucumber can be integrated with a variety of testing frameworks and tools. This allows developers to choose the tools that work best for their projects and to leverage the strengths of those tools. For example, Cucumber can be integrated with Selenium WebDriver to automate the testing of web applications. This integration makes it possible to use Cucumber to write tests that interact with a web application, while leveraging the power of Selenium to perform the actual tests.
Behavior Driven Development (BDD)
In BDD, users (business analysts, product owners) will write scripts (scenarios) or acceptance tests that describe the behavior of the system from the point of view. customers before and during the development phase. Cucumber and BDD solve a very common limitation in software projects: each person understands the system differently .
In BDD, users (business analysts and product owners) write scenarios or acceptance tests that describe the system’s behavior from the customer’s perspective, before and during the development phase. Cucumber and BDD resolve a common challenge in software projects: each person understands the system differently.
BDD has the ability to create test scenarios based on both the developer’s and the customer’s perspective. From the outset, project members will discuss to create scenarios, then implement based on those scenarios. All test scenarios are close to the English language, thus playing a role of documentation.
Workflow BDD
After the test script runs, Cucumber will read the Gherkin code from the feature file, then it will find the code in the step definition file that correctly describes the action in the feature file and execute the code, at the run of the code Cucumber can end Compatible with other frameworks like TestNG, Selenium, Spring,…

Comparison between Cucumber and other tools
Cucumber | HP ALM (QTP) | Selenium |
Free of charge | Charged | Free of charge |
Behavior driven development support tool BDD | Functional testing support tool | Functional and Performance testing support tools |
Plugin works fast | Plugin works slower than Cucumber and Selenium | Plugin works slower than Cucumber |
Supports Java, Ruby, Groovy, C#, Python, Javascript | Only VB scripts are supported | Supports Java, .Net, Ruby, Javascript, Python, PHP |
Devs and Testers write scripts | Only Testers write test scripts | Devs and Testers write scripts |
Support Web app, Mobile app combined with other Testing Framework | Support Web application, desktop, client server app | Only support web apps |
Components of Cucumber
Cucumber projects always have a subdirectory at the project root named “features”. This is where all the project’s features are stored, plus additional directories and support directories.
1. Feature
Feature can be understood as an independent unit or function of a project. For example, an e-commerce website, some of the features can be identified as:
- Log in with your system account or social network
- Commodity selection
- Pay
- Log out
In Cucumber, each feature can be understood as an independent function of the product. Before writing test scripts, we should pre-define the features that need to be tested for high efficiency. The tests built in Cucumber are called feature files and are of the form feature, each feature to be tested should be placed in a corresponding feature file.
Features in Cucumber include the following components:
Feature : Describe the current test script to be run
Scenario : Describe the execution steps and desired output for a particular test case
Scenario Outline : The Scenario executes multiple sets of data. Data is stored as a structure, separated by the symbol | |
Given : Specify the context for execution
When : Indicate the action to be performed
Then : The desired output of a test
and so on.
2. Step Definitions
Although there is a feature file, Cucumber does not really know what code will be executed for each specific scenario stated in the feature file. It needs an intermediate Step Definition file, which maps the steps, features(Given,When,Then) in the scenario to the functional code to execute. Step is defined in java file “features/step_definitions/*_steps.java”
For example:

3. Scenario
Scenario is Gherkin’s core structure. The test script declares with the keyword “Scenario:” followed by the script name. Each feature can have one or more scenarios, each scenario includes one or more steps.

Suppose the above scenario you need to execute many times with different menus. If you do it in turn, it will be very boring and not quite a good way. Cucumber provides a “Scenario outline” structure to solve this problem. Cucumber replaces different input values and enforces a series of behaviors.
As the example below, Cucumber sequentially fills the first and second runs with the values ”Project” and “Task” respectively into the <menu> variable . It automatically understands to run 2 times with 2 corresponding values.

In conclusion, Cucumber is a powerful tool for automated testing that provides many benefits to developers and stakeholders. By allowing tests to be written in a human-readable format, Cucumber encourages collaboration and helps to ensure that everyone is on the same page. Cucumber also provides a high level of abstraction, making it possible to write tests that are focused on the behavior of the application, rather than the implementation details. With its ability to be integrated with a variety of testing frameworks and tools, Cucumber is a flexible tool that can be used to automate the testing of a wide range of applications.