Setting up environment for Cucumber BDD Rails

Exclusive offer: get 50% off this eBook here
Instant Cucumber BDD How-to [Instant]

Instant Cucumber BDD How-to [Instant] — Save 50%

A short and quick guide to mastering behavior-driven software development with Cucumber with this book and ebook

$14.99    $7.50
by Wayne Ye | August 2013 | Open Source Web Development

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!

In this article by Wayne Ye, author of Instant Cucumber BDD How-to , we will cover the following topics:

  • How to use Cucumber in daily BDD development on the Ruby on Rails
  • Cucumber in other languages/platforms
  • Cucumber in your mother language

(For more resources related to this topic, see here.)

Getting ready

This article 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:

  • Ruby Version Manager
  • Version 1.9.3 of Ruby
  • Version 3.2 of Rails
  • The latest version of Cucumber
  • A handy text editor; Vim or Sublime Text

How to do it...

To install RVM, bundler, and Rails we need to complete the following steps:

  1. Install RVM (read the latest installation guide from http://rvm.io ).

    $ \curl -L https://get.rvm.io | bash -s stable --ruby

  2. Install the latest version of Ruby as follows:

    $ rvm install ruby-1.9.3

  3. Install bundler as follows:

    $ gem install bundler

  4. Install the latest version of Rails as follows:

    $ gem install rails

Cucumber is a Ruby gem. To install it we can run the following command in the terminal:

  1. Cucumber contains two parts: features and step definitions. They are explained in the following section:

    $ gem install cucumber

  2. If you are using bundler in your project, you need to add the following lines into your Gemfile:

    gem 'cucumber'

How it works...

We will have to go through the following files to see how this recipe works:

  • Feature files (their extension is .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: A unique feature title within the project scope with a description. Its format is as follows:

    Feature: <feature title>
    <feature description>

  • Scenario: This elaborates how the feature ought to behave. Its format is as follows:

    Scenario: <Scenario short description>
    Given <some initial context>
    When <an event occurs>
    Then <ensure some outcomes>

  • Step definition files: A step definition is essentially a block of code associated with one or more steps by a regular expression (or, in simple cases, an exact equivalent string).

    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 exception
  • Cucumber::Pending: Step was defined but is pending implementation
  • Ruby runtime exception: Any kind of exception thrown during step execution

Similar 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:

  • Passed
  • Pending
  • Undefined
  • Failed

The following figure demonstrates the flow chart of running a Cucumber feature:

There's more...

Cucumber is not only for Rails, and the Cucumber feature can be written in many other languages other than English.

Cucumber in other languages/platforms

Cucumber is now available on many platforms. The following is a list of a number of popular ones:

  • JVM: Cucumber-JVM
  • .NET: SpecFlow
  • Python: RubyPython, Lettuce
  • PHP: Behat
  • Erlang: Cucumberl

Cucumber in your mother language

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:

Summary:

In this article we saw what is Cucumber, how to use Cucumber in daily BDD development on the Ruby on Rails, how to install RVM, bundler, and Rails, running a Cucumber feature, and Cucumber in different language and platform.

Resources for Article :


Further resources on this subject:


Instant Cucumber BDD How-to [Instant] A short and quick guide to mastering behavior-driven software development with Cucumber with this book and ebook
Published: April 2013
eBook Price: $14.99
See more
Select your format and quantity:

About the Author :


Wayne Ye

Wayne Ye is a software developer, tech lead, and also a geek, and he has immersed himself in software development for nearly 8 years, with an emphasis in C#/ASP.NET, Ruby on Rails, HTML5, JavaScript/jQuery, and nodejs. His expertise lies in GOF Design Patterns, S.O.L.i.D principle, MVC/MVVM, SOA, REST, and AOP; he strongly believes in and uses Agile, Scrum, and TDD/BDD, and he hacks with Vim daily. He is a CodeProject MVP (2012) and a certificated PMP. In his spare time, he writes tech/life blogs on WayneYe.com frequently, and spends time with his dear wife and lovely son in Shanghai.

Books From Packt


Ruby on Rails Web Mashup Projects
Ruby on Rails Web Mashup Projects

Building Dynamic Web 2.0 Websites with Ruby on Rails
Building Dynamic Web 2.0 Websites with Ruby on Rails

Ruby   on Rails Enterprise Application Development: Plan, Program, Extend
Ruby on Rails Enterprise Application Development: Plan, Program, Extend

Cloning Internet Applications with Ruby
Cloning Internet Applications with Ruby

Ruby and MongoDB Web Development Beginner's Guide
Ruby and MongoDB Web Development Beginner's Guide

RubyMotion iOS Development Essentials
RubyMotion iOS Development Essentials

Apache Maven 3 Cookbook
Apache Maven 3 Cookbook

Metasploit Penetration Testing Cookbook
Metasploit Penetration Testing Cookbook


Your rating: None Average: 2 (2 votes)

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
U
s
E
v
8
Z
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software