What is a Cucumber?
While most people think of a vegetable first, we QA Engineers have a different tendency in mind. Cucumber is a Behaviour-Driven Development (BDD) tool that supports writing human-readable tests using a domain-specific language. It provides a platform that bridges the gap between stakeholders and technical teams.
In BDD, business analysts and/or product owners write Cucumber scenarios that describe the behavior of the system from the customer’s perspective. Product owners then review and sign off these scenarios before developers start writing their code.
The Cucumber platform uses a native Selenium WebDriver method as a framework for automating tests on web applications. It was originally built using the Ruby programming language. However, because of its popularity, it has also been implemented with other programming languages in mind.
Traditional testing approaches are typically code-based, making them more challenging to maintain and introduce change within them. On the other hand, Cucumber tests are designed to be flexible, making them easier to maintain over time. They are a reliable choice as they are less likely to break when new code changes are introduced.
The Cucumber testing platform is structured around feature files and step definitions, which utilize specific keywords to give tests structure and meaning. This structure provides clarity and purpose to the steps executed within Cucumber scenarios. Cucumber scenarios are written using the Gherkin syntax and stored in .feature files. As you will soon learn in this article, the test code behind Gherkin is written in the programming language specific to the project.
Step definitions are an essential part of the process that connects Gherkin with the code and elements within the application. These definitions are the glue that helps bring various parts of the testing process together. They define the actions that will be taken by the application in response to the user’s input. Overall, step definitions are a critical component of the testing process and play a crucial role in ensuring the quality and reliability of the application.
How to install Cucumber
To install the Cucumber BDD tool, you will need to add the 'cucumber-rails' gem to your Ruby on Rails project's Gemfile. Then, run bundle install to install the gem and its dependencies. An example of a Gemfile is given below:
After you install the gem, you can run the following command to generate the necessary files and directories for Cucumber:
This command creates a features directory in your project root and generates a cucumber.yml file. You can now create your first Cucumber feature file in the features directory and run it with the cucumber command.
How to use the Cucumber toolset - Write down your first Cucumber test scenario
Here we’ll provide an example of writing and executing your first Cucumber test, also known as a Cuke test, using a native Devōt web scenario.
To get started, create a file with a .feature extension. This file should contain a description of the feature you are testing, as well as a description of each additional example you wish to test. For example, if you want to test your homepage, the path and filename of the feature file would be:
your_rails_project/features/homepage/homepage.feature
To proceed, the scenarios must be written in Gherkin syntax using the clear and concise Given, When, Then, and And keywords at the beginning of each line. With this approach, the scenarios will be easy to understand and follow for everyone involved.
A simplified example of when to use each of the main keywords looks like the following:
An example of our Devōt web scenario is given below:
How to use the Cucumber toolset - Write out the step definitions
Since Devōt primarily uses Ruby on Rails for development, our step definition files are always regular Ruby files. To follow up on the scenario written in Gherkin above, create a file with .rb extension that will be used as a place for writing down all our step definitions.
A step definition is a block with an expression that links it to one or more Gherkin steps with the same name. When the Cucumber toolset reads a step in a scenario, it looks for a matching step definition to execute.
Basic step definitions are written in Capybara, a library written in the Ruby programming language that allows for the simulation of how a user interacts with your application.
The Devōt web scenario .feature file:
The matching step_definition.rb file:
Finally, after all the setup, it is time to run the tests using the Cucumber command-line tool. For that, simply type in:
With Rake: rake cucumber
Without Rake: [bundle exec] cucumber
Watch the magic unfold before your eyes while your first cucumber test is running and hopefully turns green by the end of it (green indicating that every step, among with its definition, has been executed successfully).
Best Cucumber practices
-
A feature is the product functionality, and the scenario is the specification.
-
Avoid details that do not contribute much.
-
Write clear and concise scenarios (so that tests are easy to understand and maintain).
-
Use tags - this helps with keeping the scenarios well organized and allows you to run only specific scenarios using additional tooling.
-
Use the background statement - avoid repeating steps.
-
Make sure that all .feature files are located in the correct directory.
-
Re-use step definitions; do not write new ones for the same purpose.
-
Use the data table and scenario outline features.
-
Map scenarios with requirements.
-
Write steps in a declarative way.
Why use Cucumber?
One of the common misconceptions is that you do not need automated regression test coverage if you already have unit tests (a method that tests individual units of source code to determine whether they are fit for use). However, regression tests are a great addition to unit tests, and they are usually run daily or weekly. At Devōt, we recommend running them before each production release.
We prefer the Cucumber automation regression suite because it provides better collaboration. Also, it bridges the gap with non-tech-savvy people involved, as they usually need help understanding the details of the unit test but do understand an example written with Gherkin syntax.
Cucumber is an easy tool to use, and it’s a mighty powerful one. It has good documentation and integrates great with tools like Jira and Slack. All of this combined makes it the ultimate tool for regression testing here at Devōt.