---
title: Sharing assertions between Cucumber and Test::Unit
teaser: Integrating testing tools with shared code.
tags: web,testing
author: Jason Morrison
published_on: 2009-10-23
---

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

<object width="425" height="344">
  <param name="movie"
    value="http://www.youtube.com/v/7k0ZMDwmzHI&amp;hl=en&amp;fs=1&amp;" />
  <embed src="http://www.youtube.com/v/7k0ZMDwmzHI&amp;hl=en&amp;fs=1&amp;"
    type="application/x-shockwave-flash" width="425" height="344" />
</object>

<figcaption>
Guinea Pigs Battle for Cucumber: a compelling and entertaining short film
</figcaption>

# 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`:

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

In `test/test_helper.rb`:

```ruby
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`:

```ruby
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](https://thoughtbot.com/blog/post/219216005/fake-it) 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?
