Java 在特定功能之前/之后执行 Cucumber 步骤

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18856458/
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-12 11:40:30  来源:igfitidea点击:

Execute Cucumber step before/after a specific feature

javacucumbercucumber-jvm

提问by Dave Novelli

I want to specify certain setup and tear down steps for each specific feature file. I've seen hooks that allows code to execute before every scenario, and hooks to execute code before each feature, but I want to specify code to run once before and after all scenarios run for one specific feature.

我想为每个特定功能文件指定某些设置和拆卸步骤。我见过允许在每个场景之前执行代码的钩子,以及允许在每个功能之前执行代码的钩子,但我想指定代码在所有场景为一个特定功能运行之前和之后运行一次。

Is this possible?

这可能吗?

采纳答案by Oh Chin Boon

Do you use cucumber-jvm? I found an article that fits your requirement.

你用黄瓜jvm吗?我找到了一篇符合您要求的文章。

http://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/

http://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/

Basically, do not use JUnit @BeforeClass and @AfterClass for this, as they are unaware of Cucumber Hook Tags. You would want Init and Teardown methods to run for certain scenarios only right?

基本上,不要为此使用 JUnit @BeforeClass 和 @AfterClass,因为它们不知道 Cucumber Hook 标签。您希望 Init 和 Teardown 方法只针对某些场景运行,对吗?

回答by Wouter

It is if you are using junit to run your tests. We use the annotations to create a unit test class and a separate steps class. The standard @Before stuff goes in the steps class, but the @BeforeClass annotation can be used in the main unit test class:

如果您正在使用 junit 来运行您的测试。我们使用注释来创建一个单元测试类和一个单独的步骤类。标准的@Before 内容在步骤类中,但 @BeforeClass 注释可以在主单元测试类中使用:

@RunWith(Cucumber.class)
@Cucumber.Options(format = {"json", "<the report file"},
    features = {"<the feature file>"},
    strict = false,
    glue = {"<package with steps classes"})
public class SomeTestIT {
    @BeforeClass
    public static void setUp(){
       ...
    }

    @AfterClass
    public static void tearDown(){
       ...
    }
}

回答by Abhishek Bedi

Try this :

尝试这个 :

In feature file :

在功能文件中:

@tagToIdentifyThatBeginAfterShouldRunForThisFeatureOnly
Feature : My new feature ....

In Stepdefinitions.java:

在 Stepdefinitions.java 中

@Before("@tagToIdentifyThatBeginAfterShouldRunForThisFeatureOnly")
public void testStart() throws Throwable {
}

@After("@tagToIdentifyThatBeginAfterShouldRunForThisFeatureOnly")
public void testStart() throws Throwable {
}