Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global BeforeAll Hook for cucumber-jvm?

The ruby version of cucumber supports a global before hook. An *.rb file placed in the features/support directory gets apparently called only once, before any and all scenarios run. See https://github.com/cucumber/cucumber/wiki/Hooks#global-hooks

That seems to be a great way to make sure a database (used in read-only during tests) gets populated (and thus is in a known state) before any tests run.

Is there a similar feature available for the Java version of cucumber?

like image 646
Christian Avatar asked Nov 04 '13 14:11

Christian


People also ask

Does Cucumber-JVM support global hook?

Almost all BDD automation frameworks have some sort of hooks that run before and after scenarios. However, not all frameworks have global hooks that run once at the beginning or end of a suite of scenarios – and Cucumber-JVM is one of these unlucky few.

What are global hooks in Cucumber?

What is Hook in Cucumber? In Cucumber, the hook is the block of code which can be defined with each scenario in step definition file by using the annotation @Before and @After. These @Before and @After annotations create a block in which we can write the code.

Which runner runs scenarios with Cucumber-JVM?

Apache Maven is the preferred build management tool for Cucumber-JVM projects. All Cucumber-JVM packages are available from the Maven Central Repository. Maven can automatically run Cucumber-JVM tests as part of the build process.

What are the different hooks available in Cucumber?

Unlike TestNG Annotations, cucumber supports only two hooks (Before & After) which works at the start and the end of the test scenario. As the name suggests, @before hook gets executed well before any other test scenario, and @after hook gets executed after executing the scenario.


2 Answers

There is no such feature natively in Cucumber JVM (see https://github.com/cucumber/cucumber-jvm/issues/515).

However, there are a couple of workarounds:

  • Use the hooks of your test framework:
    • @BeforeAll and @AfterAll for JUnit 5,
    • @BeforeClass and @AfterClass for JUnit 4
  • Use a Cucumber Before hook for lazy singleton initialization and a JVM shutdown hook for teardown
  • Implement a Cucumber EventListener and subscribe to TestRunStarted and TestRunFinished events
  • Use the integration test lifecycle features of your build framework, e.g. Maven's pre-integration-test, integration-test, post-integration-test phases and the maven-failsafe-plugin.

You will also have to solve the issue of injecting the results of such a setup step (e.g. random port numbers) into your tests.

I wrote a blog article to cover all details: https://metamorphant.de/blog/posts/2020-03-10-beforeall-afterall-cucumber-jvm-junit/

like image 28
JFK Avatar answered Oct 27 '22 09:10

JFK


Please follow this link. this a workaround for @BeforeAll and @AfterAll

https://github.com/cucumber/cucumber-jvm/issues/515

like image 144
nat Avatar answered Oct 27 '22 08:10

nat