<URL:http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/index.html>[外部]
テスト対象のソース(foo.rb)
class Foo def foo "foo" end def bar "foo" end end
テスト(footest.rb)
require 'test/unit' require 'foo' class TC_Foo < Test::Unit::TestCase def setup @obj = Foo.new end # def teardown # end def test_foo assert_equal("foo", @obj.foo) end def test_bar assert_equal("bar", @obj.bar) end end
結果
すべてをテスト
$ ruby footest.rb Loaded suite footest Started F. Finished in 0.022223 seconds. 1) Failure!!! test_bar(TC_Foo) [footest.rb:16]: <bar> expected but was <foo> 2 tests, 2 assertions, 1 failures, 0 errors
test_bar だけテスト
$ ruby footest.rb --name=test_bar Loaded suite footest Started F Finished in 0.019573 seconds. 1) Failure!!! test_bar(TC_Foo) [footest.rb:16]: <bar> expected but was <foo> 1 tests, 1 assertions, 1 failures, 0 errors
gtk を使った testrunner
$ ruby footest.rb --runner=gtk --name=test_bar
fox を使う
$ ruby footest.rb --runner=fox --name=test_bar
console を使う (default)
$ ruby footest.rb --runner=console --name=test_bar
help
$ ruby footest.rb --help Usage: footest.rb [options] [-- untouched arguments] -r, --runner=RUNNER Use the given RUNNER. (c[onsole], f[ox], g[tk], g[tk]2, t[k]) -n, --name=NAME Runs tests matching NAME. (patterns may be used). -t, --testcase=TESTCASE Runs tests in TestCases matching TESTCASE. (patterns may be used). -v, --verbose=[LEVEL] Set the output level (default is verbose). (s[ilent], p[rogress], n[ormal], v[erbose]) -- Stop processing options so that the remaining options will be passed to the test. -h, --help Display this help.
複数のテストを一度に行う場合、以下のように書いただけのファイルを実行する。
require 'test/unit' require 'footest.rb' require 'bartest.rb'
各 assertion の最後の引数 message はテストが失敗したときに表示される メッセージ
assert(boolean, message="")
boolean が真なら pass
assert_equal(expected, actual, message=nil)
expected == actual ならば pass
assert_not_equal(expected, actual, message="")
expected != actual ならば pass
assert_instance_of(klass, object, message="")
klass == object.class が真なら pass
assert_nil(object, message="")
object.nil? ならば pass
assert_not_nil(object, message="")
!object.nil? ならば pass
assert_kind_of(klass, object, message="")
object.kind_of?(klass) が真なら pass
assert_respond_to(object, method, message="")
object.respond_to?(method) が真なら pass
assert_match(pattern, string, message="")
string =~ pattern が真ならば pass
assert_no_match(regexp, string, message="")
regexp !~ string が真ならば pass
assert_same(expected, actual, message="")
actual.equal?(expected) が真なら pass
assert_not_same(expected, actual, message="")
!actual.equal?(expected) が真なら pass
assert_operator(object1, operator, object2, message="")
object1.send(operator, object2) が真なら pass
assert_raises(expected_exception_klass, message="") { ... }
ブロックを実行して例外が発生し、その例外が expected_exception_klass クラスならば pass
assert_nothing_raised(*args) { ... }
ブロックを実行して例外が起きなければ pass
flunk(message="")
常に失敗
assert_throws(expected_symbol, message="") { ... }
ブロックを実行して :expected_symbol が throw されたら pass
assert_nothing_thrown(message="") { ... }
ブロックを実行して throw が起こらなければ pass
assert_in_delta(expected_float, actual_float, delta, message="")
(expected_float.to_f - actual_float.to_f).abs <= delta.to_f が真なら pass
delta は正の数でなければならない。
assert_send(send_array, message="")
send_array[0].__send__(send_array[1], *send_array[2..-1]) が真なら pass
assert_block(message="") { ... }
block の結果が真なら pass
assertion メソッドの違いは ruby-src:lib/runit/assert.rb を参照。 RUNIT::Assert も参照。