挂钩未在Cucumber 4中运行

时间:2019-05-06 17:11:52

标签: java cucumber

在运行Runner类时,钩子的

@Before和@After方法未运行。

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

除了钩子以外,stepdef文件中的所有步骤都运行良好。最新的黄瓜版本有问题吗?

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

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

1 个答案:

答案 0 :(得分:1)

首先请确保您使用的是 cucumber.api.java.Before (界面),而不是 org.junit.Before ,因为Cucumber将使用无法处理JUnit注释。

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

希望我们在同一页面上,让我们继续前进,不要拖延。

第二让我们了解了您的步骤实现方法和HOOK CLASS是否在同一程序包中的情况,那么我们就不需要在Runner的粘胶选项中另外指定Hooks类的路径。就我而言,我确实在同一个程序包中,所以我们只需要设置一个程序包即可。

但是,如果它们位于不同的软件包中,请在运行程序文件的粘胶选项中包含Hooks类的软件包。

黄瓜赛跑者:

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>