From: "Shirai,Kaoru" Date: 2002-06-20T13:22:23+09:00 Subject: [ruby-list:35411] Re: RubyUnit: Extended assert module and stacktrace  白井です。 > 上は、RubyUnitの実装通りの動作だと思います。 > > 例外が起こったとき、スタックトレースは TestFailure.initialize()メソッド > で整形されます。この整形は、'#{$LOADPATH}/runit/' を含む行を削除しますか > ら、通常は assert文を呼び出した位置の情報が残ります。  なるほど、例外を発生した時ではなく捕捉した時にソースファイルによってスタ ックトレースを削っているわけなんですね。 > 実は、私も、元のassert文を module_evalで定義し直して、 > その内部で AssertionFailErrorを発生させるようなのを書いたのですが、 > トレースが`(eval)'という表示が残ってしまい、対応を思案中です。  同じアプローチですが、 caller を使った次のような方法ではいかがでしょうか 。 test.rb: require 'rubyunit' module MyAssert include RUNIT::Assert def self.assert_method(*name) module_eval <<-EOF alias __#{name} #{name} def #{name}(*args,&block) stacktrace = caller begin __#{name}(*args,&block) rescue RUNIT::AssertionFailedError $!.set_backtrace(stacktrace) raise end end EOF end def assert_my_fail(message) setup_assert raise_assertion_error message end assert_method :assert_my_fail end #/MyAssert class MyTest < RUNIT::TestCase include MyAssert def test_original_fail assert_fail "failed" end def test_my_fail assert_my_fail "failed" end end #/MyTest 実行結果: $ ruby test.rb MyTest#test_my_fail F. MyTest#test_original_fail F. Time: 0.016503 FAILURES!!! Test Results: Run: 2/2(2 asserts) Failures: 2 Errors: 0 Failures: 2 test.rb:35:in `test_my_fail'(MyTest): failed (RUNIT::AssertionFailedError) from test.rb:34 test.rb:31:in `test_original_fail'(MyTest): failed (RUNIT::AssertionFailedError) from test.rb:34 $ -- shirai@p1jp.com Shirai,Kaoru PlusOne Co.,Ltd