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

时间:2015-09-07 08:33:53

标签: java junit cucumber-jvm

我的黄瓜测试有问题。它运行@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

1 个答案:

答案 0 :(得分:2)

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

  

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