Implementing BDD Automation Testing with Cucumber in Python.

Prasanth V
4 min readSep 22, 2023

--

In this blog, we will understand the concepts of BDD, understand the power of Cucumber, and discover how to create efficient and maintainable automated tests for Python projects.

By Prasanth V — “Keep calm and automate on — the mantra of a proficient automation tester.”

This document presents the fundamental ideas and technologies required for automating web-based applications using Selenium WebDriver, Cucumber, and Python

What is Cucumber?

Cucumber is a tool that facilitates behavior-driven development (BDD), allowing for the definition of testing steps in plain English language (Gherkin). This makes it easily comprehensible to non-technical team members and business stakeholders. Cucumber is versatile and supports various programming languages such as Java, C#, Ruby, and Python, among others.

What is Selenium?

Selenium is an automation testing tool (open source) that can execute several functional test cases for web-based applications across multiple browsers. It supports multiple languages like Java, C#, Ruby, python, etc.

What is BDD?

Behavior Driven Development (BDD) is a method for crafting test cases in a format easily comprehensible to humans, utilizing Gherkin as a tool. BDD serves the dual purpose of enhancing communication between analysts and developers while also closing the divide between manual QA testers and automation testers.

Example:

Let us understand with this simple example which we will add in the feature file.

Feature: Caratlane Login

Scenario: Login to caratlane with valid parameters
Given Launching chrome browser
When Open caratlane Login page
And Enter userName "xxxxxxxx6@caratlane.com"
And click continue to login button
And Enter Password "YYYYYY"
And click on the Login button
Then User must logined successfully

Let’s now explore the intent behind each keyword within the Gherkin syntax.

Feature: What are we testing?
Scenario: How are we testing it?
Given: What is the starting state?
When: What do we do?
Then: What should happen?

Why use Cucumber with Selenium?

  1. Cucumber simplifies the process of reading and comprehending the application’s workflow.
  2. Selenium enables the execution of your functional test cases in web browsers.
  3. By combining Cucumber and Selenium, the divide between technical and non-technical team members is eliminated.
  4. Simplifies the understanding of test cases for clients.

Create Project in pycharm:

Create a Python Project as shown in the screenshot.

Adding Package files in the project:

Click on the File> Setting >Python interpreter Add all the libraries downloaded Selenium, Cucumber.

Creating a Feature File:

  1. Create a new file in the testing directory of the Python project.
  2. Name the file with an .feature extension. For example, testing.feature
  3. In the feature file, write the feature name, scenarios, and test steps.

Add scenario in the Cucumber feature file:

Create a new file in the testing directory of the project.

  1. Name the file with an .py extension. For example, testing.feature.Steps.py.
  2. In the step file, write the step definitions for the steps that are used in feature files.

Create Step Definition :

A Step Definition that corresponds to and is connected with Gherkin language feature files. Essentially, a Step Definition defines the actions to be taken for each step mentioned in the feature files. For every step in the feature file, a corresponding step definition method should be included. This ensures that each step in the feature file (whether it’s a Given, When, or Then step) has an associated method that defines its execution.

When you execute a feature file with Cucumber, the framework automatically runs the step definition methods that are linked to the steps in that feature file.

To illustrate this process, we first create a StepDefinition package and then add a login.py within it to organize and manage the step definition methods.

Executing the Script:

We can execute the script using the behave command

Output of the script.

On successful execution, this is how the output is shown with each step mentioned.

Automating BDD tests with Python is a proactive approach that can significantly enhance the reliability and effectiveness testing process. By reducing the need for manual intervention and ensuring the precision of test results, can offer a more dependable testing experience. Embracing BDD automation with Python ensures that testing procedures consistently yield accurate results, leading to a more effective and engaging testing process.

References:

Meet the team!

Author

Prasanth V

Reviewer

Gokul M

Editor

Seema Jain

--

--