Notes to self

Running test/unit suites with Minitest

Sometimes it happens that you want to run an old test suite, but you don’t have a correct version of the testing framework available. That happens a lot in Fedora since tooling around RPM supports only one version of each component and new rubies does not come with test/unit anymore. As we still need to run the tests, we have two options. Either go ahead and update the whole test suite to a new version or just use Ruby’s dynamic nature to actually run it. Let’s look how the latter can look like.

Here’s an example of running Builder’s tests against latest Ruby 2.2.2 and Minitest 4:

ruby -rminitest/autorun -I.:lib:test - << \EOF
  module Kernel
    alias orig_require require
    remove_method :require

    def require path
      orig_require path unless path == 'test/unit'
    end
  end
  Test = Minitest
  module Test
    class Unit
      class TestCase
        alias :assert_raise :assert_raises
        def assert_nothing_raised
          yield
        end

        def assert_not_nil exp, msg=nil
          msg = message(msg) { "<#{mu_pp(exp)}> expected to not be nil" }
          assert(!exp.nil?, msg)
        end
      end
    end
  end
  Dir.glob "./test/test_*.rb", &method(:require)
EOF

First we need to tell Ruby to require minitest/autorun, which will run our tests using Minitest automatically, and to include the /lib and /test paths on $LOAD_PATH so the requires from the tests itself can require Builder. Afterwards we rewrite the Kernel.require() method so it does not load test/unit. Once we have all that, we are ready to replace Test constant with Minitest one and then include methods like assert_not_nil and assert_nothing_raised that are not part of Minitest 4. We also rename assert_raise to assert_raises.

Finally we iterate over test files and require them. The result is as expected:

$ ruby -rminitest/autorun -I.:lib:test -
Run options: --seed 50227
# Running tests:
.................................................................................................................
Finished tests in 0.016269s, 6945.7173 tests/s, 13153.8363 assertions/s.
113 tests, 214 assertions, 0 failures, 0 errors, 0 skips
Check out my book
Deployment from Scratch is unique Linux book about web application deployment. Learn how deployment works from the first principles rather than YAML files of a specific tool.
by Josef Strzibny
RSS