Wednesday, April 29, 2009

Using Cucumber Testing Framework without Rails

Most of the articles for Cucumber are given as Rails examples. While Cucumber was born for Rails testing, it isn't limited to that environment. A few simple steps will allow you to use Cucumber with any Ruby project.

This example will use Rake to run cucumber because the Cucumber::Rake::Task does a lot of work for you. It will automatically load (require) the features and steps for you.

First, you should follow Cucumber's pattern for directories:




{project home}/featuresstores all the .feature files
{project home}/features/step_definitionsstores the .rb files containing steps
{project home}/features/supportcontains the env.rb file


First create a rakefile for your project and add a Cucumber::Rake::Task as follows...



Next create the features file(s). The rake task will automatically require all the .feature files in the features directory. This example reproduces the additions.feature shown on Cucumber's homepage.



At this time, you may run "rake features" to see Cucumber runs. The results will show that 4 steps are undefined. To define the steps create features\step_definitions\addition_steps.rb. The rake task will automatically require all the step_definitions files.



This example uses a class Calculator from my project. So lets define that class under lib\calculator.rb.



This calculator isn't very exciting. It just uses a stack to remember values. The first item in the stack is the "screen". The add method will add the top two values. This is the bare minimum needed to meet the defined feature. (Normally you would define the class and fill in the details in a test-first fashion. But to keep this article short, I've posted the completed class.)

The rake features task doesn't automatically require the calculator.rb file. Also, if you noticed the addition_steps.rb makes use of rspec expectations, which isn't available by default. To require these files, create the features/support/env.rb file. The rake features task will automatically require any file under features/support



Now when you run "rake features" everything will work together. Your steps should all run and pass with green color!

Saturday, April 04, 2009

TofuHash is done!

I've released TofuHash v1.0 which supports the entire Hash interface for both Ruby 1.8.x and Ruby 1.9.1.

On Monday April 6, I'll be presenting a lightning talk on TofuHash to Rubyham. The talk will mostly cover the ruby 1.9.1 issues encountered.