Java 如何忽略黄瓜中的特定场景?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34655222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:53:27  来源:igfitidea点击:

How to ignore particular scenario in cucumber?

javaautomationcucumber

提问by selvi

  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.
  1. 我正在使用黄瓜来提供场景和 Java 作为语言。
  2. 在运行自动化测试时,我需要忽略特定场景。
  3. 我试过下面的@ignore 语法,它根本不起作用。
  4. 它不会跳过特定场景,而是继续执行所有测试场景,我在功能文件中提供了这些测试场景。


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

回答by Aravin

Use tag ~@tag_name

使用标签 ~@tag_name

To exclude scenarios with a certain tag

排除带有特定标签的场景

cucumber --tags ~@tag_name

NoteI 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).

这里要注意的一件事是,如果您的 @wip-tagged 场景通过,Cucumber 将以 1 状态退出(这是提醒,因为它们通过后不再进行中)。

UPDATE 1

更新 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

官方文档:https: //github.com/cucumber/cucumber/wiki/Tags

回答by M. F.

According to Cucumber.iothere 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:

根据Cucumber.io,有2 种样式可以定义标签表达式。对于您的特定情况,要排除标有 @ignore 的步骤或功能,这 2 种样式转换为:

  • old style: cucumber --tags ~@ignore
  • new style: cucumber --tags "not @ignore".
  • 旧风格cucumber --tags ~@ignore
  • 新风格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.

令我惊讶的是,使用运行在Node.js v6.9.2 上的同一个cucumber-js v1.3.1,我发现Windows 版本只接受新样式,而Linux 版本只接受旧样式。根据您的设置,您可能想同时尝试这两种方法,看看是否能成功使用其中的任何一种。

回答by RamanaMuttana

@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"}

在 runner 类中,使用如下所示的你不想运行的标签:tags = {"~@avoid"}

回答by ran632

*.feature

*。特征

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

CucumberHooks.java

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);
    }
}

回答by Pysis

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

我相信特殊标签@wip已经有了原生支持,并且可以在不添加任何其他代码的情况下使用。

It even has a related command line switch:

它甚至还有一个相关的命令行开关:

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

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

回答by Code Rocker

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

使用JUnit runner class和 参考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

回答by Harry

From the command line, you can write

从命令行,您可以编写

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

mvn test -DCucumber.options="--tags '@login 而不是 @grid'"

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

把双引号 ("") 放在外面,把单引号 (') 放在里面

回答by james25

Simply use a different tag than you defined in CucumberOptions.

只需使用与您在 CucumberOptions 中定义的标签不同的标签即可。

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

假设您在这里使用“@regression”来运行测试:

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

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

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

在功能文件中,只需使用与“@regression”不同的标签:

enter image description here

在此处输入图片说明