引用链接:https://github.com/cucumber/cucumber/wiki/Hooks

Hooks

Cucumber provides a number of hooks which allow us to run blocks at various points in the Cucumber test cycle. You can put them in your support/env.rb file or any other file under the support directory, for example in a file called support/hooks.rb. There is no association between where the hook is defined and which scenario/step it is run for, but you can use tagged hooks (see below) if you want more fine grained control.

All defined hooks are run whenever the relevant event occurs.

Scenario hooks

Before hooks will be run before the first step of each scenario. They will run in the same order of which they are registered.

Before do
# Do something before each scenario.
end
Before do |scenario|
# The +scenario+ argument is optional, but if you use it, you can get the title,
# description, or name (title + description) of the scenario that is about to be
# executed.
Rails.logger.debug "Starting scenario: #{scenario.title}"
end

After hooks will be run after the last step of each scenario, even when there are failing, undefined, pending or skipped steps. They will run in the opposite order of which they are registered.

After do |scenario|
# Do something after each scenario.
# The +scenario+ argument is optional, but
# if you use it, you can inspect status with
# the #failed?, #passed? and #exception methods. if(scenario.failed?)
subject = "[Project X] #{scenario.exception.message}"
send_failure_email(subject)
end
end

Here is another example, where we exit at the first failure (could be useful in some rare cases like Continuous Integration when fast feedback is important).

After do |s|
# Tell Cucumber to quit after this scenario is done - if it failed.
Cucumber.wants_to_quit = true if s.failed?
end

Around hooks will run “around” a scenario. This can be used to wrap the execution of a scenario in a block. The Around hook receives a scenario object and a block (Proc) object. The scenario will be executed when you invoke block.call.

The following example will cause scenarios tagged with @fast to fail if the execution takes longer than 0.5 seconds:

Around('@fast') do |scenario, block|
Timeout.timeout(0.5) do
block.call
end
end

You may want to take a look at SystemTimer if you want a more reliable timeout.

Step hooks

Warning: AfterStep hook does not work with scenarios which have backgrounds (cucumber 0.3.11)

AfterStep do |scenario|
# Do something after each step.
end

Tagged hooks

Sometimes you may want a certain hook to run only for certain scenarios. This can be achieved by associating a Before, After, Around or AfterStep hook with one or more tags. You can OR and AND tags in much the same way as you can when running Cucumber from the command line. Examples:

For OR tags, pass the tags in a single string comma separated:

Before('@cucumis, @sativus') do
# This will only run before scenarios tagged
# with @cucumis OR @sativus.
end

For AND tags, pass the tags as separate tag strings:

Before('@cucumis', '~@sativus') do
# This will only run before scenarios tagged
# with @cucumis AND NOT @sativus.
end

You create complex tag conditions using both OR and AND on tags:

Before('@cucumis, @sativus', '@aqua') do
# This will only run before scenarios tagged
# with (@cucumis OR @sativus) AND @aqua
end

After Step example:

AfterStep('@cucumis', '@sativus') do
# This will only run before steps within scenarios tagged
# with @cucumis AND @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.

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

Running a Before hook only once

If you have a hook you only want to run once, use a global variable:

Before do
if !$dunit
# do it
step "run the really slow log in method"
$dunit = true
end
end

AfterConfiguration

You may also provide an AfterConfiguration hook that will be run after Cucumber has been configured. The block you provide will be passed the cucumber configuration (an instance of Cucumber::Cli::Configuration). Example:

AfterConfiguration do |config|
puts "Features dwell in #{config.feature_dirs}"
end

This hook will run only once; after support has been loaded but before features are loaded. You can use this hook to extend Cucumber, for example you could affect how features are loaded or register custom formatters programatically.

最新文章

  1. UNP环境配置
  2. 04- Shell脚本学习--条件控制和循环语句
  3. php配置php-fpm启动参数及配置详解
  4. [原]如何在Android用FFmpeg解码图像
  5. Notes of Principles of Parallel Programming - TODO
  6. 【转】BLE_CC2540_初学者入门指导
  7. 【solr】solr5.0整合中文分词器
  8. Oracle登陆及修改用户密码
  9. c#委托和事件(上)
  10. Oracle EBS-SQL (BOM-19):主BOM与替代BOM互换.sql
  11. Codeforces Round #254 (Div. 2) DZY Loves Chemistry【并查集基础】
  12. 最大流量dinci模板
  13. 获取Excel部分数据并很据项目要求计算适宜性等级综合指数判断该地区的土壤适宜性
  14. JS高级程序设计学习笔记——继承
  15. 【Python基础】lpthw - Exercise 40 模块、类和对象
  16. element-ui 组件源码分析整理笔记目录
  17. 20145315何佳蕾《网络对抗》Web安全基础
  18. dubbo-常用配置
  19. leetcode349
  20. spring aop方式配置事务中的三个概念 pointcut advice advisor

热门文章

  1. PWA PSI statusingclient.UpdateStatus更新任务页面的AssnCustomFields的TextValue值
  2. C语言学习笔记--单引号和双引号
  3. js中的函数易忽略的点小节
  4. DES加密/解密
  5. K.I.S.S 原则
  6. 关于Synchronized关键字锁住对象的嵌套问题
  7. 关于ArcGis for javascript整合百度地图 天地图 高德地图进行搜索
  8. 常用转义字符例如&的含义
  9. FMDB 使用注意点
  10. Boost Python官方样例(二)