I have a problem with my cucumber tests. It runs the @Before
method in
all the glue classes.
For example. This feature file have one glue code in the 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
The 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 { } }
I have an additional glue file called: 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 { } }
And finally I have my runner 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 { }
When I run this I get the runtime exception from the OtherStepDef
setup method.
Why does this happen? Should it not execute only the glue needed for the feature?
Example project: https://github.com/cannibalcow/cucumberproblem
Advertisement
Answer
This is the intended behaviour of 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).