Test::Unit and MiniTest with different Ruby versions


In recent weeks, I’ve been trying to ensure that Mocha works with the new versions of Ruby as well as MiniTest, the Test::Unit replacement. I started getting very confused with all the different ways to write/run tests, so I made some notes for myself. In the wake of the release of Ruby 1.9.1, I thought these notes might be useful to others – so here they are.

I haven’t got into the MiniTest::Spec or MiniTest::Mock side of things, nor have I looked at other testing libraries like RSpec, Shoulda, Test/Spec, etc. Please let me know if you see any errors or omissions and I’ll update this article.

I’ve included the information about Ruby 1.9.0 for completeness, but most people will only be interested in the sections on 1.9.1 which is the “first stable release of Ruby 1.9 series”.

As I mentioned in the release notes, Mocha 0.9.5 should work with Test::Unit or MiniTest in Ruby 1.8.* or Ruby 1.9.1. It does not currently work in Ruby 1.9.0, but I won’t be making this a priority unless I have specific requests to do so.

Note that at some point MiniTest changed its name from MiniUnit.

Ruby 1.8 with Test::Unit standard library

The basics.

require 'test/unit'

class Ruby18TestUnitTest < Test::Unit::TestCase

Ruby 1.8 with MiniTest gem

If you can’t upgrade to Ruby 1.9, you can still use MiniTest by installing the gem. Note that you have to install the autorun exit hook manually for MiniTest – this is done automatically for Test::Unit when you require ‘test/unit’ – if you want the tests to be executed when the file run as a Ruby script.

# gem install minitest

require 'rubygems'
gem 'minitest'
require 'minitest/unit'


class Ruby18MiniTestGemTest < MiniTest::Unit::TestCase

Ruby 1.8 with MiniTest-Test::Unit shim

This takes things a step further by installing a shim gem which makes requiring ‘test/unit’ behave the same way as it does in a vanilla Ruby 1.9.1 installation i.e. Test::Unit becomes a thin wrapper around MiniTest. This means that you don’t have to change your tests to inherit from MiniTest::Unit::TestCase in order to use MiniTest. However, you should note that some of Test::Unit’s API (e.g. Test::Unit::TestResult) is no longer available. Note that you can reverse the effect of use_minitest yes using use_minitest no.

# sudo gem install minitest_tu_shim
# sudo use_minitest yes

require 'rubygems'
gem 'minitest'
require 'test/unit'

class Ruby18MiniTestTUShimTest < Test::Unit::TestCase

Ruby 1.9.0 with Test::Unit standard library

The Test::Unit standard library seems to be unchanged.

require 'test/unit'

class Ruby190TestUnitTest < Test::Unit::TestCase

Ruby 1.9.0 with Mini::Test standard library

An earlier version of MiniTest (i.e. Mini::Test) is also available as a standard library. Note the different directory, file, class & module names.

require 'mini/test' # c.f. require 'minitest/unit' in Ruby 1.9.1


class Ruby190MiniTestTest < Mini::Test::TestCase

Ruby 1.9.1 with Test::Unit standard library

Test::Unit is just a thin wrapper around MiniTest as described in Ruby 1.8 with MiniTest-Test::Unit shim

require 'test/unit'

class Ruby191TestUnitTest < Test::Unit::TestCase

Ruby 1.9.1 with MiniTest standard library

MiniTest is intended to be the default standard library to use for testing.

require 'minitest/unit'


class Ruby191MiniTestTest < MiniTest::Unit::TestCase

Ruby 1.9.1 with Test::Unit gem

It is possible to use the classic Test::Unit by installing the gem. It looks like you need to specify version 1.2.3 to avoid picking up what appears to be some more significant changes in versions >= 2.0.

# sudo gem install test-unit -v 1.2.3

require 'rubygems'
gem 'test-unit'
require 'test/unit'

class Ruby19TestUnitGemTest < Test::Unit::TestCase

Versions used for testing

  • OSX Leopard 10.5.6
  • Ruby 1.8.6 (2008-08-11 patchlevel 287)
  • Ruby 1.9.0 (2008-10-04 revision 19669)
  • Ruby 1.9.1p0 (2009-01-30 revision 21907)
  • minitest gem 1.3.1
  • minitest_tu_shim gem 1.3.0
  • test-unit gem 1.2.3