Search code examples
testingcucumber

How do I stop the execution of a .feature file with cucumber


I have the following scenario:

Scenario Outline: Searching for something on Google
    Given user goes to google.com
    When user search for "<something>"
    Then <somethingHappens>

I would like to prevent Then <somethingHappens> from being executed if a condition in When user search for "<something>" is not met. Let say that google does not find something, how do I stop the execution of the test?

I found some post saying that this is not the way Cucumber works. Instead, I'd rather create another scenario that would match what I'm looking for. Is that correct?

Thanks in advance for your help


Solution

  • There are few ways you gan go:

    1. The correct way is to have two scenarios. Because the behavior (aka logic) is not defined for the case that notheing has been found. I would rework your scenario to:
    Scenario Outline: Searching for something on Google
        Given user goes to google.com
        When user search for "<something>"
        And there are results returned
        Then <somethingHappens>
    

    and

    Scenario Outline: Searching for something on Google
        Given user goes to google.com
        When user search for "<something>"
        And there are no results returned
        Then <nothingHappens>
    

    Hence your logic would look more like a test..

    1. You can use the capabilities of unit-test framework that is currenty driving your Cucumber test. For example if you are using JUnit you coud have the step definition like this:
    @When("user search for something")
    public void when(){
      boolean condition = testForConditionMet();
      Assume.assumeTrue(condition)
    }
    

    The above code would make your test skip if condition is false.

    If you would like to fail the test then use Assert.assertTrue(condition) instead of Assume.

    I would not recommend to use the second approach since you are stuck to unit-test framework in that case.