40
  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
selvi
  • 1,271
  • 2
  • 21
  • 41
  • What tags are you using in your @Options annotation? – Reimeus Jan 07 '16 at 12:34
  • 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"}) – selvi Jan 07 '16 at 12:39
  • 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. – selvi Jan 08 '16 at 12:23
  • Your Java test file as you have it above. you should probably add this to the question... – Reimeus Jan 08 '16 at 14:24

9 Answers9

36

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.

danrhjones
  • 17
  • 7
M. F.
  • 1,654
  • 11
  • 16
33

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

Aravin
  • 6,605
  • 5
  • 42
  • 58
  • 1
    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. – selvi Jan 08 '16 at 10:29
  • Hi Selvi, Sorry for delayed response.Please find the updated answer. – Aravin Apr 01 '16 at 05:38
  • 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"] } – selvi Apr 29 '16 at 05:47
  • @selvi, It is basic functionality of Cucumber, it will definitely work. Kindly share your feature file and running command. – Aravin Apr 30 '16 at 05:39
  • I have added my feature file above. Here, is the command, Which I am using /UIAutomation com.s.runner.UIRunner env chrome URL -t TestName – selvi May 02 '16 at 10:58
  • 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? – Marit Jan 19 '18 at 09:58
  • 2
    Looks like `~@skip` syntax is no longer working and no longer documented. `not @skip` is the new way to go. https://cucumber.io/docs/cucumber/api/#ignoring-a-subset-of-scenarios – Nick Grealy Mar 09 '20 at 11:14
  • 2
    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" – David Lopez Jul 19 '20 at 22:18
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);
    }
}
ran632
  • 1,099
  • 1
  • 12
  • 14
  • 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 tests – nishantvas Mar 21 '18 at 07:25
22

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

Code Rocker
  • 500
  • 4
  • 15
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"}

RamanaMuttana
  • 481
  • 6
  • 22
  • 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" – MattG Oct 08 '18 at 14:32
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.

Pysis
  • 1,452
  • 2
  • 17
  • 31
  • 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://stackoverflow.com/a/34658962/1809799 shows correct usage of the tag. – René Jan 04 '19 at 16:12
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

Harry
  • 4,705
  • 17
  • 73
  • 101
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

james25
  • 321
  • 5
  • 4
0

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

tags= "not @tag1"