Mastering Cucumber: A Step-by-Step Guide to Setting up an Gherkin E2E tests

This guide covers the essential steps to set up a Cucumber testing framework for end-to-end (E2E) or unit testing. It begins by showing you how to install Cucumber and create a project directory. Next, you will learn how to initialize the project and create the necessary directory structure. From there, you will be shown how to write your feature files in Gherkin and implement the step definitions in a programming language like JavaScript or Ruby. Finally, you will learn how to run your tests and specify specific feature files or directories to run. By following these steps, you will have a solid foundation for testing your application with Cucumber.

Cucumber is a powerful testing framework that allows you to write tests in a natural language syntax called Gherkin. This makes it easy for non-technical stakeholders to understand and write tests, while also allowing developers to implement the tests in a programming language of their choice.

gherkin best practices 1

BDD advantages and disadvantages

Here are some advantages of using behavior-driven development (BDD) tests:

  1. Improved communication: BDD tests are written in a natural language syntax called Gherkin, which makes them easy for non-technical stakeholders to understand. This can improve communication between developers and non-technical team members and ensure that everyone is on the same page about the desired behavior of the application.
  2. Increased collaboration: BDD tests encourage collaboration between developers, testers, and business analysts, as everyone can contribute to the development of the tests. This can lead to a more holistic approach to testing and a better understanding of the business requirements.
  3. More focused testing: BDD tests are designed to focus on the behavior of the application, rather than the implementation details. This can help ensure that the application is tested from the perspective of the end user and that the most important behaviors are covered.

Here are some disadvantages of using BDD tests:

  1. Requires a learning curve: BDD tests use a new syntax (Gherkin) that may require a learning curve for those unfamiliar with it. This can be a barrier to adoption, especially for teams with limited time or resources.
  2. Can be time-consuming: Writing BDD tests requires more upfront planning and collaboration than traditional testing approaches. This can be time-consuming and may not be practical for all projects.
  3. May not be suitable for all projects: BDD tests are best suited for projects with well-defined business requirements and a clear understanding of the desired behavior of the application. If these conditions are not met, BDD tests may not be the most effective testing approach.
  4. Limited tool support: While there are several tools available for automating BDD tests, the tool landscape is still evolving and may not offer the same level of support as more established testing frameworks.

Cucumber Tutorial

In this tutorial, we will go over how to set up a cucumber testing framework for end-to-end (E2E) or unit testing.

Summary:

  1. Install Cucumber
  2. Create a project directory
  3. Initialize the project
  4. Write your feature files
  5. Write your step definitions
  6. Run your tests

By following these steps, you will have a basic cucumber testing framework set up for E2E or unit testing. You can then continue to add more feature files and step definitions as needed to test different aspects of your application.

  1. Install Cucumber: To use Cucumber, you will need to install it first. You can do this using a package manager like npm (for JavaScript) or gem (for Ruby).
    # npm 
    npm install cucumber 
    
    # gem 
    gem install cucumber
  2. Create a project directory: Create a new directory for your project and navigate to it in your terminal.
  3. Initialize the project: Initialize the project by running the following command:
    cucumber --init
    

    This will create the following directory structure:

    .
    ├── features
    │   ├── step_definitions
    │   └── support
    └── cucumber.yml
    

    The features directory is where you will store your cucumber feature files. The step_definitions directory is where you will store the implementation for your step definitions. The support directory is where you can store any additional support code for your tests. The cucumber.yml file is used to configure cucumber.

  4. Write your feature files: Feature files are written in Gherkin, a natural language syntax for describing software behavior. Each feature file should describe a single feature or behavior of your application.Here is an example feature file for a login feature:
    Feature: Login
      As a user
      I want to be able to log in to the application
      So that I can access my account information
    
      Scenario: Successful login
        Given I am on the login page
        When I enter my username and password
        And I click the login button
        Then I should be taken to the dashboard page
    
  5. Write your step definitions: Step definitions are the implementation for the steps in your feature files. They are written in a programming language like JavaScript or Ruby.Here is an example step definition file for the login feature:
    Given("I am on the login page", () => {
      // Navigate to the login page
    });
    
    When("I enter my username and password", () => {
      // Enter the username and password
    });
    
    When("I click the login button", () => {
      // Click the login button
    });
    
    Then("I should be taken to the dashboard page", () => {
      // Verify that the user is taken to the dashboard page
    });
    
  6. Run your tests: To run your tests, use the following command:
    cucumber
    

    This will run all the feature files in the features directory. You can also specify a specific feature file or directory to run by passing it as an argument:

    cucumber features/login.feature
    

    That’s it! You now have a basic cucumber testing framework set up for E2E or unit testing. You can continue to add more feature files and step definitions as needed to test different aspects of your application.

BDD usage tips and tricks

By following these tips and tricks, you can help avoid problems with your BDD tests and ensure that they are effective at testing the behavior of your application:

  1. Clearly define the behavior of the application: Before writing any BDD tests, it is important to have a clear understanding of the desired behavior of the application. This will help ensure that the tests are focused on the most important behaviors and will reduce the risk of writing tests that are not relevant to the business requirements.
  2. Collaborate with all stakeholders: BDD tests are designed to be written by a cross-functional team, including developers, testers, and business analysts. Make sure to involve all relevant stakeholders in the test-writing process to ensure that the tests accurately reflect the business requirements.
  3. Use a consistent syntax: BDD tests are written in a natural language syntax called Gherkin. Make sure to use a consistent syntax across all tests to ensure that they are easy to read and understand.
  4. Keep the tests focused: BDD tests should focus on a single behavior or feature of the application. Avoid writing tests that cover multiple behaviors or features, as this can make them more difficult to understand and maintain.
  5. Use clear and descriptive names: Use clear and descriptive names for your feature files, scenarios, and step definitions to make them easy to understand and maintain.
  6. Regularly review and update your tests: As the application evolves, it is important to regularly review and update your BDD tests to ensure that they are still relevant and accurately reflect the desired behavior of the application.

Good BDD example

Some of the key characteristics of the good examples include:

  1. Focus on a single behavior or feature: Each scenario focuses on a single behavior or feature of the application, which makes the test easier to understand and maintain.
  2. Clear steps and verification points: The scenarios include clear steps and verification points to ensure that the behavior of the application is being tested effectively.
  3. Use of descriptive and clear names: The feature files, scenarios, and step definitions have descriptive and clear names, which makes the test easy to understand and maintain.

Let’s take a look to a good gherkin test case. The next test covers three different behaviors of the shopping cart feature: adding an item to the cart, removing an item from the cart, and emptying the cart. It also includes multiple steps and verification points for each behavior, which makes the test more comprehensive.

Feature: Shopping cart
  As a shopper
  I want to be able to add and remove items from my shopping cart
  So that I can manage the items I want to purchase

Scenario: Add an item to the shopping cart
  Given I am on the product page for a shirt
  When I click the "add to cart" button
  And I select the size and quantity of the shirt
  And I click the "add to cart" button again
  Then I should see a notification that says "Shirt added to cart"
  And the shopping cart icon should show the correct number of items

Scenario: Remove an item from the shopping cart
  Given I am on the shopping cart page
  And there is at least one item in the cart
  When I click the "remove" button for an item
  Then I should see a notification that says "Item removed from cart"
  And the shopping cart icon should show the updated number of items
  And the item should no longer be listed in the shopping cart

Scenario: Empty the shopping cart
  Given I am on the shopping cart page
  And there are multiple items in the cart
  When I click the "empty cart" button
  Then I should see a notification that says "Shopping cart emptied"
  And the shopping cart icon should show 0 items
  And the shopping cart should be empty

BDD problems and bad examples

In this exercise, you will see examples of bad BDD tests for a shopping cart feature. These examples illustrate common mistakes that can occur when writing BDD tests and can help you understand what to avoid when writing your own tests.

Some of the problems with the bad examples include:

  1. Lack of focus: The scenarios are too general and do not focus on a single behavior or feature of the application.
  2. Lack of steps and verification points: The scenarios do not include enough steps and verification points to test the behavior of the application.
  3. Combining multiple behaviors into a single scenario: This can make the test more difficult to understand and maintain.

By comparing the bad examples to good examples of BDD tests, you can see the importance of writing clear, focused, and comprehensive tests that accurately reflect the desired behavior of the application.

The next test is bad because it combines multiple behaviors into a single scenario (“Add and remove items from the shopping cart”). This makes it difficult to understand what is being tested and makes the test more prone to errors. Additionally, the test does not include any verification points to ensure that the shopping cart is being updated correctly.

Feature: Shopping cart
  As a shopper
  I want to be able to use my shopping cart

Scenario: Add and remove items from the shopping cart
  Given I am on the shopping cart page
  When I click the "add to cart" button for an item
  And I click the "remove" button for the same item
  Then I should see a notification that says "Item added to cart"
  And I should see a notification that says "Item removed from cart"

Scenario: Checkout
  Given I am on the shopping cart page
  When I click the "checkout" button
  Then I should be taken to the checkout page

Scenario: Empty the shopping cart
  Given I am on the shopping cart page
  When I click the "empty cart" button
  Then I should see a notification that says "Shopping cart emptied"

The next test is bad because it does not clearly define the behavior of the shopping cart feature. The scenarios are too general and do not specify the steps or verification points necessary to test the feature. Additionally, the test only covers a limited number of behaviors, which means that the shopping cart feature is not being tested comprehensively.

Feature: Shopping cart
  As a shopper
  I want to be able to use my shopping cart

Scenario: Add an item to the shopping cart
  Given I am on the product page
  When I click the "add to cart" button
  Then I should see a notification

Scenario: Remove an item from the shopping cart
  Given I am on the shopping cart page
  When I click the "remove" button for an item
  Then I should see a notification

Scenario: Checkout
  Given I am on the shopping cart page
  When I click the "checkout" button
  Then I should be taken to the checkout page

Summary

BDD is a powerful tool for improving communication and collaboration within a development team, as it allows non-technical stakeholders to contribute to the testing process. In this exercise, we have covered the basics of BDD and provided examples of good and bad BDD tests. By following best practices and learning about advanced features and tools, you can continue to improve skills in the BDD approach.

What to learn or to do next? That is your learning curve:

  1. Learn about advanced Gherkin syntax: Gherkin is the natural language syntax used to write BDD tests. There are advanced features of Gherkin, such as scenario outlines and data tables, that can help you write more efficient and flexible tests.
  2. Explore different BDD tools: There are several tools available for automating BDD tests, such as Cucumber, Behat, and SpecFlow. Explore different tools and choose the one that best fits your needs and workflow.
  3. Understand the role of BDD in the development process: BDD is not just a testing approach, but a philosophy that can be applied throughout the development process. Understand how BDD fits into your development workflow and how it can be used to improve communication and collaboration within your team.
  4. Learn about other testing approaches: In addition to BDD, there are other testing approaches that can be useful, such as test-driven development (TDD) and acceptance test-driven development (ATDD). Understanding the strengths and weaknesses of different testing approaches can help you choose the right approach for your project.
  5. Keep up to date with industry trends: The field of software testing is constantly evolving. Stay up to date with industry trends and best practices by reading blogs and attending conferences and workshops. This will help you stay current and improve your skills as a senior-level engineer.
📋 R5X Faster with Testomat.io
Unlock your single source of test management for automated tests
Follow us