Book Image

Instant Cucumber BDD How-to

By : Wayne Ye
Book Image

Instant Cucumber BDD How-to

By: Wayne Ye

Overview of this book

<p>Cucumber is a Behavior Driven Design framework, which allows a developer to write specification tests which then tests that the program works as it should. It is a different development paradigm, as it involves writing what the program should do first, then you develop until it passes the tests.<br /><br />Instant Cucumber BDD How-to will cover basics of Cucumber in a Behaviour Driven Development (BDD) style and explain the essence of Cucumber, describe how to write Cucumber features to drive development in a real project, and also describe many pro tips for writing good Cucumber features and steps. Cucumber is a very fun and cool tool for writing automated acceptance tests to support software development in a Behaviour Driven Development (BDD) style.<br /><br />Instant Cucumber BDD How-to will highlight Cucumber's central role in a development approach called Behaviour Driven Development (BDD), describe how to write Cucumber features to drive development in a real project, and finally introduce some famous third-party libraries used inline with Cucumber.</p> <p>It will show you how to carry out all the tasks associated with BDD using Cucumber and write basic Cucumber steps. It will assist you in using Pro tips for writing expressive Gherkin and implement guidelines for writing DRY steps. You'll learn how to use Cucumber's Gherkin to describe the behavior customers want from the system in a plain language.</p>
Table of Contents (7 chapters)

Writing your first Hello World feature (Simple)


During the first two recipes we learnt the concept of BDD and the basics of Cucumber; now we know that we can benefit from BDD using Cucumber, so it is time to write the first Hello World Cucumber feature.

Getting ready

In the first recipe we've already successfully installed Ruby, RubyGems, bundle, and Rails. To write our first Cucumber feature, we need a Rails application with Cucumber installed.

How to do it...

Now we create a Rails project and install Cucumber in the project. Follow the given steps:

  1. Create a new Rails app, cucumber_bdd_how_to, by running the following Rails command in the terminal:

    $ rails new cucumber_bdd_how_to
    
  2. Add gem 'cucumber-rails' into the project's Gemfile; it should be similar to the following code snippet:

    source 'https://rubygems.org'
    
    gem 'rails', '3.2.9'
    
    # Bundle edge Rails instead:
    # gem 'rails', :git => 'git://github.com/rails/rails.git'
    
    gem 'sqlite3'
    
    # Gems used only for assets and not required# in production environments by default.group :assets do
     gem 'sass-rails',   '~> 3.2.3'
     gem 'coffee-rails', '~> 3.2.1'
    
     # See https://github.com/sstephenson/execjs#readme for # more supported runtimes# gem 'therubyracer', :platforms => :ruby
    
     gem 'uglifier', '>= 1.0.3'
    end
    
    gem 'jquery-rails'
    
    group :test do
     gem 'cucumber-rails'
    end
    
    # To use ActiveModelhas_secure_password
    # gem 'bcrypt-ruby', '~> 3.0.0'
    
    # To use Jbuilder templates for JSON
    # gem 'jbuilder'
    
    # Use unicorn as the app server
    # gem 'unicorn'
    
    # Deploy with Capistrano
    # gem 'capistrano'
    
    # To use debugger
    # gem 'debugger'

    Tip

    Downloading the example code

    You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

  3. Run the bundle install in the terminal:

    $ bundler install
    
  4. After the installation is completed, cd into your RoR project directory and run:

    $ rails generate cucumber:install
    
  5. By running this, Cucumber will initialize a folder called features in your Rails project:

  6. Now we create a file under the features folder called hello_world.feature, and write down our first Cucumber test:

    Feature: Learn Cucumber
      As a Software Developer
      I want to learn Cucumber
      So that I can developer in BDD style!
    
      Scenario: Write Hello World Cucumber
        Given I have a Rails project
        When I write a Hello World Cucumber test
        Then I should be able to run it and see "Hello World" printed on screen
  7. And we go to the terminal and run the following Cucumber command:

    $ bundle exec cucumber features/hello_world.feature
    
  8. Now we should see that it fails since we haven't implemented the steps yet. The message should be similar to the following screenshot:

  9. Create a hello_world_steps.rb under the step_definitions directory.

  10. Copy the code shown on the console and paste it to hello_world_steps.rb.

  11. Modify the step code as follows:

    Given /^I have a Rails project$/ do
     puts "Yes, I am at my RoR project."
    end
    
    When /^I write a Hello World Cucumber test$/ do
     puts "Yeah! I just wrote my test"
    end
    
    Then /^I should be able to run it and see "(.*?)" printed on screen$/ do |arg|
     puts arg
    end
  12. Now we run it again and we see it successfully passed:

How it works...

In this simple example, we wrote our first Cucumber feature named "Hello World", it has one scenario, "Write Hello World Cucumber", with three steps. We also implemented three step definitions and successfully made it pass.

In the Cucumber feature, one step is usually started with a preposition or an adverb (Given, When, Then, And, and But), each step is parsed and corresponding to a step definition, in our previous example the last step accepts one argument to be passed in, which means you can put any word in the step, and we passed the string Hello World, so that it is printed on the screen.