How to ignore particular scenario in cucumber?
Asked Answered
H

11

42
  1. I am using cucumber to feed scenario and java as a language.
  2. I need to ignore particular scenario, while running an automation test.
  3. I have tried with below @ignore syntax, it doesn't work at all.
  4. It doesn't skip particular scenario, it keeps on executing all the test scenario, which I have feed in the feature file.

Feature File

@ActivateSegment
Feature: Test for Activate segment

  Scenario: Login
    Given I navigate to M
    And I enter user name 
    And I enter password 
    And I login to MM

  Scenario: Open grid
    Given I choose menu
    And I choose Segments menu

  Scenario: Open segment creation page
    Given I click on New button
    And I click on Segment button
Hanlon answered 7/1, 2016 at 12:31 Comment(4)
What tags are you using in your @Options annotation?Lareelareena
Annotation which I referred, @ignore is the annotation to ignore a scenario. import org.junit.runner.RunWith; import cucumber.api.junit.Cucumber; @RunWith(Cucumber.class) @Cucumber.Options(features = {"classpath:my_feature.feature"}, tags = {"~@ignore"})Hanlon
Hi , I am new to this. Can you elaborate, In Which file I need to add above annotation, whether in .feature file or in .java file. My need is to ignore above scenario such as Open Segment. I really got confused after referring some websites. In which file, I need to add junit annotations for skipping a certain scenario.Hanlon
Your Java test file as you have it above. you should probably add this to the question...Lareelareena
C
38

According to Cucumber.io there are 2 styles in which a tag expression can be defined. For your specific case, to exclude steps or features marked with @ignore, these 2 styles translate into:

  • old style : cucumber --tags ~@ignore
  • new style : cucumber --tags "not @ignore".

To my surprise, using the same cucumber-js v1.3.1 running on Node.js v6.9.2, I found that the Windows version accepted only the new style, while the Linux one accepted only the old style. Depending on your setup, you may want to try both and see if you succeed with any of them.

Calotte answered 19/12, 2016 at 13:44 Comment(0)
E
35

Use tag ~@tag_name

To exclude scenarios with a certain tag

cucumber --tags ~@tag_name

Note I used ~ symbol.


One thing to note here is that Cucumber will exit with a status of 1 if your @wip-tagged scenarios pass (it’s a reminder that they’re not works in progress anymore since they pass).

UPDATE 1

Sample Scenario

@billing
Feature: Verify billing

  @important
  Scenario: Missing product description

  Scenario: Several products

Running Tags

cucumber --tags @billing            # Runs both scenarios
cucumber --tags @important          # Runs the first scenario
cucumber --tags ~@important         # Runs the second scenario (Scenarios without @important)

Offical document: https://github.com/cucumber/cucumber/wiki/Tags

Ejectment answered 7/1, 2016 at 15:28 Comment(8)
I have tried with your solution. It doesn't skip at all.Can you elaborate with sample, how to use the above ta g in my feature file.Hanlon
Hi Selvi, Sorry for delayed response.Please find the updated answer.Ejectment
I have tried with ignore syntax. It doesn't work at all. Code: Feature: list owned books As a book owner I want to list my books so that I can look up which books I own @ignore Scenario: list existing books Given I have already added "Specification by Example" When I view the book list Then my book list contains "Specification by Example" Runner: cucumber { tags = ["~@ignore"] }Hanlon
@selvi, It is basic functionality of Cucumber, it will definitely work. Kindly share your feature file and running command.Ejectment
I have added my feature file above. Here, is the command, Which I am using /UIAutomation com.s.runner.UIRunner env chrome URL -t TestNameHanlon
Have you tried with { tags = ["not @ignore"] } as suggested by @M.F.? (Which version of Cucumber are you using?). And could you add both your runner and how/where you are tagging your feature or scenario to your question?Degrade
Looks like ~@skip syntax is no longer working and no longer documented. not @skip is the new way to go. cucumber.io/docs/cucumber/api/#ignoring-a-subset-of-scenariosTitanothere
As mentioned in other answers "~" character is old style and does not work in Windows for current versions of cucumber-js. Use --tags "not @tag_to_exclude"Samuelsamuela
A
23

*.feature

@skip_scenario
Scenario: Hey i am a scenario
  Given blah blah
  And blah blah blah

CucumberHooks.java

package CucumberHooks;
import cucumber.api.Scenario;
import cucumber.api.java.Before;

public class CucumberHooks {
    @Before("@skip_scenario")
    public void skip_scenario(Scenario scenario){
        System.out.println("SKIP SCENARIO: " + scenario.getName());
        Assume.assumeTrue(false);
    }
}
Antidromic answered 13/12, 2017 at 15:53 Comment(1)
I found an issue with this, apparently if i skip tests like this, cucumber exits with code 1, which fails my pipelines even for skipped testsInocenciainoculable
A
23

Using the JUnit runner class and with reference to https://cucumber.io/docs/cucumber/api/#ignoring-a-subset-of-scenarios

You can create your own ignore tag

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(tags = "not @ignore")
public class RunCucumberTest {
}

Then just tag the scenario like so:

  @ignore
  Scenario: Check if all the 3 categories of cats are displayed

    Given I open the Cats App

    When I view the home screen

    Then I should see all three cats categories displayed

Asexual answered 20/5, 2019 at 23:4 Comment(0)
P
4
@ActivateSegment
Feature: Test for Activate segment

  Scenario: Login
    Given I navigate to M
    And I enter user name 
    And I enter password 
    And I login to MM

  Scenario: Open grid
    Given I choose menu
    And I choose Segments menu

   @avoid
  Scenario: Open segment creation page
    Given I click on New button
    And I click on Segment button

in cucumber options

in runner class, use the tags as shown below that you don't want to run: tags = {"~@avoid"}

Pannikin answered 24/5, 2017 at 10:16 Comment(1)
Thanks, this worked for me, the others didn't, I am using Intellij. I had to add the -- tags option to my "run configuration" Program arguments: like this --plugin org.jetbrains.plugins.cucumber.java.run.CucumberJvmSMFormatter --monochrome --tags "~@avoid"Sounding
T
3

I believe the special tag @wip already has native support, and can be used without any other code additions.

It even has a related command line switch:

-w, --wip Fail if there are any passing scenarios.

Tabethatabib answered 9/2, 2018 at 20:34 Comment(1)
I'm not sure if belief is the best source of information ;-) I tested using just the @wip tag and it did not work (cucumber-java8 4.2.0). https://mcmap.net/q/381935/-how-to-ignore-particular-scenario-in-cucumber shows correct usage of the tag.Rhinitis
L
3

From the command line, you can write

mvn test -DCucumber.options="--tags '@login and not @grid'"

put double quote ("") outside and single quote(') inside

Laveta answered 17/6, 2019 at 12:48 Comment(0)
A
2

The JUnit 5 equivalent for this is now:

@ConfigurationParameter(key = FILTER_TAGS_PROPERTY_NAME, value = "not @Ignore")
Affine answered 8/4 at 21:51 Comment(0)
A
1

Simply use a different tag than you defined in CucumberOptions.

Let's say here you use "@regression" to runs tests:

@CucumberOptions(glue = { "stepDefinitions" }, tags = { "@regression" }, plugin = { "pretty", "io.qameta.allure.cucumber4jvm.AllureCucumber4Jvm" }

In Feature file just use a different tag than "@regression":

enter image description here

Artless answered 28/4, 2020 at 9:18 Comment(0)
I
0

To skip the test from execution Use ' not' instead of '~'( option has been updated) Ex:

tags= "not @tag1"
Isolative answered 12/9, 2021 at 16:16 Comment(0)
A
0

You could use a Before hook and skip_this_scenario like this:

Before('@test_that_needs_fancy_pants_flag') do
  fancy_pants_toggle = DataUtils.get_toggle_setting('fancy_pants')
  skip_this_scenario unless fancy_pants_toggle
end
Acidosis answered 9/7 at 16:11 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Rung

© 2022 - 2024 — McMap. All rights reserved.