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

Dav*_*lli 27 java cucumber cucumber-jvm

我想为每个特定的功能文件指定某些设置和拆除步骤.我已经看到了允许代码在每个场景之前执行的钩子,并且钩子在每个功能之前执行代码,但我想指定在一个特定功能运行所有场景之前和之后运行一次的代码.

这可能吗?

Wou*_*ter 17

如果您使用junit来运行测试.我们使用注释来创建单元测试类和单独的步骤类.标准的@Before东西在steps类中,但@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(){
       ...
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是一个很好的快速解决方案:)然而,正式情况下,我认为情景的顺序并不保证,我相信你甚至可以让方案并行运行,所以你不能这样做.请记住一些事情.. (2认同)

Oh *_*oon 14

你用的是黄瓜-jvm吗?我发现了一篇符合您要求的文章.

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

基本上,不要使用JUnit @BeforeClass和@AfterClass,因为他们不知道Cucumber Hook标签.您是否希望Init和Teardown方法仅适用于某些场景?


Abh*_*edi 6

尝试这个 :

在功能文件中:

@tagToIdentifyThatBeginAfterShouldRunForThisFeatureOnly
Feature : My new feature ....
Run Code Online (Sandbox Code Playgroud)

在 Stepdefinitions.java 中

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

@After("@tagToIdentifyThatBeginAfterShouldRunForThisFeatureOnly")
public void testStart() throws Throwable {
}
Run Code Online (Sandbox Code Playgroud)

  • 它将针对该功能文件中的每个场景运行。 (4认同)