Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Cucumber step before/after a specific feature

I want to specify certain setup and tear down steps for each specific feature file. I've seen hooks that allows code to execute before every scenario, and hooks to execute code before each feature, but I want to specify code to run once before and after all scenarios run for one specific feature.

Is this possible?

like image 338
Dave Novelli Avatar asked Sep 17 '13 17:09

Dave Novelli


People also ask

How do you set an execution order in Cucumber?

The execution order of Hooks Execution order will be Before Hook 0 -> Before Hook 1 -> Scenario -> After Hook 1 -> After Hook 0. Before the first step of each scenario, Before hooks will be run.

How do you use before class in Cucumber?

We use the annotations to create a unit test class and a separate steps class. The standard @Before stuff goes in the steps class, but the @BeforeClass annotation can be used in the main unit test class: @RunWith(Cucumber. class) @Cucumber.


2 Answers

Do you use cucumber-jvm? I found an article that fits your requirement.

http://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/

Basically, do not use JUnit @BeforeClass and @AfterClass for this, as they are unaware of Cucumber Hook Tags. You would want Init and Teardown methods to run for certain scenarios only right?

like image 130
Oh Chin Boon Avatar answered Oct 17 '22 09:10

Oh Chin Boon


It is if you are using junit to run your tests. We use the annotations to create a unit test class and a separate steps class. The standard @Before stuff goes in the steps class, but the @BeforeClass annotation can be used in the main unit test class:

@RunWith(Cucumber.class)
@Cucumber.Options(format = {"json", "<the report file"},
    features = {"<the feature file>"},
    strict = false,
    glue = {"<package with steps classes"})
public class SomeTestIT {
    @BeforeClass
    public static void setUp(){
       ...
    }

    @AfterClass
    public static void tearDown(){
       ...
    }
}
like image 19
Wouter Avatar answered Oct 17 '22 07:10

Wouter