ruby で unit test
環境
$ ruby -vruby 1.9.3p385 (2013-02-06 revision 39114) [x86_64-linux]
すごくシンプルなテスト
simpleTest.rb
require 'test/unit'
class TC_simple < Test::Unit::TestCase
def test_equal
assert_equal(3, 3)
end
def test_not_equal
assert_not_equal(3, 4)
end
def test_same
assert_same(5, 5)
end
end
実行結果
$ ruby simpleTest.rb Run options: # Running tests: ... Finished tests in 0.000483s, 6212.5821 tests/s, 6212.5821 assertions/s. 3 tests, 3 assertions, 0 failures, 0 errors, 0 skips
別ファイルにある関数に対するunittestのサンプル
maxNum.rb
def maxNum(x, y)
if(x > y)
return x
else
return y
end
end
test.rb
require "test/unit"
require "./maxNum"
class TC_MaxNum < Test::Unit::TestCase
def test_maxNum
assert_equal(3, maxNum(2, 3))
end
end
実行結果
$ ruby test.rb Run options: # Running tests: . Finished tests in 0.001074s, 931.2175 tests/s, 931.2175 assertions/s. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
別ファイルにあるクラスのメソッドに対するunittestのサンプル
maxNum.rb
class MaxNum
def maxNum(x, y)
if(x > y)
return x
else
return y
end
end
end
test.rb
require "test/unit"
require "./maxNum"
class TC_MaxNum < Test::Unit::TestCase
def setup
@obj = MaxNum.new
end
def test_maxNum
assert_equal(3, @obj.maxNum(2, 3))
end
end
実行結果
$ ruby test.rb Run options: # Running tests: . Finished tests in 0.001399s, 715.0457 tests/s, 715.0457 assertions/s. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
注意
テストを実行するメソッドの名前は test で始める参考
test/unitライブラリ
http://doc.ruby-lang.org/ja/1.9.3/library/test=2funit.html
0 件のコメント:
コメントを投稿