Home > Articles > Programming > Ruby

  • Print
  • + Share This
From the author of

From the author of

Cucumber

Cucumber

Behavior Driven Development means that before writing code, a developer should figure out the requirements of the feature that is going to be developed. Then tests are written to validate these requirements, and only then, as the final step, the feature code is written.

Cucumber is an innovative testing framework that takes BDD one step further. It provides a simple way to allow the ones who are responsible for writing the requirements to write them in a language called Gherkin, which is plain English with a few formatting rules. The developer, in turn, writes code that interprets these requirements and converts them into real tests.

This removes an entire step from the software development process—the step where the developer writes tests according to requirements and changes them when the requirements change. Cucumber enables using the same requirement documents for documentation purposes and tests as well.

For example, the next Gherkin document specifies some simple requirements for the DayNightHelper class. It should be saved in a folder named “features” and in a file with an extension “feature”:

Feature: Helper class to determine day and night time
  As a regular user
  I want to know whether it is day or night
  To do stuff differently accordingly
	  
  Scenario: 15:00 is day time
    Given the time is 01/01/2010 15:00
    When I check if it is daytime
    Then the result should be true
  Scenario: 15:00 is not nighttime
    Given the time is 01/01/2010 15:00
    When I check if it is nighttime
    Then the result should be false

Notice the special format; the document starts with the feature title and description, followed by multiple scenarios. Every scenario contains a title and scenario steps that consist of the format Given-When-Then. These are the heart of any scenario: Given describes the prerequisites for the scenario, When describes the behavior that is being tested, and Then describes the expected result.

Interpreting this requirement document is made easy with the Cucumber framework. The code that does this is provided with built-in methods named Given, When and Then, which interpret the matching scenario step. The interpretation is performed with regular expressions.

Every method does its own interpretation and saves data that is needed for later steps in a class variable (which can be identified by an at symbol, @, in front of it). The Then method, in turn, should also compare the expected result and the actual one. This comparison, by default, is done with the should method (similar to RSpec).

For example, the next code interprets the feature document from the last sample and converts it into real tests of the DayNightHelper class. The file should be saved in the same folder of the feature file (or in a subfolder named “step_definitions”):

require "time"
require "DayNightHelper.dll"

# Notice how every regular expression variable
# like (.*) is sent to the code block (here as 
# a parameter named time)
Given /the time is (.*)/ do |time|
  @param_value = Time.parse(time.to_s)
end

When /I check if it is daytime/ do
  instance = CustomTools::DayNightHelper.new
  @result = instance.is_day(@param_value)
end

When /I check if it is nighttime/ do
  instance = CustomTools::DayNightHelper.new
  @result = instance.is_night(@param_value)
end

Then /the result should be (.*)/ do |expected_result|
  @result.to_s.should == expected_result.to_s
end

Running it is done via the command line with the icucumber tool (which is the IronRuby’s equivalent for Ruby’s cucumber tool). Make sure to execute the command from the parent folder of the features folder:

>icucumber --no-source
Feature: Helper class to determine day and night time
  As a regular user
  I want to know whether it is day or night
  To do stuff differently accordingly

  Scenario: 15:00 is day time
    Given the time is 01/01/2010 15:00
    When I check if it is daytime
    Then the result should be true

  Scenario: 15:00 is not nighttime
    Given the time is 01/01/2010 15:00
    When I check if it is nighttime
    Then the result should be false

2 scenarios (2 passed)
6 steps (6 passed)
0m0.413s

This is just the tip of the iceberg regarding the Cucumber framework. For more information, you can visit Cucumber’s official site.

  • + Share This
  • 🔖 Save To Your Account