为什么 cucumber 运行 @Before 在所有胶水代码文件中

Why does cucumber run @Before in all glue code files

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

例如。此功能文件在 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 {
    }
}

终于有了我的 运行ner class。

@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 设置方法中得到 运行time 异常。

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

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

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

This is the intended behaviour of the @Before and @After hooks: they are not related to each step definition. Every hook is run on each scenario (unless it's filtered out by tags).