1

This question is the same as this one but for Java. I've not found any satisfactory answer for this potential limitation: @Before and @After annotations are called for every scenario executed and I was wondering how to invoke setup and cleanup methods before and after each feature. Thank you for your time.

Community
  • 1
  • 1
João Matos
  • 6,102
  • 5
  • 41
  • 76
  • What is your use case behind that question? Needing a single setup (once executed for all feature test) most likely reveals a wrong approach to unittests. It most likely leads to execution order dependencies between the tests an alike sort of problems. – Timothy Truckle Dec 27 '16 at 17:24
  • So basically what your are saying is that using the @BeforeStory/@AfterStory @BeforeStories/@AfterStories annotations of JBehave is just bad practice? (http://jbehave.org/reference/stable/annotations.html) What I'm asking is for solutions that enable the implementation of something analogous to the above-mentioned annotations of JBehave in Cucumber. – João Matos Dec 27 '16 at 18:07
  • *"So basically what your are saying is that using the [...] annotations of JBehave is just bad practice?"* as far as *UnitTests* are concerned: Yes. - But there are other test types which may be implemented using JBehave (or any other testing framework) for which they may be useful. – Timothy Truckle Dec 27 '16 at 18:29
  • "as far as UnitTests are concerned": Who said anything about UnitTests? "But there are other test types [...] for which they may be useful": why is it so hard to believe that I'm trying to perform such type(s) of test using cucumber? – João Matos Dec 27 '16 at 18:47
  • *"why is it so hard to believe that I'm trying to perform such type(s) of test using cucumber?"* it is not hard to believe, you just didn't mentioned it before.. – Timothy Truckle Dec 27 '16 at 18:50
  • Why would I mention that? Why did it even cross your mind? It is not (even remotely) implicit in my question.. – João Matos Dec 27 '16 at 23:59
  • http://stackoverflow.com/questions/18856458/execute-cucumber-step-before-after-a-specific-feature... Setup which runs once only for a feature is easy to do. Give the feature a tag(make this unique and use here only) and add that tag to the before hook. This makes the hook run only for this feature. Setup a static flag and set it to false. In the before hook check the value of the flag. If false run setup code and set flag to true. If true skip setup. – Grasshopper Dec 28 '16 at 08:20
  • For cleanup with After hook, you can add code to the jvm shutdown hook, fits your needs if this the only feature file. Other way is to create a separate scenario for cleanup at the end of the feature file. In case of an error in any scenario and you do not want to clean up you should set up an After hook with the feature tag and pass the Scenario object to this. From the Scenario object you can check if the scenario failed and setup a static flag accordingly. Before the cleanup logic you can check the value of the flag. I think this should work if you doing parallel stuff but cannot guarantee. – Grasshopper Dec 28 '16 at 08:29
  • Thank you Grasshopper, I will look into that today :) – João Matos Dec 28 '16 at 08:39
  • refer [answer](https://stackoverflow.com/questions/49826054/do-we-have-any-annotation-in-cucumber-where-it-will-run-before-any-of-the-tests/49835048#49835048) for similar question. – user861594 Apr 14 '18 at 21:46

0 Answers0