Rubyのtest-unitをEclipseで使う

今までコマンドラインでは使っていたのですが,周囲にRubyを広めるためにEclipse化しました.

まずEclipse自体は普通にインストール.
次にこの辺をいれる.

本家
http://www.eclipse.org/dltk/


適当にググったら出たページ
kkaneko.com
スクリプト言語をサポートするEclipseプラグイン (2/3):CoolなEclipseプラグイン(10) - @IT

ただし,

Rubyスクリプトを選択、右クリックし[実行]→[Ruby Application]を選択すると、スクリプトを実行することができます

とありますが,今の私の環境では[Ruby Script]と出ています.
Eclipseバージョンアップのせいか,RDTのバージョンアップのせいかは分かりません.

gemでtest-unitを入れます.
ただし私のFreeBSD環境では,Rubyは1.9がデフォルトになっていて,Ruby2系はRuby20みたいな名前でportsインストールされているので,新しい方を使いたいということでインストール先を指定します.

# gem install -i /usr/local/lib/ruby/gems/2.0 test-unit
Fetching: test-unit-2.5.5.gem (100%)
Successfully installed test-unit-2.5.5
1 gem installed
Installing ri documentation for test-unit-2.5.5...
Installing RDoc documentation for test-unit-2.5.5...

ベタベタなテストプログラムを作ります.

class Main
  def main
    return "Hello"
  end
end

単体テストも作ります.

require 'test/unit'
require './src/Main'

class TEST_Main < Test::Unit::TestCase
  def setup
    @obj = Main.new();
  end
  def test_normal
    assert_equal("Hello", @obj.main());
  end
  def test_abnormal
    assert_equal("hello", @obj.main());
  end
end

テストプログラムを,Ruby Scriptとして実行します.

Run options: 

# Running tests:

F.

Finished tests in 0.002452s, 815.6174 tests/s, 815.6174 assertions/s.

  1) Failure:
test_abnormal(TEST_Main) [/home/foo/workspace/Test/test/test_Main.rb:26]:
<"hello"> expected but was
<"Hello">.

2 tests, 2 assertions, 1 failures, 0 errors, 0 skips

ruby -v: ruby 2.0.0p353 (2013-11-22 revision 43784) [amd64-freebsd9]

普通にコマンドラインで実行した結果が出ます.

テストプログラムをRuby Testとして実行します.

/home/foo/.eclipse/org.eclipse.platform_3.7.0_946975857/configuration/org.eclipse.osgi/bundles/215/1/.cp/testing/dltk-testunit-runner.rb:252:in `block in <top (required)>': uninitialized constant Test::Unit::UI::SILENT (NameError)

これはメニューから選択したり実行ボタンのアイコンを押したり,Script Testの実行ボタンを叩いても同じです.
Script Testのメニューは,よーーーく見ると,「Terminated」って出ています.

余りにも寂しい.

解決方法は以下.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=323736

 # emacs -nw /home/foo/.eclipse/org.eclipse.platform_3.7.0_946975857/configuration/org.eclipse.osgi/bundles/215/1/.cp/testing/dltk-testunit-runner.rb

#                       autoRunner.output_level = Test::Unit::UI::SILENT        
                        autoRunner.runner_options[:output_level] = Test::Unit::UI::Console::OutputLevel::NORMAL

もう一つの解決法も出てますが(RubyのMixinを使うヤツ),テストプログラムが複数になるとうまく動きませんでした.
なので,Eclipseと一緒にインストールしたプログラム自体を書き換えてしまうのがオススメ.
その結果.

ファンタスティック!

コンソール表示も変わって,

Loaded suite /home/foo/workspace/Test/test/test_Main
Started
F
===============================================================================
Failure:
test_abnormal(TEST_Main)
/home/foo/workspace/Test/test/test_Main.rb:26:in `test_abnormal'
     23:     assert_equal("Hello", @obj.main());
     24:   end
     25:   def test_abnormal
  => 26:     assert_equal("hello", @obj.main());
     27:   end
     28: end
/home/foo/.eclipse/org.eclipse.platform_3.7.0_946975857/configuration/org.eclipse.osgi/bundles/215/1/.cp/testing/dltk-testunit-runner.rb:77:in `start'
<"hello"> expected but was
<"Hello">

diff:
? hello
? H    
===============================================================================
.

Finished in 0.006754847 seconds.

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

296.08 tests/s, 296.08 assertions/s

分かりやすくなります.