OSChina 的链接 Cucumber
ruby 的 BDD框架,类似自然语言的DSL,适用于BDD模式和验收测试。
Book
The Rspec Book
The Cucumber Book
本系列所有内容取自The Rspec Book 虽标记原创 但其实我只是翻译总结了一下
参数
Given a "150" tickets and "0" taken do |arg1,arg2|
#arg1 and arg2 都是字符串类型!
end
嵌套
你可以把一个Step继续划分,在Step Definition里继续定义
When /I transfer (.*) from (.*) to (.*)/ do |amount, source, target|
steps %Q{
When I select #{source} as the source account
And I select #{target} as the target account
And I set #{amount} as the amount
And I click transfer
}
end
Hooks
• Before: 每个Scenario之前执行
• After: 每个Scenario之后执行
• AfterStep: 每个Step之后执行
• 上面三个方法可以跟标签 例如 Before(@foo)
使用标签的规则跟标签规则一致
例子:
# -*- encoding : utf-8 -*-
class Hello
def greet
"Hello Cucumber!"
end
end
Before do
puts "Now im going to work!"
end
After do
puts "Now i have finished my work!"
end
AfterStep do
puts "I finish one step!"
end
Given /^如该我给了你一个测试的hello$/ do
@hello = Hello.new
end
When /^当我给你一个greet的时候$/ do
@message = @hello.greet
end
Then /^我应该看到 "(.*?)"$/ do |greeting|
@message.should == greeting
end
执行结果:
$ cucumber features
Feature: greeter says hello
Cucumber的Hello World
Scenario: 对世界打个招呼吧 # features/greeter_says_hello.feature:3
Given 如该我给了你一个测试的hello # features/step_definitions/g_steps.rb:16
Now im going to work!
I finish one step!
When 当我给你一个greet的时候 # features/step_definitions/g_steps.rb:20
I finish one step!
Then 我应该看到 "Hello Cucumber!" # features/step_definitions/g_steps.rb:24
I finish one step!
1 scenario (1 passed)
3 steps (3 passed)
0m0.004s
Background
Background 是feature的hook (上面的是step definition里的hook)
例子如下:
Feature: invite friends
Background: Logged in
Given I am logged in as "Aslak"
And the following people exist:
| name| David |
| friend? |yes|
| Vidkun | no|
Scenario: Invite someone who is already a friend
Scenario: Invite someone who is not a friend
Scenario: Invite someone who doesn't have an account
执行顺序是:Before–>Backgroun–>Scenario
Tables
Scenario:Booking ticket
When I need a table
|name|number|
|abc |1|
|def |2|
一个Table传递过去是一个Cucumber::Ast::Table 一种类似Hash的结构 用ruby处理如下,其他语言也可以处理:
When /^I need a table$/ do |table|
table.hashes.each do |hash|
hash.each do |k,v|
puts "#{k}:#{v}"
end
end
end
执行结果:
Scenario: Booking ticket # features/say_good_night.feature:6
When I need a table # features/step_definitions/good_steps.rb:11
name:abc
number:1
name:def
number:2
| name | number |
| abc | 1 |
| def | 2 |
1 scenario (1 passed)
1 step (1 passed)
0m0.006s
Outline
outline是处理类似的Scenario的时候使用的
比如:
Scenario: One Scenario
Given the secret code is 1234
When I guess 1234
Then the mark should be bbbb
Scenario: Another Scenario
Given the secret code is 1234
When I guess 1235
Then the mark should be bbb
可以用另一种形式写出来:
Scenario Outline: submit guess
Given the secret code is "<code>"
When I guess "<guess>"
Then the mark should be "<mark>"
Examples: all numbers correct
| code | guess | mark |
| 1234 | 1234 | ++++ |
| 1234 | 1243 | ++-- |
| 1234 | 1423 | +--- |
| 1234 | 4321 | ---- |
在这种情况下,cucumber会自动依次带入Scenarios里的table到Scenario Outline里来执行。
明天Cucumber in Rails