トップ > 技術メモ (ソフトウェア開発) > Rubyスクリプトのテスト技法 >

リファレンス Test::Unit

(2006.11.11更新。RubyUnit (RUNIT) からTest::Unitへ変更。)

リファレンスマニュアルの記述で不十分なところがあるので、作ってみた。

モジュール Test::Unit::Assertions

各種アサーションのメソッドを定めるモジュール。

定数

UncaughtThrow

シングルトンメソッド

use_pp = boolean_value
pretty-printerを使うかどうか設定する。

インスタンスメソッド

下記のメソッドは、assert_*() の「*」がパスしたときは何も起こらない。テストに失敗したときはAssertionFailedErrorが発生する。

assert(boolean, message = nil)
ブロックを渡すとエラーになる。booleanが真ならpass.
assert_block(message = "assert_block failed.") {...}
blockの結果が真ならpass
assert_equal(expected, actual, message=nil)
expected == actualが真ならpass。expectedの==メソッドが使われる。
assert_in_delta(expected_float, actual_float, delta, message="")
expected_float, actual_float, deltaは、実数(かto_fメソッドを持つ)でなければならない。deltaは非負でなければならない。(expected_float.to_f - actual_float.to_f).abs <= delta.to_f ならpass.
assert_instance_of(klass, object, message = "")
klassがクラスではないときはエラー。object.instance_of?(klass) が真ならpass。
assert_kind_of(klass, object, message="")
klassがモジュール(またはクラス)ではないときはエラー。object.kind_of?(klass) が真ならpass。
assert_match(pattern, string, message="")
string =~ pattern が真ならpass。patternが文字列の場合は、内部でRegexpオブジェクトに変換される。
assert_nil(object, message="")
nil == object が真ならpass
assert_no_match(regexp, string, message = "")
regexpは正規表現オブジェクトでなければならない。regexp !~ string が真ならpass.
assert_not_equal(expected, actual, message="")
expected != actualが真ならpass
assert_not_nil(object, message="")
!object.nil? が真ならpass
assert_not_same(expected, actual, message="")
オブジェクトが同一ではないか。!actual.equal?(expected) が真のときにパス。
assert_nothing_raised(exception [, ...] [, message]) {...}
ブロックを実行し、例外が発生しなければpass. exception, ... の例外が発生すればテストに失敗、それ以外の例外が発生すればその例外があがる。
assert_nothing_thrown(message="") {...}
ブロックを実行し、何もthrowされなければpass.
assert_operator(object1, operator, object2, message="")
operatorはシンボルか文字列(to_strメソッドを持つ)でなければならない。object1.__send__(operator, object2) が真ならpass.
assert_raise(exception [, ...] [, message]) {...}
ブロックを実行して、exception, ... のいずれかの例外が発生したらpass
assert_raises(exception [, ...] [, message]) {...}
deprecated (非推奨). assert_raise() を使用してください。
assert_respond_to(object, method, message="")
methodはシンボルまたは文字列(to_strメソッドを持つオブジェクト)でなければならない。object.respond_to?(method)が真であればpass.
assert_same(expected, actual, message="")
オブジェクトの同一性を確認する。actual.equal?(expected) が真ならpass
assert_send(send_array, message="")
send_arrayは配列でなければならない。その値は、レシーバ, メソッド名 [, 引数...] となる。send_array[0].send_array[1](send_array[2], ...) が真ならpass.
assert_throws(expected_symbol, message="") {...}
expected_symbolはシンボルでなければならない。expected_symbol がブロック内でthrowされればpass.
build_message(head, template=nil, ...)
テストに失敗したときのメッセージを生成する。
flunk(message = "Flunked")
常に失敗する。

クラス Test::Unit::TestCase

テストケースを記述するためのスーパークラス。サブクラスにてtest*() メソッドを追加し、setup, teardown は(必要があれば)オーバーライドする。Rubyのリフレクション機能によって、test*() メソッドが順に実行される。

TestCase派生クラスを定義するファイルを実行すると、そのテストだけ行える。--helpでコマンドラインオプションが表示される。

$ ruby tc.rb --help
Test::Unit automatic runner.
Usage: tc.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.

Deprecated options:
        --console                    Console runner (use --runner).
        --gtk                        GTK runner (use --runner).
        --fox                        Fox runner (use --runner).

Superclass

Object

Included Modules

Test::Unit::Assertions Test::Unit::Util::BacktraceFilter

定数

FINISHED STARTED

シングルトンメソッド

suite

インスタンスメソッド

==
default_test
method_name
name
run
setup
サブクラスでオーバーライドする。各テストメソッドが呼び出される前に呼び出される。
size
teardown
サブクラスでオーバーライドする。各テストメソッドが戻ったあとに(例外が発生した場合でも)必ず呼び出される。
to_s

トップ > 技術メモ (ソフトウェア開発) > Rubyスクリプトのテスト技法 > リファレンス Test::Unit
このページについてのご感想・ご提案などをお寄せください。なお、コメントに「http:」、HTML aタグが含まれると送信されません。
評価: ◎ ← → ×
コメント:
お名前:
メールアドレス:
[Profile]  [Privacy Policy]  [Legal & Link]  [Site Map]  

banner Netsphere Laboratories http://www.nslabs.jp/

Copyright (c) HORIKAWA Hisashi. All rights reserved.

[PR]

はてなブックマークに追加  

サイト内検索:

[PR]