-
Notifications
You must be signed in to change notification settings - Fork 0
Hooks
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.
Before do
# do something before each scenario.
end
or – if you want to inspect the actual scenario object:
Before do |scenario|
@scenario = scenario
# our After block can look at @scenario to find out stuff later...
end
After do
#do something after each scenario
end
AfterStep do
#do something after each step
end
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
This is only possible in 0.1.99 and higher
Only available in Cucumber 0.3.0 and above*
Sometimes you may want a certain before or after hook to run only for certain scenarios. This can be achieved by associating a hook with one or more tags. Example:
Before('@cucumis', '@sativus') do
# This will only run before scenarios tagged
# with @cucumis or @sativus.
end
Think twice before you use this feature, as whatever happens in hooks is invisible to people who only read the features. You should consider using background as a more explicit alternative if the setup should be readable by non-technical people.