Skip to content
This repository was archived by the owner on Nov 16, 2018. It is now read-only.

Cucumber Backgrounder

byrnejb edited this page Aug 13, 2010 · 551 revisions

Or: How I Learned to Stop Worrying and Love Testing

Introduction

This discussion deals principally with the initial set up and use of cucumber features in a Ruby on Rails (RoR) Project. Discussion of Behaviour Driven (BDD), Test Driven (TDD), and Panic Driven Development (SNAFU) can be found elsewhere. Details regarding installing the Cucumber gem and its recommended support tools for RoR are found at Ruby on Rails.

Where to Start?

Given that you have installed the gem called “Cucumber”
And you have generated a RoR project called “MyProject”
And your session’s working directory is called “MyProject”

When you run “script/generate cucumber”

Then you will create a sub-directory called “features”

The forgoing is written in the style of the feature statements that make up the user interface to cucumber testing. We will return to that topic later for for the moment we deal with the logical arrangement of cucumber files within the context of a RoR project.

An archetypal RoR project directory tree looks like this:

MyProject
|-- README
|-- Rakefile
|-- app
|-- config
|-- db
|-- doc
|-- lib
|-- log
|-- public
|-- script
|-- test
|-- tmp
`-- vendor

Running script/generate cucumber adds this layout to the existing structure:

|-- features
|   |-- step_definitions
|   |   `-- webrat_steps.rb
|   `-- support
|       `-- env.rb

We are now ready to begin testing with cucumber.

Where do I put Tests?

Cucumber divides testing into two parts, the outward facing features and the inward facing steps. As discussed elsewhere, features are descriptions of desired behaviour under predefined conditions and following upon specific events. They are typically used in conjunction with end-user input, and in some cases, may be entirely under end-user (in the form of a domain expert) control. Feature files are given the extension .feature.

Steps, on the other hand, are ruby (.rb) files that contain matchers, snippets of text from the feature files, and blocks of ruby and rails coding together with whatever test system you have installed. Given that cucumber evolved out of RSpec stories it is assumed in the cucumber generator that rspec is available, as is evidenced in the default contents of features/support/env.rb:

# Sets up the Rails environment for Cucumber
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
require 'cucumber/rails/world'
Cucumber::Rails.use_transactional_fixtures

# Comment out the next line if you're not using RSpec's matchers (should / should_not) in your steps.
require 'cucumber/rails/rspec'

Clone this wiki locally