【问题标题】:Why does cucumber run @Before in all glue code files为什么黄瓜在所有胶水代码文件中都运行@Before
【发布时间】:2015-09-07 08:33:53
【问题描述】:

我的黄瓜测试有问题。它运行@Before 方法 所有的胶水类。

例如。此功能文件在MainStepDef.class 中有一个粘合代码。

#language: en
@run
Feature: Testing a feature
  Test before method

  Background: 
    Given stuff is created

  Scenario: The test
    When i do stuff
    Then stuff will be done

MainStepDef

public class MainStepDef {

    @Before
    public void setup() {
        System.out.println("This is OK!");
    }

    @Given("^stuff is created$")
    public void stuff_is_created() throws Throwable {
    }

    @When("^i do stuff$")
    public void i_do_stuff() throws Throwable {
    }

    @Then("^stuff will be done$")
    public void stuff_will_be_done() throws Throwable {
    }

}

我有一个额外的胶水文件名为:OtherStep.class

public class OtherStepDef {

    @Before
    public void setup() {
        throw new RuntimeException("No! Bad code! BAD CODE!");
    }

    @Given("^some other stuff is also created$")
    public void some_other_stuff_is_also_created() throws Throwable {
    }
}

最后我有我的跑步者课。

@RunWith(Cucumber.class)
@CucumberOptions(strict = true, tags = {"@run", "~@ignore" }, 
        format = {"html:target/systemtest", "pretty" }, features = "classpath:features/",
        glue = {"com.sorkmos.stepdef" }, monochrome = true)
public class RunFeatures {

}

当我运行它时,我从 OtherStepDef 设置方法中得到运行时异常。

为什么会这样?它不应该只执行功能所需的粘合吗?

示例项目: https://github.com/cannibalcow/cucumberproblem

【问题讨论】:

    标签: java junit cucumber-jvm


    【解决方案1】:

    这是 Cucumber 的预期行为:https://groups.google.com/forum/#!topic/cukes/7gILvMsE2Js

    这是 @Before 和 @After 钩子的预期行为:它们 与每个步骤定义无关。每个钩子都在每个钩子上运行 场景(除非被标签过滤掉)。

    【讨论】:

    • 通过将粘合“之前”方法与标签分组,然后将依赖标签添加到功能文件中解决了我的问题。性能提高了 50%。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多