Starting With Cucumber

Cucumber is a testing tool that runs acceptance tests. It was developed to ease the collaboration between non-coders and programmers as its syntax is written language.

For example, it would be more understandable if you showed your client or project manager that you are testing this scenario for a feature:


Feature: Playing Bowling
 As a player
 I need to be able to know my score
 So that I can keep track of my awesomeness at bowling 

 Scenario: Not Hitting Any Pins
  Given I am a player
  When I hit no pins
  Then my score is still zero

rather than this:


require 'bowling'

describe Bowling, "#score" do
  it "returns 0 for all gutter game" do
    bowling = Bowling.new
    20.times { bowling.hit(0) }
    bowling.score.should eq(0)
  end
end

The first one reads better for non-technical folks and when you eventually show that all tests are passing (or failing) they will know exactly what you tested for.

Interested to know how to implement Cucumber in your Rails application? Let us get you started:

In your Gemfile write:


group :development, :test do
  gem "rspec-rails", ">= 2.0.1"
  gem 'cucumber-rails'
  gem 'database_cleaner'
end

Don’t forget to set up with $ bundle install and $ rails g cucumber:install.

On your Rails project, Cucumber tests stay at features folder on .feature extension files.

Let’s create a show_my_cucumber_love.feature within the features folder.

In it, type:


Feature: 
 As a cucumber lover
 I want to express my feelings
 So the whole world knows it
  
 Scenario: Visiting the Cucumber Page
   Given I am a visitor
   When I go to the cucumber page
   Then I should see I love cukeys

Indentation matters here. Make sure you have them right. As you can see the The scenario is composed by many steps and each step is validation or a test you are going to write. If you are curious about what format it is, this is Gherkin. Gherkin is a Business Readable Domain Specific Language that describes a softwareʼs behavior without defining how the behavior is implemented.
“Given” steps are used to create context and perform setup necessary for the “Scenario”. “When” steps define the action that is taken as a part of the “Scenario”. “Then” is where you verify the expected results. “Given”, “When”, and “Then” can have additional steps that begin with “And”.

Now it is time to run the script. Go to your terminal and type cucumber.

You should see something like this:


Feature: 
 As a cucumber lover
 I want to express my feelings
 So the whole world knows it
  
 Scenario: Visiting the Cucumber Page
   Given I am a visitor
   When I go to the cucumber page
   Then I should see I love cukeys

1 scenario (1 undefined)
3 steps (3 undefined)
0m0.232s

You can implement step definitions for undefined steps with 
these snippets:

Given /^I am a visitor$/ do
 pending # express the regexp above with the code you wish …
end
When /^I go to the cucumber page$/ do
pending # express the regexp above with the code you wish …
end
Then /^I should see I love cukeys
 pending # express the regexp above with the code you wish …
end

Do you see the line with “You can implement set definitions …”? This is Cucumber telling us that the ... _steps file has not been created. Inside features/step_definitions/ folder, create show_my_cucumber_love_steps.rb and in it type:


Given /^I am a visitor$/ do
end

When /^I go to the cucumber page$/ do
visit root_path
end

Then /^I should see I love cukeys
 expect(page).to have_content("I love cukeys")
end

If you run “cucumber” again in your shell, now you should see your tests fail.
Let’s make them pass. Create your controllers “rails g controller”, add an empty index in it just for the heck of it. Create also a view and write “I love cukeys” in it. Set this view as your root path in your Rails config/routes.rb file, and run “cucumber” again. Now you should see your tests pass.

Congratulations! You have just performed your first acceptance test using Cucumber.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s