From: Pit Capitain Date: 2005-04-15T17:43:58+09:00 Subject: Re: Multiple asserts in a single unit test Daniel Berger schrieb: > I came across an article regarding unit testing philosophy that I > thought was interesting: > > http://weblogs.asp.net/rosherove/archive/2005/04/14/AvoidMultipleAsserts.aspx > > This sparked a conversation on IRC regarding coming up with a way to > have asserts continue even if one of them fails within a unit test. Is > there a way to alter the behavior of test-unit so that it doesn't bail > out on subsequent asserts if one fails? Something builtin to test-unit > or a simple hack? It may not be simple, but at least it's a hack: require 'test/unit' module ErrorCollector def collecting_errors is_collecting = @is_collecting @is_collecting = true yield ensure @is_collecting = is_collecting end def raise( * ) super rescue Test::Unit::AssertionFailedError handle_error( :add_failure, $! ) rescue StandardError, ScriptError handle_error( :add_error, $! ) end def handle_error( method, error ) bck = error.backtrace bck.shift if @is_collecting bck.slice!( 5, 2 ) send( method, error.message, bck ) else Kernel.raise( error, error.message, error.backtrace ) end end end class SampleTest < Test::Unit::TestCase include ErrorCollector def test_multiple_errors collecting_errors do assert_equal( 1, 2 ) assert_equal( 2, 3 ) end assert_equal( 3, 4 ) assert_equal( 4, 5 ) end end This is the output: Loaded suite C:/tmp/r Started FFF Finished in 0.015 seconds. 1) Failure: test_multiple_errors(SampleTest) [C:/tmp/r.rb:42]: <1> expected but was <2>. 2) Failure: test_multiple_errors(SampleTest) [C:/tmp/r.rb:43]: <2> expected but was <3>. 3) Failure: test_multiple_errors(SampleTest) [C:/tmp/r.rb:45]: <3> expected but was <4>. 1 tests, 3 assertions, 3 failures, 0 errors Regards, Pit