如何 运行 现有测试代码 Ruby 2.2

How to run existing test code on Ruby 2.2

以下代码(没有 Gemfile)适用于 Ruby 2.1.1,但不适用于 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

在 Ruby 2.1.1 上,我得到

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]

但是在 Ruby 2.2.0 上,我得到

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]

我怀疑这是因为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)

如何使代码在 Ruby 2.2 上运行,最好是代码更改量最少?

在 Ruby 2.2 之前,test/unit 只是 minitest 的薄包装。即使您的测试用例继承自 Test::Unit::TestCase,您实际上使用的是 minitest 断言。因此 skip 有效,这是 minitest.

的一个特征

标准 Ruby 2.2 发行版捆绑了 test-unit 3.0.8minitest 5.4.3,但不包括之前存在的集成。请注意,它们实际上是 2 个独立的测试框架 - 本质上是两个独立的 gem。

我提出 2 个选项:

1。使用 test-unit - rubydoc

您的测试可以保留 Test::Unit::TestCase 基础 class,但您需要调整断言以使用 test-unit 基础。

例如,将skip替换为omit

require "test/unit"

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

2。使用 minitest - rubydoc

您将继续使用您知道的断言,但您需要 require "minitest/autorun" 并将基数 class 切换为 Minitest::Test:

require "minitest/autorun"

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

test-unit backports 已包装成 gem:test-unit-minitest