Behat: Allow altering the execution order of hooks

Created on 19 Oct 2011  路  2Comments  路  Source: Behat/Behat

Currently, as far as I can tell, there is no way to specify where in the stack a hook should be inserted, and they are fired on a first-in, first-out basis. It would be helpful to be able to rearrange the order of hooks for events from within a context.

Sample use case:

  1. A feature suite uses Mink to do headless testing on a Symfony application
  2. A beforeScenario hook does some set-up on an in-memory database
  3. The hook injects the database connection into the service container of the kernel under test (otherwise the kernel would open its own connection, which would effectively be a new database)
  4. The hook is queued to run after Mink's prepareMinkSessions hook, so that the database connection can be injected after the session is initialised. Otherwise, Mink resets the session after our database set-up hook and the service container and its database connection are lost.

Most helpful comment

Let's reopen this feature request. It made sense back in 2011 for projects to have only a FeatureContext file that contains all the code and it was possible to determine the hook order by rearranging them in the file, but this is no longer the case today.

Nowadays projects are composed of dozens of contexts from various sources. It would be useful to be able to set an optional priority for certain hooks so that we can determine their execution order, especially in the case where some code needs to run before or after a hook supplied by a third party context.

@everzet @ciaranmcnulty

All 2 comments

The hooks are fired in the order, in which they were defined inside your context. That's it. I don't wanna add complex hooks ordering mechanism, as it will add more WTF effects into the feature suites. You can always redefine any hook functions with inheritance, simply disabling them and placing your own hook functions in the order you want to. It's all control you'll ever need to in your feature suites - more controll will totally kill hook system simplicity and i'm trying very hard to avoid this ;-)

Let's reopen this feature request. It made sense back in 2011 for projects to have only a FeatureContext file that contains all the code and it was possible to determine the hook order by rearranging them in the file, but this is no longer the case today.

Nowadays projects are composed of dozens of contexts from various sources. It would be useful to be able to set an optional priority for certain hooks so that we can determine their execution order, especially in the case where some code needs to run before or after a hook supplied by a third party context.

@everzet @ciaranmcnulty

Was this page helpful?
0 / 5 - 0 ratings

Related issues

diamondsea picture diamondsea  路  6Comments

DavidGarciaCat picture DavidGarciaCat  路  8Comments

stof picture stof  路  5Comments

iongion picture iongion  路  8Comments

blixit picture blixit  路  8Comments