Scenario outline in Cucumber

Sometimes, we need to execute the same scenario but with different test data. In such cases, we can use the concept called as scenario outline. In below example, same scenario will be executed twice. First time with 99 and 100. Second time with 11 and 200.

Feature: Outline feature

  Scenario Outline: Addition of numbers
    When I add integers <i1> and <i2>
    Then I see the result as <sum>

    Examples:
      | i1     | i2     | sum     |
      |    99  |  100   |     199 |
      |    11  | 200    |     211 |

Here is the step definitions class.

package org.softpost;

import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import junit.framework.Assert;

/**
 * Created by Sagar on 13-07-2016.
 */
public class outlinesteps {
    int result = 0;
    @When("^I add integers (\\d+) and (\\d+)$")
    public void i_add_integers_and(int arg1, int arg2) throws Throwable {
      result = arg1 + arg2;
        System.out.println("Adding " + arg1 + " and " + arg2 + "\n\n");
    }

    @Then("^I see the result as (\\d+)$")
    public void i_see_the_result_as(int arg1) throws Throwable {
        Assert.assertEquals(result,arg1);
    }

}

Here is the output of execution of above feature file.

Testing started at 19:18 …
Adding 99 and 100

Adding 11 and 200

2 Scenarios (2 passed)
4 Steps (4 passed)
0m0.595s

You may also like...