cucumber学习笔记7
上一篇 /
下一篇 2013-08-27 18:08:15
Using hooks:Cucumber supports hooks, which are blocks of code that run before or after each scenario. You can define them anywhere in your support or step definition layers, using the methods Before and After.
To
test them, take any Cucumber project and try adding a file features/support/hooks.rb that looks like this:
Before do
puts "go"
end
After do
puts "stop"
end
you can also use @tag to run the certain Feature/Scenario:
Before('@tag1,@tag2') do
puts "go"
end
After('@tag1,@tag2') do
puts "stop"
end
The hook can accept a single argument that represents the
scenario:
Before do |scenario|
puts scenario.name
end
After do |scenario|
puts "scenario failed" if scenario.failed?
end
Around hooks:
Around hooks allowing you to actually control how many times the scenario is run. An Around hook is passed two parameters: an object that represents the scenario and a block of code
that, when called, will run the scenario:
Around do |scenario,block|
puts "start running #{scenario.name}"
block.call
puts "end running #{scenario.name}"
end
if you forget to call the block, the scenario won’t run at all.
(Around hooks can also use @tag)
One great trick we’ve seen them used for is to run the same scenario twice under different conditions, like this:
Around('@run_with_and_without_javascript') do |scenario, block|
Capybara.current_driver = Capybara.javascript_driver
block.call
Capybara.use_default_driver
block.call
end
Other Hooks:
at_exit;AfterStep;AfterConfiguration
收藏
举报
TAG: