Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run existing test code on Ruby 2.2

The following code (which has no Gemfile) works on Ruby 2.1.1, but not Ruby 2.2.0

require "bundler/setup"
gem "minitest", "4.7.5"
require "test/unit"

class TestFoo < Test::Unit::TestCase
  def test_foo
    assert true, "Useless mesage"
    skip "Skip works"
  end
end

On Ruby 2.1.1, I get

Run options: 

# Running tests:

[1/1] TestFoo#test_foo = 0.00 s
  1) Skipped:
TestFoo#test_foo [test_220.rb:8]:
Skip works

Finished tests in 0.004435s, 225.4791 tests/s, 225.4791 assertions/s.
1 tests, 1 assertions, 0 failures, 0 errors, 1 skips

ruby -v: ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin10.0]

But on Ruby 2.2.0, I get

192-168-1-5:test_220 agrimm$ ruby test_220.rb 
Loaded suite test_220
Started
E
===============================================================================
Error: test_foo(TestFoo)
: NoMethodError: undefined method `skip' for #<TestFoo:0x007fb75484f158>
test_220.rb:8:in `test_foo'
      5: class TestFoo < Test::Unit::TestCase
      6:   def test_foo
      7:     assert true, "Useless mesage"
  =>  8:     skip "Skip works"
      9:   end
     10: end
===============================================================================


Finished in 0.001504 seconds.

1 tests, 1 assertions, 0 failures, 1 errors, 0 pendings, 0 omissions, 0 notifications
0% passed

664.89 tests/s, 664.89 assertions/s
$ ruby --version
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin13]

I suspect that this is because of changes associated with Ruby 2.2.0:

Update test-unit 3.0.8 (removed from repository but bundled in tarball)

Update minitest 5.4.3 (removed from repository but bundled in tarball)

How do I make the code work on Ruby 2.2, preferably with the least amount of code change?

like image 390
Andrew Grimm Avatar asked Dec 05 '22 23:12

Andrew Grimm


1 Answers

Before Ruby 2.2, test/unit was just a thin wrapper over minitest. Even though your test cases inherited from Test::Unit::TestCase, you were in fact using minitest assertions. Hence skip worked, which is a feature of minitest.

The standard Ruby 2.2 distributions bundles test-unit 3.0.8 and minitest 5.4.3, but does not include the integration which existed before. Note that they are in fact 2 separate testing frameworks - essentially two separate gems.

I propose 2 options:

1. Use test-unit - rubydoc

Your tests can keep the Test::Unit::TestCase base class, but you will need to adapt the assertions to use the test-unit ones.

For instance, replace skip with omit:

require "test/unit"

class TestFoo < Test::Unit::TestCase
  def test_foo
    assert true, "Useless mesage"
    omit "Omit works"
  end
end

2. Use minitest - rubydoc

You will keep using the assertions you know, but you will need to require "minitest/autorun" and switch the base class to Minitest::Test:

require "minitest/autorun"

class TestFoo < Minitest::Test
  def test_foo
    assert true, "Useless mesage"
    skip "Skip works"
  end
end
like image 115
czak Avatar answered Jan 06 '23 07:01

czak