Cucumber is a tool for BDD-style development widely used in the Ruby on Rails platform. It introduced a domain-specific language (DSL) named Gherkin to allow the execution of feature documentation written in business-facing text, and implement acceptance test code in other languages (for example Ruby).
Cucumber sets up a great bridge between business people and development teams. Its natural and human readable language ultimately eliminates misunderstanding, and the regular expression "translation" layer provides the ability for developers to do anything magical and powerful!
This book will focus on how to use Cucumber in daily BDD development on the Ruby on Rails platform. Please install the following software to get started:
To install RVM, bundler, and Rails we need to complete the following steps:
$ curl -L https://get.rvm.io | bash -s stable --ruby
$ rvm install ruby-1.9.3
$ gem install bundler
$ gem install rails
Cucumber is a Ruby gem. To install it we can run the following command in the terminal:
$ gem install cucumber
Gemfile
:gem 'cucumber'
We will have to go through the following files to see how this recipe works:
.feature
): Each feature is captured as a "story", which defines the scope of the feature along with its acceptance criteria. A feature contains a feature title and a description of one or more scenarios. One scenario contains describing steps.Feature: <feature title> <feature description>
Scenario: <Scenario short description> Given <some initial context> When <an event occurs> Then <ensure some outcomes>
Given "I log into system through login page" do visit login_page fill_in "User name", :with => "wayne" fill_in "Password", :with => "123456" click_button "Login" end
When running a Cucumber feature, each step in the feature file is like a method invocation targeting the related step definition. Each step definition is like a Ruby method which takes one or more arguments (the arguments are interpreted and captured by the Cucumber engine and passed to the step method; this is essentially done by regular expression). The engine reads the feature steps and tries to find the step definition one by one. If all the steps match and are executed without any exceptions thrown, then the result will be passed; otherwise, if one or more exceptions are thrown during the run, the exception can be one of the following:
Cucumber::Undefined
: Step was an undefined exceptionCucumber::Pending:
Step was defined but is pending implementationRuby runtime exception:
Any kind of exception thrown during step executionSimilar with other unit-testing frameworks, Cucumber runs will either pass or fail depending on whether or not exception(s) are thrown, whereas the difference is that according to different types of exceptions, running a Cucumber could result in the following four kinds:
The following figure demonstrates the flow chart of running a Cucumber feature:
Cucumber is not only for Rails, and the Cucumber feature can be written in many other languages other than English.
Cucumber is now available on many platforms. The following is a list of a number of popular ones:
We can actually write Gherkin in languages other than English too, which is very important because domain experts might not speak English. Cucumber now supports 37 different languages.
There are many great resources online for learning Cucumber:
18.217.199.122