Search code examples
javarubycucumbercucumber-jvm

How to skip step definitions or mark the scenario as passed?


In the below feature I am checking for the existence of a particular job type (contract) and do something if found otherwise skip the rest of the steps. When skippedm, mark the scenario as passed (technically its not a pass nor a fail or pending) How do I do it in cucumber or cucumber-jvm?

Feature: View job types
  Users can view job type from front page and from side menu

  Scenario Outline: View job type from front page

    Given I login as "<user>"
    And if there are contract jobs
    Then it should have a hourly rate
    And the daily rate in "USD" with "2" decimal places

    Examples:
    | user |
    | hello|
    | world|

Solution

  • The dirty way: In step 'if there are contract jobs'

    @jobs = false
    @jobs = true If contract_jobs
    

    Then in your following steps, say 'it should have a hourly rate'

    if @jobs
     <your other assertions>
    else
     true
    end
    

    Just setting true in a step definition makes the step pass (in fact any non-assertive statement will work). Though I wouldn't recommend building up a scenario like this (Arguably, Scenarios with conditionals are not useful / cucumber style). Personally, I would split it into 2 - a positive scenario:

    Given I login as "<user>"
    Then there are contract jobs
    And the job has an hourly rate
    And the job has a daily rate in "USD" with "2" decimal places
    

    and a negative scenario

    Given I login as "<user>" (a profile for which you know there won't be contract jobs)
    Then there are no contract jobs