technicalpickles

Open Source ProjectsCode that Might be Useful to You

Talks I've GivenOn Technologies and Ideas

ThoughtsWhere I Sometimes Write Things

Resume If You Believe In Those

Follow Me On

GitHubIf coding is your thing

TwitterIf you tweet

Adding Cucumber to a Ruby project


I had announced recently that jeweler now supports generating the files needed to use cucumber in your project. Personally, I’ve always been annoyed when looking for documentation about, for example, adding cucumber to your project, and then come across stuff that is like “Oh hai, just generate a project with jeweler, lol.”

I don’t want to be that guy, so here’s the process I used for adding Cucumber to the testing mix of jeweler.

Background

I’m not going to go any details about actually using Cucumber, so you’ll want to do a little reading ahead of time. I’d recommend the Cucumber website, the Cucumber Wiki on GitHub, and The RSpec Book (currently in beta).

File layout

  • Everything cucumber should live in features directory.
  • Features describing your project live in this features directory
  • features/support/env.rb sets up the world that the features will be run under
  • Steps live in the features/step_definitions directory

Here’s some convient bash one-liner for making the directories:

mkdir -p features/{support,step_definitions}

env.rb

You do have one choice to make here… what framework do you want to use implement your steps? The wiki has instructions for using Test::Unit, MiniTest, and RSpec.

It’s also worth noting that env.rb is the cucumber-equivalent of test_helper.rb or spec_helper.rb, so do any configuration or requireing here. For example you probably want to require your main ruby file from the lib directory. For jeweler, I did:

$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
require 'jeweler'

Rake configuration

You do have a Rakefile, right? Given the file layout above, you can add this snippet:

begin
  require 'cucumber/rake/task'
  Cucumber::Rake::Task.new(:features)
rescue LoadError
  puts "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
end

Summary

Adding cucumber to a project is pretty straightforward, but all the info was never in one place. Hopefully, this article addresses that. Now you can have fun writing your features and step definitions.

comments powered by Disqus