“Testing is like eating your vegetables; you do it because you know it’s good for you, not because you plan on enjoying a second of it.” – Antiquated Developer

Sure, this may have been true in the past, but not anymore, thanks to the aptly named testing framework, Cucumber.

 Unlike most testing frameworks, Cucumber doesn’t cause users to cringe with each bite. It’s easy to digest, appealing to the palate, and a simple addition to nearly any project.

 Cucumber is a behavior-driven testing framework originally written in Ruby and ported to other languages. It bridges the gap between software developers and business managers by using tests written in natural language (Gherkin). This makes Cucumber understandable to any layman. The following is a simple feature written in Gherkin.

1
2
3
4
5
6
7
8
9
Feature: 01 View Initial Load Page
  In order to schedule an event
  As an event creator
  I want to view the initial page
 
  Scenario: Show event form
    Given I am on the landing page
    And I click Create An Event
    Then I should see "EVENT INFO"

Cucumber definitions have two sections, the optional feature description section and the feature behavior section. The feature description has three parts: the “In order” line, which states our overall goal, the “As a” line, which defines a condition, and the “I want” line, which specifies the feature we want to implement.

The second section is where users define the behavior of the feature; this is accomplished by writing one or more scenarios. Much like the feature description, a scenario is written in natural language using “Given”, “When”, and “Then”. In the above example, the written scenario defines the behavior of the application when “Create An Event” is clicked.

 With the feature written cucumber can run for the first time:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ cucumber features/view_page.feature
 
Feature: 01 View Initial Load Page
  In order to schedule an event
  As an event creator
  I want to view the initial page
 
  Scenario: Show event form        # features/01_viewInitialLoadPage.feature:6
    Given I am on the landing page # features/01_viewInitialLoadPage.feature:7
    And I click Create An Event    # features/01_viewInitialLoadPage.feature:8
    Then I should see "EVENT INFO" # features/01_viewInitialLoadPage.feature:9
 
1 scenario (1 undefined)
3 steps (3 undefined)
0m0.002s
 
You can implement step definitions for undefined steps with these snippets:
 
Given(/^I am on the landing page$/) do
  pending # express the regexp above with the code you wish you had
end
 
Given(/^I click Create An Event$/) do
  pending # express the regexp above with the code you wish you had
end
 
Then(/^I should see "(.*?)"$/) do |arg1|
  pending # express the regexp above with the code you wish you had
end

 Cucumber shows us that the three steps written earlier have not been executed because they were undefined. This means that step definitions need to be written in Ruby so Cucumber will know how to run the Gherkin tests. Luckily, Cucumber provides code templates to get users started.

Each step is defined as a regular expression. This allows users to take the steps written in Gherkin and manipulate them using Ruby code (or Java/Javascript depending on what port of Cucumber being used).

1
2
3
4
5
6
7
8
9
10
11
12
13
$ vi features/step_definitions/web_steps.rb
 
Given(/^I am on the landing page$/) do
  visit "http://localhost:9000/index.html"
end
 
Given(/^I click "(.*?)"$/) do |link|
  click_on(link)
end
 
Then(/^I should see "(.*?)"$/) do |text|
  page.should have_content(text)
end

To reduce repetition it is important to modify the step definitions to allow multiple sub expressions. For example, it is possible to write Gherkin steps “I click Create an Event”, “I click Upcoming Events”, and “I click Completed Events” and they will all be handled by the one step definition shown above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ cucumber features/view_page.feature
 
Feature: 01 View Initial Load Page
  In order to schedule an event
  As an event creator
  I want to be view the initial page
 
  Scenario: Show event form        # features/01_viewInitialLoadPage.feature:6
    Given I am on the landing page # features/step_definitions/web_steps.rb:46
    And I click Create An Event    # features/step_definitions/web_steps.rb:185
    Then I should see "EVENT INFO" # features/step_definitions/web_steps.rb:19
 
1 scenario (1 passed)
3 steps (3 passed)
0m4.578s

When running Cucumber again, all three of the steps have passed, providing the confidence that this specific feature and scenario is working as it should. The beauty of Cucumber is testing the user experience of an application from the outside in. This allows tests of everything from the UI all the way down to the database, using simple, natural language and the definitions in Ruby. It may be a framework named after a vegetable, but it sure doesn’t taste like one, making any testing a lot more palatable.

 For more in depth examples of Cucumber in action, or how to implement Cucumber for an upcoming or current project, try these links:

Cucumber site for general information: http://cukes.info/

Railscast on Cucumber: http://railscasts.com/episodes/155-beginning-with-cucumber

Standalone Cucumber Test Suite: https://github.com/thuss/standalone-cucumber