什么是Cucumber
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.
这样开始
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 Webrat and Rspec(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)
添加测试部分
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.
引用
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 "[email protected]"
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"
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 "[email protected]"
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"
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 => "[email protected]" 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
执行定义测试步骤
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.