cucumber的hooks
引用链接: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.
cucumber的hooks的更多相关文章
- BDD框架之Cucumber研究
BDD框架之Cucumber研究 引用链接:http://kongqingyun123.blog.163.com/blog/static/6377283520134158437813/ Cucumbe ...
- uiautomator+cucumber实现自动化测试
前提 由于公司业务要求,所以自动化测试要达到以下几点: 跨应用的测试 测试用例可读性强 测试报告可读性强 对失败的用例有截图保存并在报告中体现 基于以上几点,在对自动化测试框架选型的时候就选择了uia ...
- uiautomator+cucumber实现移动app自动化测试
前提 由于公司业务要求,所以自动化测试要达到以下几点: 跨应用的测试 测试用例可读性强 测试报告可读性强 对失败的用例有截图保存并在报告中体现 基于以上几点,在对自动化测试框架选型的时候就选择了uia ...
- cucumber:extentreports集成报告
extentreports 测试报告 只支持java..Net 先在pom.xml文件中加入包引用 <!-- report--> <dependency> <groupI ...
- 使用 Git Hooks 实现自动项目部署
最近在某服务器上面搭建 git 开发和部署环境,git 开发环境很简单,按照 ProGit 一书的相关知识就可以轻松搞定,实现了类似 Github 的使用 SSH + 私有 Clone 的方式. 关于 ...
- Cucumber(一): Preparation
Every time I wrote some code in ruby and executed our cucumber features I craved for something simil ...
- CI框架之HOOKS使用流程及原理
Ci框架中Hooks可以理解:在框架的执行流程过程中,允许开发者在固定的某些时间点上(如:调用控制器前,调用控制器后等时间点上),调用其他函数来扩充CI框架执行流程的一种方法.技术上来就是通过 ...
- Windows建立Cucumber和Ruby测试环境
1. 下载安装Ruby1.9.3, 不要用RubyInstall 一键安装,下载zip然后解压到c:\Ruby193 (不要用2.0,用2.0安装不成功,不要怪我) 2. 环境变量配置RUBY_HOM ...
- 深入浏览器兼容 细数jQuery Hooks 属性篇
关于钩子:http://www.cnblogs.com/aaronjs/p/3387906.html 本章的目的很简单,通过钩子函数更细节的了解浏览器差异与处理方案, 版本是2.0.3所以不兼容ie6 ...
随机推荐
- Jenkins配置邮件SMTP(使用QQ邮箱)
一.QQ邮箱中开启SMTP服务 进入QQ邮箱的设置页面,选择开启POP3/SMTP服务 需要发送一条短信开启服务,成功后,会收到一个密码,一定要截图.复制密码保存好 二.Jenkins中配置SMTP ...
- python 基础 字符串格式化
print "hello %s %s" % ('wd','pc') c风格 print "hello {1} {0}".format("wd" ...
- python的语法糖
# -*- coding: utf-8 -*-def deco(func): print("before myfunc() called.") func() print(" ...
- linux 统计 程序 运行时间
测试 代码运行时间 linux 中的 <sys/time.h> 中 有个函数可以获取当前时间,精确到 微秒 ----> gettimeofday() #include <sy ...
- window 删除文件提示指定的文件名无效或太长
方法0: 使用 chkdsk 磁盘修复工具 .单击“开始”,点击“运行”,输入cmd并回车打开命令提示符窗口: .在此窗口输入以下命令: 例如:检查并修复D分区 chkdsk D: /f 回车,输入 ...
- linux命令-stty
一.用途: stty——改变和打印终端行设置 二.参数: 1.打印终端行设置 -a,--all 以人可读的方式打印所有当前设置:-a参数比单独的stty命令输出的终端信息更详细 -g,--save ...
- 3. 从零开始学CSRF
为什么要拿CSRF来当“攻击手法系列”的开头篇呢?因为CSRF/XSRF我个人喜爱他的程度已经超过XSS了.如果说XSS是一个老虎,那么CSRF就是隐藏在暗处的蛇. 相信现在很多人不明 ...
- git中避免提交.DS_Store文件[转载]
1. 先删除原有的.DS_Store: find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch 命令解释:在当前文件夹 ...
- C#在Linux上的开发指南(续)
续之前的一篇开发指南http://www.cnblogs.com/RainbowInTheSky/p/5496777.html 部分人在部署的时候经常出现dll兼容问题(其实可以看小蝶惊鸿的文章,蝶神 ...
- C#和Python 图片和base64的互转
C#实例代码: /// <summary> /// 图片转base64 /// </summary> /// <param name="bmp"> ...