Forum Discussion

pras120687's avatar
pras120687
Occasional Contributor
2 years ago

Can we skip example data sets

Hi All,

 

I need your help ASAP on this.

 

I am having 5 sets of test data using examples keyword. Here my case is if scenario is passed with one of the data set it should exit and go to next scenario. It shouldn't execute other data sets.

 

Please guide me if this is possible in cucumber.

  • jackharry's avatar
    jackharry
    Occasional Visitor

    pras120687 wrote:

    Hi All,

     

    I need your help ASAP on this.

     

    I am having 5 sets of test data using examples keyword. Here my case is if scenario is passed with one of the data set it should exit and go to next scenario. It shouldn't execute other data sets. best tablet to play minecraft

     

    Please guide me if this is possible in cucumber.


    Yes, it is possible to achieve the behavior you described in Cucumber, where you want to skip the execution of other data sets once a scenario passes with one of the data sets. You can use a combination of Cucumber's scenario outlines and conditional logic in your step definitions to accomplish this.

    Here's a high-level outline of how you can achieve this:

    1. Define your test scenario using a Scenario Outline in your feature file. Specify placeholders for the test data using < > brackets.

     

    Scenario Outline: Test my scenario with multiple data sets
      Given I have some initial state
      When I perform some action with "<test_data>"
      Then I should expect a certain outcome

     

      1. In your step definitions (e.g., in a Java or Ruby file), you will need to implement the steps for the scenario outline. You can use conditional logic to check whether the scenario should continue based on the result of the current data set.

      For example, in Java:

     

    import cucumber.api.java.en.Given;
    import cucumber.api.java.en.When;
    import cucumber.api.java.en.Then;
    
    public class MyStepDefinitions {
        private boolean shouldContinue = true; // A flag to control scenario continuation
    
        @Given("I have some initial state")
        public void givenInitialState() {
            // Implement your initial state setup here
        }
    
        @When("I perform some action with {string}")
        public void performSomeAction(String testData) {
            // Implement your action using testData
            if (someConditionIsMet(testData)) {
                // If the condition for stopping is met, set shouldContinue to false
                shouldContinue = false;
            }
        }
    
        @Then("I should expect a certain outcome")
        public void expectOutcome() {
            if (shouldContinue) {
                // Implement your outcome verification
            }
        }
    
        private boolean someConditionIsMet(String testData) {
            // Implement your condition-checking logic here
            // Return true if you want to stop further execution, false otherwise
            return false;
        }
    }

     

    • In this example, the shouldContinue flag is used to control whether the subsequent steps should be executed based on a condition you define in the someConditionIsMet method.

      1. Customize the someConditionIsMet method to evaluate whether the current data set should trigger the early exit condition. If the condition is met, set shouldContinue to false, and the subsequent steps in the scenario outline will not be executed.

      By implementing this approach, you can achieve the behavior of exiting the scenario after one data set passes without executing the remaining data sets.

    • pras120687's avatar
      pras120687
      Occasional Contributor

      Hi Jack,

       

      Thank you very much for your quick help. But even I returned 

      shouldContinue = false

      it is not stopping further set.

       

      Below is my code

      boolean shouldContinue = true; // A flag to control scenario continuation

      @Given("I want to write a step with {string}")
      public void method1(String arg1) {

      System.out.println(arg1 + "is printed");
      }

      @When("I check for the {string} in step")
      public void method2(String arg2) {

      System.out.println(arg2 + "is printed");
      shouldContinue = false;
      }

      @Then("I verify the {string} in step")
      public void method3(String arg3) {

      System.out.println(arg3 + "is printed");
      }