leifg
leifg

Reputation: 8988

Rails 2.3 and minitest

I would like to try minitest with an existing Rails 2.3.14 application. I already tried several approaches but none of them seem to work.

It essentially boils down to this. When I add minitest to the Gemfile, all tests I run with bundle exec rake test are NOT executed with minitest.

The problem is that most of the test cases extend the ActiveSupport::Testcase or ActionController::Testcase (which extends ActiveSupport::TestCase).

By looking at the code of this class I saw that it already (somehow) suppports MiniTest:

class TestCase < ::Test::Unit::TestCase
    if defined? MiniTest
      Assertion = MiniTest::Assertion
      alias_method :method_name, :name if method_defined? :name
      alias_method :method_name, :__name__ if method_defined? :__name__
    else
...
end

(see https://github.com/rails/rails/blob/2-3-stable/activesupport/lib/active_support/test_case.rb for complete code)

My question here is:

Is it somehow possible to use minitest instead of test::unit in Rails 2.3 without having to extend (MiniTest::Unit::TestCase)

Upvotes: 4

Views: 991

Answers (1)

Nigel Thorne
Nigel Thorne

Reputation: 21548

Any testing framework is made of two things, a way to define and run tests (call this the test runner) and an assertion framework.

Looking at ActiveSupport it seems that if MiniTest is defined at all, running the rails Unit test runner will default to using MiniTest's assertions classes. This means you get to use Minitest's assertion syntax.

Getting MiniTest defined should just be a case of requiring 'minitest\unit'.

However if you want to use the runners, then you need to derive from the MniTest framework test fixture base classes.

There is a step by step walkthrough in railscasts for setting it up yourself.

It boils down to:

  1. Remove test_unit from your config/applications.rb
  2. Add the minitest gem to your gem file "Gemfile"
  3. Setup the environment in a helper file you can include into your tests
  4. Add a rake task to run the tests. (/lib/tasks/minitest.rake)

Railscasts has the details.

If you don't want to change your existing tests to do that.. then you have to monkey patch Test::Unit to redefine TestCase to either be the chosen MiniTest base class or something that derives from one. This is the approach that this gem takes and may give you some ideas on how best to go about that.

The other solution is to do a search/replace through your code to switch the base classes of the test cases.

I hope this helps.


I Updated my answer and remove the following as out of date:

minitest-rails has rightly been pointed out as it works for Rails 3.1 only.

This gem seems to be old (26th March 2009) but it may help. Check the source code if you need hints to do something similar.

Upvotes: 2

Related Questions