Unique Marketing, Guaranteed Results.

Tutorial: How to install/setup Cucumber

April 9th, 2009 by Alan Carl Mitchell

This tutorial assumes that you have ruby, rails, and mysql installed on your machine, and doesn’t explain too much about Cucumber and will mostly show you a quick way to get it working. A lot of material is borrowed from this tutorial on setting up RSpec and Factory Girl. It may be useful to look over first, but not necessary.

Create the Rails Project
Go to your projects directory and create a rails project. Just for fun, let’s call it ‘bank’:

[email protected]:~$ cd projects
[email protected]:~/projects$ rails -d mysql bank
[email protected]:~/projects$ cd bank
[email protected]:~/projects/bank$

Create MySQL Databases
Go into mysql and create the two databases that rails has set up the bank project to use:

[email protected]:~/projects/bank$ mysql -u root -p
mysql>create database bank_development
mysql>create database bank_test
mysql>exit
Bye
[email protected]:~/projects/bank$

Put Gems Into Environments
Edit the files

config/environments/development.rb

and

config/environments/test.rb:

and add the following lines:

config.gem "cucumber", :version => '0.1.16'
config.gem "webrat", :version => '0.4.3'

This sets up the test and development environments to use cucumber. Since cucumber uses webrat to do the actual page navigation, we’ll need it too.

Install the Gems
Install the gems referenced in your environments:

[email protected]:~/projects/bank$ rake gems:install

You’ll see output that says that the gems were installed, if you didn’t have them already.

Bootstrap Cucumber
Do a:

[email protected]:~/projects/bank$ script/generate cucumber

You will see the following:

[email protected]:~/projects/bank$ script/generate cucumber
create features/step_definitions
create features/step_definitions/webrat_steps.rb
create features/support
create features/support/env.rb
create features/support/paths.rb
exists lib/tasks
create lib/tasks/cucumber.rake
create script/cucumber
[email protected]:~/projects/bank$

This generates some default cucumber files and directories that the project will use.

Generate Basic Files with scaffold
Do a:

[email protected]:~/projects/bank$ script/generate scaffold BankAccount

You will see the following output:

[email protected]:~/projects/bank$ script/generate scaffold BankAccount
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/bank_accounts
exists app/views/layouts/
exists test/functional/
exists test/unit/
create test/unit/helpers/
exists public/stylesheets/
create app/views/bank_accounts/index.html.erb
create app/views/bank_accounts/show.html.erb
create app/views/bank_accounts/new.html.erb
create app/views/bank_accounts/edit.html.erb
create app/views/layouts/bank_accounts.html.erb
create public/stylesheets/scaffold.css
create app/controllers/bank_accounts_controller.rb
create test/functional/bank_accounts_controller_test.rb
create app/helpers/bank_accounts_helper.rb
create test/unit/helpers/bank_accounts_helper_test.rb
route map.resources :bank_accounts
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
skip app/models/bank_account.rb
create test/unit/bank_account_test.rb
create test/fixtures/bank_accounts.yml
exists db/migrate
Another migration is already named create_bank_accounts: db/migrate/20090402160952_create_bank_accounts.rb
[email protected]:~/projects/bank$

If you haven’t done the tutorial on setting up RSpec and Factory Girl, then your output will look different to reflect the fact that most of the files that scaffold tried to create didn’t already exist.

Create the .feature File
Create

features/bank_account.feature

and fill it with:

Feature: Manage bank accounts
  In order to manage bank accounts and retain customers
  Bank Managers
  want to be able to manipulate bank accounts

  Scenario: See all bank accounts
    When I go to bank_accounts
    Then I should see "Listing bank_accounts"

Something you should know is that Cucumber will try to match each line of each scenario to a step definition. In other words, you can’t just put whatever you want in the scenario. If I put “When I go to the page bank_accounts” then that would not work because there is no step definition that matches that pattern. To see the step definitions, go to

features/step_definitions/webrat_steps.rb

and notice the line that says:

When /^I go to (.+)$/ do |page_name|
  visit path_to(page_name)
end

This is what will make my “When I go to bank_accounts” in my scenario work. If what is in my scenario doesn’t match one of the step definitions, it won’t work. You can’t just put whatever you want into the scenario unless you have made a step definition for it.

Note: If you are using Netbeans, check out this post about a plugin that will put color into your .feature files

Run Cucumber (unsuccessfully) and Then Fix
Do a:

[email protected]:~/projects/bank$ rake features

and you should see

[email protected]:~/projects/bank$ rake features
(in /home/user/projects/bank)
/usr/bin/ruby1.8 -I “/usr/lib/ruby/gems/1.8/gems/cucumber-0.1.16/lib” “/usr/lib/ruby/gems/1.8/gems/cucumber-0.1.16/bin/cucumber” –format pretty –require features/support/env.rb –require features/support/paths.rb –require features/step_definitions/webrat_steps.rb features/bank_account.feature
Feature: Manage bank accounts # features/bank_account.feature
In order to manage bank accounts and retain customers
Bank Managers
want to be able to manipulate bank accounts
Scenario: See all bank accounts                     # features/bank_account.feature:6
When I go to bank_accounts                          # features/step_definitions/webrat_steps.rb:6
Can’t find mapping from “bank_accounts” to a path. (RuntimeError)
/home/osadmin/projects/bank_account/bank/features/support/paths.rb:10:in `path_to’
./features/step_definitions/webrat_steps.rb:7:in `When /^I go to (.+)$/’
features/bank_account.feature:7:in `When I go to bank_accounts’
Then I should see “Listing bank_accounts # features/step_definitions/webrat_steps.rb:89

1 scenario
1 step failed
1 step skipped
rake aborted!
Command failed with status (1): [/usr/bin/ruby1.8 -I “/usr/lib/ruby/gems/1….]

(See full trace by running task with –trace)
[email protected]:~/projects/bank$

What this is complaining about is in the scenario when it says “When I go to bank_accounts” it doesn’t know where the bank_accounts page is. The paths are defined in the file

features/support/paths.rb

Open it up and change it to:

def path_to(page_name)
  case page_name
  
  when /the homepage/i
    root_path
  when /bank_accounts/i
    bank_accounts_path
  # Add more page name => path mappings here
  
  else
    raise "Can't find mapping from \"#{page_name}\" to a path."
  end
end

Notice that we added another path mapping for bank_accounts. It already had one for the homepage, meaning in our scenario we could have put “When I go to the homepage” and it would have worked on that line of the scenario.

Run Cucumber (successfully)
Do a:

[email protected]:~/projects/bank$ rake features

and you should see

[email protected]:~/projects/bank$ rake features
(in /home/user/projects/bank)
/usr/bin/ruby1.8 -I “/usr/lib/ruby/gems/1.8/gems/cucumber-0.1.16/lib” “/usr/lib/ruby/gems/1.8/gems/cucumber-0.1.16/bin/cucumber” –format pretty –require features/support/env.rb –require features/support/paths.rb –require features/step_definitions/webrat_steps.rb features/bank_account.feature
Feature: Manage bank accounts # features/bank_account.feature
In order to manage bank accounts and retain customers
Bank Managers
want to be able to manipulate bank accounts
Scenario: See all bank accounts                      # features/bank_account.feature:6
When I go to bank_accounts                           # features/step_definitions/webrat_steps.rb:6
Then I should see “Listing bank_accounts # features/step_definitions/webrat_steps.rb:89

1 scenario
2 steps passed
[email protected]:~/projects/bank$

There, you’ve not got Cucumber working and can (hopefully) start writing other tests for it.

Filed under: Programming,Tutorials — Tags: , , — Alan Carl Mitchell @ 10:32 am on April 9, 2009

4 Comments

  1. […] Tutorial: How to install/setup Cucumber, Alan Mitchell […]

    Pingback by Robby on Rails : 20 articles on Cucumber and a free beverage recipe! — April 9, 2009 @ 8:32 pm

  2. […] How to Install Cucumber | PMA Media Group (tags: rubyonrails cucumber bdd installation) […]

    Pingback by Mark’s Link Blog » links for 2009-06-09 — June 9, 2009 @ 11:06 pm

  3. how to install gherkin runtime…
    where to get that runtime
    it asks for gherkin runtime while installing cucumber gem ….

    Comment by valan lsl — September 20, 2010 @ 2:58 am

  4. Awesome guide!!! Made my day and month!

    Comment by Shashank — December 22, 2010 @ 5:52 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Copyright © 2005-2016 PMA Media Group. All Rights Reserved &nbsp