Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Atalanta/842165 to your computer and use it in GitHub Desktop.
Save Atalanta/842165 to your computer and use it in GitHub Desktop.

Anatomy of a Cucumber test suite

features/                  # Base directory for all your features
features/step_definitions/ # Generic steps used across multiple features
features/support/          # Cucumber bootstrapping directory

On boot, Cucumber will load up features/support/env.rb. This is where you should include all the libraries you want to use. So features/support/env.rb doesn't get cluttered, it's suggested you split out your customisations into separate files under features/support/.

cucumber-nagios's env.rb looks like this:

 1 #!/usr/bin/env ruby
 2
 3 $: << File.expand_path(File.dirname(__FILE__))
 4
 5 require 'cucumber/nagios'
 6 require 'webrat/adapters/mechanize'
 7 require 'webrat_logging_patches'
 8
 9 class ResponseHelper
10   def response
11     webrat_session.response
12   end
13 end
14
15 World do
16   ResponseHelper.new
17   Webrat::Session.new(Webrat::MechanizeAdapter.new)
18 end

Line 3 is just appending the directory the current file is in to the load path. This is needed so the require on Line 7 works.

Lines 5-7 are requiring bits of cucumber-nagios and webrat. These libraries are purely optional and not required by Cucumber. For example, if you were testing command line tools you'd require 'aruba'

Lines 9-13 are creating a class that contains a helper method which will be used later on in Lines 15-18.

Lines 15-18 are passing a bunch of objects to World. World is a self contained namespace that your steps run in. Instantiating objects in a World block will delegate method calls to them. For example...

class FoobarHelper
  def wangity
    "boing!"
  end
end

World do
  FoobarHelper.new
end

... will make the wangity method available in all your steps:

Given /^we are flying to the moon$/ do
  wangity.should == "boing!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment