Cucumber的 Step Definitions

上一篇 / 下一篇  2013-01-25 13:09:09 / 个人分类:测试

Cucumber的step deifne过程,就是通过正则表达式,将feature里的文本步骤转义为可执行的代码。

好的步骤定义的标准是:

匹配串尽可能的短
最好能同时匹配正向和反向的条件
一个步骤最多两个匹配参数
用于匹配的参数名称最好简单明确
每个步骤最好不要超过10行的处理,否则进行二次封装
最好不要去引用其他的步骤


在step define文件中,没有次序的概念,Given,When,Then可以任意行定义。

甚至结构也可以是嵌套的, 例如:

Given /some "(.*)" action/  do |act|
   .  .  .
end

Then /some "(.*)" action/  do |act|
   .  .  .
end

When /in an invoiced non-shipped situation/ do
  Given "some \"invoiced\" action" 
  Then "some \"non-shipped\" action"
  .  .  .
end

我们在上例可以看到, 在when里直接包含了已经预先定义好的Given和Then,这是允许的。但是最好不要这么做。

事实上我们为了避免这种看起来很混淆的代码,我们一般用step来取代这些关键词,上面的例子就变为了:

When /in an invoiced non-shipped situation/ do
  step("some \"invoiced\" action") 
  step %Q("some \"non-shipped\" action")
  .  .  .
end


%Q是用来去除多余的“符号

对于多个step,我们可以用steps块来定义, 上例就变为:

When /in an invoiced non-shipped situation/ do
  steps %Q{
  Given "some \"invoiced\" action" 
  Then "some \"non-shipped\" action"
  .  .  .
  }
end




TAG:

 

评分:0

我来说两句

日历

« 2024-05-05  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 151745
  • 日志数: 185
  • 文件数: 6
  • 建立时间: 2007-08-06
  • 更新时间: 2015-01-06

RSS订阅

Open Toolbar