Ruby 1.8.7 リファレンスマニュアル > ライブラリ一覧 > test/unitライブラリ
ユニットテストを行うためのライブラリです。
Test::Unit は以下のように使います。
まずテスト対象のソース(foo.rb)が必要です。
class Foo
def foo
"foo"
end
def bar
"foo"
end
end
次にユニットテスト(test_foo.rb)を書きます。テストを実行するメソッド(テストメソッド)の名前は 全て test_ で始まる必要があります。テストメソッドが実行される前には setup メソッドが必ず 呼ばれます。実行されたあとには、teardown メソッドが必ず呼ばれます。
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
テストを実行するには上で用意した test_foo.rb を実行します。 デフォルトではすべてのテストが実行されます。
$ ruby test_foo.rb Loaded suite test_foo Started F. Finished in 0.022223 seconds. 1) Failure!!! test_bar(TC_Foo) [test_foo.rb:16]: <bar> expected but was <foo> 2 tests, 2 assertions, 1 failures, 0 errors
test_bar だけテストしたい場合は以下のようなオプションを与えます。
$ ruby test_foo.rb --name=test_bar Loaded suite test_foo Started F Finished in 0.019573 seconds. 1) Failure!!! test_bar(TC_Foo) [test_foo.rb:16]: <bar> expected but was <foo> 1 tests, 1 assertions, 1 failures, 0 errors
gtk を使った testrunner
$ ruby test_foo.rb --runner=gtk --name=test_bar
fox を使う
$ ruby test_foo.rb --runner=fox --name=test_bar
console を使う (default)
$ ruby test_foo.rb --runner=console --name=test_bar
以下のようにすると help も表示されます。
$ ruby test_foo.rb --help
Usage: test_foo.rb [options] [-- untouched arguments]
-r, --runner=RUNNER Use the given RUNNER.
(c[onsole], f[ox], g[tk], g[tk]2, t[k])
-b, --basedir=DIR Base directory of test suites.
-w, --workdir=DIR Working directory to run tests.
-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 'test_foo.rb' require 'test_bar.rb'
もう少し高度なテストの実行方法に関しては Test::Unit::AutoRunner を 参照して下さい。
上の例では、テストクラスを「定義しただけ」で、テストが実行されています。 これは、Kernel.#at_exit と ObjectSpace.#each_object を使って 実装されています。つまり、上の例ではテストは終了時の後処理として実行されます。
大抵の場合は、これで問題ありません。が、そうでない場合は、 testrb コマンドや Test::Unit::AutoRunner 、各種 TestRunner クラスを使うことにより、 明示的にテストを実行することができます。
テストメソッド実行中に例外が発生した。
アサーションに失敗した。
| Test::Unit::AutoRunner | テストの実行を操作したいときにこの AutoRunner クラスを使います。 大量のテストの中から特定のテストスクリプトのみを実行したい場合、 特定のテストクラスのみを実行したい場合などに使います。 AutoRunner は Collector::Dir オブジェクトなどの Collector に テストを集めさせて、UI::Console::TestRunner オブジェクトなどの Runner にテストを実行させているクラスです。 |
| Test::Unit::Error | テスト時のエラーを表現するクラスです。テスト中に例外が発生した時に Test::Unit::TestCase から作られます。 |
| Test::Unit::Failure | テストの失敗を表現するクラスです。テスト中にアサーションが失敗した時に Test::Unit::TestCaseから作られます。 |
| Test::Unit::TestCase | テストの基本単位(あるいは「テスト本体」)を表すクラスです。 テストを行うメソッド(テストメソッド)は TestCase のサブクラスのインスタンスメソッド として定義されます。テストメソッドの名前は「test」で始まっていなければなりません。 逆に、「test」で始まっているメソッドは全てテストメソッドと見なされます。 各テストメソッドは、Test::Unit::TestCase.suite により Test::Unit::TestSuite オブジェクトへとひとつにまとめられます。 |
| Test::Unit::TestSuite | 複数のテストをひとつにまとめたクラスです。TestSuite 同士をまとめてひとつの TestSuite にすることもできます。 Test::Unit::TestSuite#run によりまとめたテストを一度に 実行することができます。テストは TestSuite へ加えられた順に実行されます。 自身が TestSuite を含んでいる場合は、再帰的にテストを実行します。 |
| Test::Unit | ユニットテストを行うためのモジュールです。 |
| Test::Unit::Assertions | 各種の assert メソッドを提供するモジュールです。 |
| Test::Unit::UI | 各種 TestRunner を実装するためのモジュールです。 |
| Test::Unit::UI::TestRunnerUtilities | 各種 TestRunner を実装するためのモジュールです。 |
| Test::Unit::AssertionFailedError | アサーションに失敗した時に Test::Unit::Assertions から raise されます。 |
| test/unit | ユニットテストを行うためのライブラリです。 |
| test/unit/assertions | 各種の assert メソッドを提供します。 |
| test/unit/autorunner | ユニットテストの実行を操作したいときに使います。 |
| test/unit/testcase | テストケースを記述するときに使います。 |
| test/unit/testsuite | 複数のテストをひとつにまとめるためのライブラリです。 |
| test/unit/ui/testrunnerutilities | 各種 TestRunner を実装するために使われます。 |
| test/unit/testresult | テストの結果を処理するときに使われます。 |
| test/unit/ui/console/testrunner | コンソール上でユニットテストを実行し、結果を表示するための ライブラリです。 |
| test/unit/ui/fox/testrunner | FOX Toolkit を用いた UI 上でユニットテストを実行し、結果を表示するため のライブラリです。 |
| test/unit/ui/gtk/testrunner | GTK を用いた UI 上でユニットテストを実行し、結果を表示するためのライブ ラリです。 |
| test/unit/ui/gtk2/testrunner | GTK+ を用いた UI 上でユニットテストを実行し、結果を表示するためのライブ ラリです。 |
| test/unit/ui/testrunnermediator | 各種 TestRunner を実装するために使われます。 |
| test/unit/ui/tk/testrunner | Tk を用いた UI 上でユニットテストを実行し、結果を表示するためのライブラ リです。 |
| test/unit/util/observable | イベントに対して通知を行うメソッドを提供するためのライブラリです。 |