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 require
ing here. For example you probably want to require your main ruby file from the lib
directory. For jeweler, I did:
Rake configuration
You do have a Rakefile
, right? Given the file layout above, you can add this snippet:
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.