Skip to content

What is Cucumber? What is Test Driven Design?

DeeDeeG edited this page May 1, 2017 · 1 revision

Cucumber is a Test Driven Design testing framework.

It is written in a human-language-like way.

For example, here is our search.feature file:

@javascript
Feature: Search for restrooms

  Scenario: Text search from splash page
    Given a restroom exists in Winnipeg
    When I am on the splash page
    And I search for "Winnipeg"
    Then I should see a restroom

  Scenario: Location search from splash page
    Given a restroom exists in Winnipeg
    When I am on the splash page
    And I search from Vancouver
    Then I should not see a restroom

  Scenario: Map display
    Given a restroom exists in Winnipeg
    When I am on the splash page
    And I search from Winnipeg
    And I show the map
Then I should see a restroom on the map

And here are the definitions for some part of that English-like test, found in search_steps.rb:

When(/^I search for "(.*?)"$/) do |search|
  fill_in 'search', with: search
  find('.submit-search-button').click
end

When(/^I search from (.*?)$/) do |location|
  mock_location location
  find('.current-location-button').click
end

This example simulates searching for a location in two different ways, and simulates searching from two different cities.

Some parts of the test functionality (such as mock_location location) are defined in other files than are shown here. (e.g. locations.rb) To understand the full test functionality, find and open the file that defines each part of the test.

Basically, Cucumber takes phrases written in a human language (the "main test file"), and does find-and-replace, to replace bits of the main test file with Ruby code you define in another file (your "test steps" file). Theoretically, this lets you write Ruby code once, to define the underlying functionality of tests, then write all the tests in a human language, e.g. English. This helps non-technical users have an idea what the tests are for, and what isn't working if the test fails.

As you can most-likely tell from this explanation, Cucumber is a Ruby-based testing framework.