What Is Cucumber? A Detail Guide On Cucumber Testing Tool

Devstringx Technologies
6 min readJan 6, 2023

--

To Understand what is Cucumber, we must first understand What Is Behaviour Driver Development (BDD)?

What Is BDD?

BDD is one of the Development approaches which involves the usage of a common language that enhances communication between various tech and non-tech teams. Tests are more user-focused and based on the system’s behavior. In “BDD”, the Given, When, Then approach is used for writing test cases.

  • Given: In this keyword, we defire we define the pre-condition of our testcase.
  • When: This is where we define the Actions that are to be performed in order for our test case to run.
  • Then: Here we validate the outcomes of the test case, basically comparing the expected and actual test case

What Is Cucumber?

Cucumber is a tool that supports writing test cases in the Behavior Driver Development format. Cucumber also provides a lot of additional features like Tags, and Hooks Feature files, which actually decrease the number of lines in our code.

How Is Cucumber different from Selenium?

Selenium is the tool to design web Automation Tests, whereas Cucumber helps to design Framework for Selenium Automation tests written in BDD standard.

Pre-requisites

For our automation framework to understand Cucumber concepts, we must install one Plugin, called Cucumber Plugin (That is available in the Eclipse marketplace, or could be found in maven dependency).

Cucumber needs a skeleton to run on it, we call it Maven. We have our pom.xml file where we add all the dependencies of the project. For the project, we need to install Cucumber’s latest version from the mvnrepository.

Terminologies in Cucumber

  • Feature File

A feature file is a file with the extension .features. It is where we write all the scenarios and steps in the “Given, when and then” format is written properly in plain English.

Here is an example:

  • Scenario: Title of your scenario
  • Given I want to write a step with precondition
  • And some other precondition
  • When I complete the action
  • And some other actions with “Correct” data
  • And yet another action with “Correct” data
  • Then I validate the outcomes with “true”
  • And check more outcomes

We use “And” in the Scenario to combine the step with the previous steps in our feature file.

  • Step definition file: This is our class, where we write the code to automate each step on the scenario from our feature file. We map the test methods written in the step definition file with the test step written in the feature file.
  • Test Runner file: This is where we pass the path of our feature file, and mention all the scenarios to run in our test run.

Getting Dynamic Data from the Feature File

When we have to run the same tests with some positive and negative scenarios. We use one correct and one incorrect data. How can we do it through Cucumber?

We can use the same line of code in our feature file, we put the dynamic data in double quotes “dynamic data”. And use that data in our feature file using regex. Example:

Implement the above line or code in our Step definition class:

@And(“^some other action with \”([^\”]*)\” data$”)

public void some_other_action_with_something_data(String strArg1) throws Throwable {

System.out.println(“Printing data from When with data….”+strArg1);

}

@And(“^yet another action with \”([^\”]*)\” data$”)

public void yet_another_action_with_something_data(String strArg1) throws Throwable {

System.out.println(“Printing some more data from When with data….”+strArg1);

}

We use Regex, for the same implementation but with different data.

Getting Data from the Feature File:

Tables in Cucumber: When we want to run the tests with different data. We can write the data in a line separated by a Pipeline Operator- “|”. Cucumber is smart enough to separate the data between these pipeline operators. Example:

| jenny | abcd | john@abcd.com |Australia | 3242353|

To use this data in our test Class:

We use this data, we create a list of Lists

List<List<String>> allData= new ArrayList<List<String>>();

allData= data.asLists();

allData.get(0).get(0);

This will return the data in the first column of the first row. In our case, it is “jenny”. Now we can use it anywhere in our code.

Recommended to Read- Most Popular Automation Testing Tools

Parameterization of the test cases

In order to use Parameterization, we must write and declare the Scenario outline first

Scenario Outline

We define all the scenarios in our feature file and when we want to run the tests multiple times but with a different set of data.

User login with a different username. Login with 10 different usernames and passwords.

How to achieve it?

In our feature file:

In the Step, we should make a dynamic variable with <UserName> and password <Password>

Examples

|Username |Password|

|user1 |pass1 |

|user2 |pass2 |

|user3 |pass3 |

|user4 |pass4 |

In this scenario, the test case will run 5 times with the UserName and Password as mentioned above.

Tagging Feature In Cucumber

Suppose, we have a scenario where we want to run only selected test cases, then we can tag them as @SmokeTests, @SanityTests, @RegressionTests, or @MobileTests.

Cucumber provides us the facility to run only the test cases with the Tags that we want to run.

We can provide the tag name of the test cases in our Runner file, and that’s it. Only test cases which are tagged with the tag we mentioned, will run.

Example: In the screenshot below, the scenario is tagged with @smokeTest. When we mention this in our Runner class. Only this class will run.

Our Runner Class

Note: We can run tests with Multiple tags. Let’s say we want to run both Smoke and Sanity Tests, we can pass both the tags in our Runner class. And all the Test Methods are tagged with both the @SmokeTests and @SanityTests.

Understanding or and “And” or not in the tags:

  1. @SmokeTest: Only Smoke tests will run
  2. @SmokeTest and @RegTest: TestMethods with both tags- smoke and Regression tests will run
  3. @SmokeTest or @RegTest: TestMethods with either tags- smoke or Regression will run. Tests with both tags will not run here.
  4. not @SmokeTest: All the test cases except the SmokeTests will run

Recommended to Read- What Is Smoke Testing?

Other Tags in Cucumber

@Background Keyword: The Background keyword is used to define what all steps should run before all our test cases. It is just like the BeforeClass in TestNg Framework. The Steps mentioned in Background will always run first.

All the test steps mentioned under the Background will run before all our scenarios in our feature file.

We use this keyword when pre-requisite is common across all the feature files.

Hooks In Cucumber

We use hooks when we want to run a piece of code before or after some of the test methods in our feature file.

  • @Before Keyword: The steps mapped with the Before keyword will always run before all the scenarios of the feature file.

Example: @Before(@MobileTest)

Public void beforeValidation(){
}

What this means is, all the before-validation classes will run before all the mobile test Methods.

We can’t use Background and @Before together for the same feature test, as it will create ambiguity and Cucumber will not be able to decide which one to run tests. As both of these run before the test methods

  • @After Keyword: The steps mapped with the After keyword will always run after each scenario mentioned in our feature file.

We generally use this hook to close the browser, quit the driver, or log the test status in the console.

With years of experience our experts have written a lot of other blogs on software testing to boost your knowledge please feel free to visit & read our blogs.

Originally published at https://www.devstringx.com on January 06, 2023

--

--

Devstringx Technologies

Devstringx Technologies is highly recommended IT company for custom software development, mobile app development and automation testing services