Cucumber入门示例

Cucumber过去一年的测试热点和行为驱动开发的引导。
[b]
什么是Cucumber[/b]

Cucumber is known as an Acceptance Testing Framework. It’s part of Rspec’s testing tools. Aslak Hellesoy started it as an alternative to overcome the shortcomings of existing Rspec Story Runner. Cucumber allows you to write feature documentation in Plain Text. It means you could sit with your Client or Business Analyst to write down the features to be build on your application. Though Cucumber itself is written in Ruby, it can be used to test applications written in any language.

[b]这样开始[/b]

Lets see how to use Cucumber, within the context of a Ruby On Rails application.

To get started, first install the Cucumber gem.


   sudo gem install cucumber



Also, we will require [size=large]Webrat[/size] and [size=large]Rspec[/size](it’s not necessary, you could even use Test::Unit assertions)


   sudo gem install rspec rspec-rails webrat



Now move to your Rails application(or create a new one). Inside your Rails application directory run:


   script/generate cucumber



This will create a new directory called ‘features’ which includes Cucumber specific environment configurations and common set of steps(more on steps later)

[b]添加测试部分[/b]

Centerpiece of Acceptance Testing is defining your features. It is recommended to do this process by hand, without using the generators.

For example, we need to add a admin section to manage users of our app. We could define this feature file(features/admin_dashboard.feature) in Cucumber, like this.


[quote] Feature: Admin Dashboard
In order to manage my users
As the admin of the app
I want to have an admin dashboard

Scenario: Listing All Users
Given I logged in as admin
When I visit users section
Then I should see all current users

Scenario: Editing a user
Given I logged in as admin
And there is a user called "Tom"
When I edit Toms email
Then I should see "Save successful!"
And I should see "newtom@example.com"

Scenario: Deleting a user
Given I logged in as admin
And there is a user called "Tom"
When I delete "Tom"
Then I should see "User was deleted."
And I should not see "Tom"

Scenario: Searching for a user
Given I logged in as admin
And there is a user called "Tom"
And there is a user called "Jerry"
When I search for "Tom"
Then I should see "Tom"
And I should not see "Jerry"[/quote]


Header of the feature file is used to introduce the feature, what is its purpose, for whom it would matter and how it would be profitable for the business. There is no strict rule on what should be added in this section, you could add whatever you want in here. Define the tests in first-person. It will give you better focus and good understanding on how you would really use the feature.

After the header you have to define different scenarios of the feature. Each scenario consists of set of steps. A step must start with one of the following keywords - Given, When, Then, But or And. These steps allows you to structure your scenarios to a form which is easy to comprehend for anyone.

A step should have a definition which is written in Ruby. A step definition should consist a keyword, a string or regular expression, and a block. For example:


  When /^I edit Tom's email$/ do
fill_in "Email", :with => "newuser@example.com"
clicks_button "Update"
end



Above step definition, instructs Cucumber to fill in the Email field and submit the form. You could define steps as if a live user is interacting with the app. This has become possible thanks to Webrat. It allows you to fill in forms, follow links and scan for content within a page, as if it’s viewed via a web browser.

Further, you could attach hooks to your scenarios and steps. For example defining a ‘Before’ hook would ensure the code block is executed before each scenario.


  Before do
@user = User.find_by_name("Tom")
end



[b]执行定义测试步骤[/b]

To run all features in your app, in the command line enter rake features. If you need only a specific feature to be tested, run that feature file with cucumber command:


   cucumber features/admin_dashboard.feature



When a features are executed, it will show which the steps which are still pending, steps failed and steps skipped. Running features could also be used to grasp the status of a project. You will know how many features are still pending, which feature is currently in development and etc.
Give a try

I just covered only the basics of Cucumber. You will come to learn a lot when you give it a try. Especially, things such as how to use it with traditional unit testing and how to pace in the outside-in approach.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值