Skip to content
This repository was archived by the owner on Nov 16, 2018. It is now read-only.
aslakhellesoy edited this page Aug 13, 2010 · 36 revisions

Cucumber provides a number of hooks which allow us to run blocks at various points in the Cucumber test cycle. There is no association between where the hook is defined and which scenario/step it is run for.

Every hook gets added to a global stack, and then each hook in the stack gets executed before or after the scenario.

All hooks defined are run whenever the relevant event occurs. For example all Before hooks defined throughout the required code will be run for each scenario.

Scenario hooks

Before do
  #do something before each scenario.
end
After do
  #do something after each scenario
end

Step hooks

AfterStep do
  #do something after each step
end

Global hooks

If you want something to happen once before any scenario is run – just put that code at the top-level in your env.rb file (or any other file in your features/support directory. Use Kernel#at_exit for global teardown. Example:


my_heavy_object = HeavyObject.new
my_heavy_object.do_it

at_exit do
  my_heavy_object.undo_it
end

Clone this wiki locally