Sharing assertions between Cucumber and Test::Unit

Jason Morrison

YO CUCUMBER, imma let you finish but according to rake stats, my units have the best collection of hand-rolled assertions of ALL TIME.

Guinea Pigs Battle for Cucumber: a compelling and entertaining short film

How to share with Cucumber

The gist is this: declare your assertions in a TestSupport::Assertions module, inside test/support – feel free to split them up into multiple files, to keep things organized. Then, both Test::Unit and Cucumber will load those files up and include the modules as appropriate:

In features/support/env.rb:

Dir[File.join(RAILS_ROOT, 'test', 'support', '*.rb')].each { |f| require f }
World(TestSupport::Assertions)

In test/test_helper.rb:

Dir[File.join(RAILS_ROOT, 'test', 'support', '*.rb')].each { |f| require f }
class Test::Unit::TestCase
  include TestSupport::Assertions
end

In test/support/stuff_assertions.rb:

module TestSupport
  module Assertions
    def assert_stuff(params)
      assert_match /stuff/, params
    end
  end
end

Ker-DRYed up code!

Letters from viewers

I hear from people who know about testing that this approach to sharing between units and Cucumber is de-facto in RSpec.

So, what do you like to share between Cucumber and your unit tests?