Sharing assertions between Cucumber and Test::Unit

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:

[~/dev/app] tail -n 2 features/support/env.rb
Dir[File.join(RAILS_ROOT, 'test', 'support', '*.rb')].each { |f| require f }

[~/dev/app] tail -n 4 test/test_helper.rb
Dir[File.join(RAILS_ROOT, 'test', 'support', '*.rb')].each { |f| require f }
class Test::Unit::TestCase
  include TestSupport::Assertions

[~/dev/app] cat test/support/stuff_assertions.rb
module TestSupport
  module Assertions
    def assert_stuff(params)
      assert_match /stuff/, params

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?


Sharpen your programming skills by completing coding exercises that are reviewed by other developers at Upcase today.