Data Driven Testing using Examples in Cucumber

HOME

 

In the previous tutorial, I have explained about various type of Reports available in Cucumber. In this tutorial, I will explain how we can do Data Driven Testing in Cucumber. Cucumber inherently supports Data-Driven testing by the use of the Scenario Outline and Examples section.  Using these keywords, Cucumber allows for easy Data-Driven testing to be complete where no changes need to made to the Java file (StepDefinition file).

Scenario Outline – It is use to repeat same tests by passing different values or arguments to Step Definition

Examples – All scenario outlines have to be follow with the Examples section. This contains the data that has to be pass on to the scenario in Feature File

Scenario Outline: Book Flight for one-way trip on 22-Jan
 
  Given I live in Dublin with 2 adults and 2 kids
  And I want to book one way flight ticket from Dublin to London on 22-Jan-2020
  When I search online
  Then TripAdvisor should provide me options of flights on 22-Jan-2020
  And Cost of my flight should not be more than 100 Euro per person
  And Tickets should be refundable
 
Scenario Outline: Book Flight for one-way trip on 12-Mar
  
  Given I live in Dublin with 2 adults and 0 kids
  And I want to book one way flight ticket from Dublin to London on 12-Mar-2020
  When I search online
  Then TripAdvisor should provide me options of flights on 12-Mar-2020
  And Cost of my flight should not be more than 50 Euro per person
  And Tickets should be refundable

We can repeatedly do something like above and check for each parameters that we want to test, but that will make tests hard to maintain with many repetitions. Instead, we can use scenario outline to add different inputs or arguments to the same scenario. We can re-write like this:

Scenario Outline: Book Flight for one-way trip
  
  Given I live in Dublin with adults and kids
  And I want to book one way flight ticket from Dublin to London on 
  When I search online
  Then TripAdvisor should provide me options of flights on 
  And Cost of my flight should not be more than Euro per person
 And Tickets should be refundable
  
 Examples:
  |noOfAdults         |noOfKids         |travelDate    |flightFare    |
  |2                  |  2              |22-Jan-2020   |100           |
  |1                  |  0              |12-Mar-2020   |50             |

Note:-  The table must have a header row corresponding to the variables in the Scenario Outline steps.

The Examples section is a table where each argument variable represents a column in the table, separated by “|”. Each line below the header represents an individual run of the test case with the respective data. As a result, if there are 2 lines below the header in the Examples table, the script will run 2 times with its respective data.

When Cucumber starts to run this program, first, it will map parameters from data table to placeholders like, and soon in the Feature File. The corresponding StepDefinition of Feature file is mention below. The steps can use delimited parameters that reference headers in the examples table. Cucumber will replace these parameters with values from the table before it tries to match the step against a step definition.

import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class MyHolidayDefinitions {

      @Given("^I live in Dublin with (\\d+) adults and (\\d+) kids$")
       public voidneedFlight(int noOfAdults, int noOfKids) {
            System.out.println("I live in Dublin with " + noOfAdults+ " adults and " + noOfKids+ " kids");
        }

      @Given("^I need 1 room with 2 double beds$")
      public voidneedRoom() {
            System.out.println("I need 1 room with 2 double beds");
       }

      @When("^I search online$")
       public voidonlineSearch() {
            System.out.println("I search online");
       }

       @Then("^TripAdvisor should provide me options of flights on (.*)$")
       public voidsearchFlightInTripAdvisor(String travelDate) {
            System.out.println("TripAdvisor should provide me options of flights on " + travelDate);
      }

       @Then("^Cost of my flight should not be more than (\\d+) Euro per person$")
       public voidflightFare(int flightFare) {
           System.out.println("Cost of my flight should not be more than " + flightFare+ " Euro per person");
      }

       @Then("^TripAdvisor should provide me options of hotels for time period of 22nd Jan 2020 to 25th Jan 2020")
       public voidsearchRoomInTripAdvisor() {
             System.out.println("TripAdvisor should provide me options of hotels for time period of 22nd Jan 2020 to 25th Jan 2020");
       }

      @And("^I want to book one way flight ticket from Dublin to London on (.*)$")
      public voidticketType(String travelDate) {
           System.out.println("I want to book one way flight ticket from Dublin to London on "+ travelDate);
       }

      @And("^I want to book hotel from 22nd Jan 2020 to 25th Jan 2020$")
       public voidstayDuration() {
             System.out.println("I want to book hotel from 22nd Jan 2020 to 25th Jan 2020");
      }

       @And("^Fare of my room should not be more than 200 Euro per night$")
       public voidroomFare() {
           System.out.println("Fare of my room should not be more than 200 Euro per night");
      }

      @And("^Breakfast should be included in the room fare$")
      public voidincludeBreakfast() {
             System.out.println("Breakfast should be included in the room fare");
      }

      @And("^Tickets should be refundable$")
      public voidrefunableTicket() {
          System.out.println("Tickets should be refundable");
      }
}

TestRunner Class

 Run the test by Right Click on TestRunner class and Click Run As  > JUnit Test Application.

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(monochrome = true, plugin = "pretty", features = "src/test/resources/features/MyHoliday.feature", tags = {
                        "@BookOneWayFlight"})
 
public classMyRunner {
}

Below is the output in Console:

2 thoughts on “Data Driven Testing using Examples in Cucumber

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s