33

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?

Dave Novelli
  • 1,774
  • 4
  • 23
  • 36

3 Answers3

19

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?

Oh Chin Boon
  • 21,393
  • 45
  • 133
  • 208
  • [cucumber-jvm](https://github.com/cucumber/cucumber-jvm/issues/515) explains it better than the above article. – CodeMonkeyKing Sep 01 '15 at 01:04
  • Although if you have multiple feature file then Global Hook might be problematic – Nikhil Dec 21 '17 at 01:56
  • Note that the imports in that link are out-of-date. Instead of 'cucumber.annotation.{Before/After}' you should import 'cucumber.api.java.{Before/After}' – João Matos Apr 04 '18 at 14:21
17

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(){
       ...
    }
}
Wouter
  • 3,763
  • 2
  • 28
  • 49
  • If I understand correctly, in order to provide a specific setUp and tearDown for each feature, each feature would need it's own unit test class? It's not ideal, but a it's a reasonable solution. – Dave Novelli Sep 17 '13 at 19:52
  • There is an alternative you might try, which I have never used, so don't know it works: Add tags to your feature definition and then use the cucumber before annotation like so @Before("tagname"). This needs a bit of extra housekeeping, but then you don't need a testclass per feature so it seems. – Wouter Sep 18 '13 at 05:59
  • 1
    Another approach dawned on me that's even simpler. Just create scenarios for the setup and teardown. It may violate the intention of cucumber, but it's very straightforward and doesn't require any additional modifications – Dave Novelli Sep 18 '13 at 21:48
  • 2
    It's a nice quick'ndirty solution :) Officially however the order of scenario's isn't guaranteed I think and I believe that you can even have the scenario's run in parallel, so you can't do that. Just something to keep in mind.. – Wouter Sep 19 '13 at 06:02
  • I'm unable to run any methods in that class. – Julsteri Jun 02 '17 at 10:39
4

Try this :

In feature file :

@tagToIdentifyThatBeginAfterShouldRunForThisFeatureOnly
Feature : My new feature ....

In Stepdefinitions.java :

@Before("@tagToIdentifyThatBeginAfterShouldRunForThisFeatureOnly")
public void testStart() throws Throwable {
}

@After("@tagToIdentifyThatBeginAfterShouldRunForThisFeatureOnly")
public void testStart() throws Throwable {
}
Joel H.
  • 2,754
  • 3
  • 21
  • 34
Abhishek Bedi
  • 4,097
  • 1
  • 27
  • 56