在 Cucumber 4 中挂钩不是 运行

Hooks not running in Cucumber 4

钩子的@Before 和@After 方法不是运行 而运行 Runner class。

我正在使用依赖项: 黄瓜-java 4.3.0 黄瓜-jvm 4.3.0

stepdef 文件中的所有步骤 运行 除了 hooks 都很好。最新的黄瓜版本有问题吗?

public class Hooks {
@Before
public void beforeHooks() {
    System.out.println("Run Before Scenario");
}

@After
public void afterHooks() {
    System.out.println("Run After Scenario");
}

首先确保你使用的是cucumber.api.java.Before(接口)而不是org.junit.Before 因为 Cucumber 不会处理 JUnit 注释。

  • @Before - 导入 cucumber.api.java.Before;
  • @After - 导入 cucumber.api.java.After;

希望我们意见一致,不要拖延,让我们更进一步。

Second 可以理解,如果您的步骤实现方法和 HOOK CLASS 在同一个包中,那么我们不需要指定 Hook 的路径 class 另外在跑步者的胶水选项中。在我的例子中,我确实在同一个包中有两个,所以我们只需要设置一个包。

但是如果它们在不同的包中,那么请在 runner 文件的 glue 选项中包含 Hooks 的包 class。

黄瓜赛跑者:

package com.jacksparrow.automation.suite.runner;

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

@RunWith(Cucumber.class)
@CucumberOptions(features = "classpath:features/functional/",
                     glue = {"com.jacksparrow.automation.steps_definitions.functional" },
                   plugin = { "pretty","json:target/cucumber-json/cucumber.json",
                            "junit:target/cucumber-reports/Cucumber.xml", "html:target/cucumber-reports"},
                   tags = { "@BAMS_Submitted_State_Guest_User" },
                   strict = false,
                   dryRun = false,
               monochrome = true)

public class RunCukeTest {
}

要点: 我们不能混合直接依赖和传递依赖,特别是它们的版本!这样做会导致不可预知的结果。您可以添加以下一组黄瓜最小依赖项。

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>4.3.0</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-picocontainer</artifactId>
    <version>4.3.0</version>
    <scope>test</scope>
</dependency>