From: mike@... (Mike Stok) Date: 2003-06-19T20:42:37+09:00 Subject: Re: Test::Unit: assert in loop ? In article , Johan Holmberg wrote: [...] > require 'test/unit' > > class TC_basic < Test::Unit::TestCase > @@table = [ > [1, 6, 7], > [8, 3, 11], # is ok > [100, 20, 120], > [-10, 20, 11], # will cause error > ] > > def test_plus > for a, b, sum in @@table > assert_equal( sum, a+b ) > end > end > end > > >The problem I'm having is that when an error occur, it is hard to tell >which iteration that caused the error. The example above gives the error: > > Loaded suite TC_table_driven > Started > F > Finished in 0.0082 seconds. > > 1) Failure!!! > test_plus(TC_basic) [TC_table_driven.rb:13]: > <11> expected but was > <10> > > 1 tests, 4 assertions, 1 failures, 0 errors > >It is impossible for me to tell which entry in @@table that caused the >error. I don't know if there is some other way I could have written my >tests to see this ? You can add a message to the assertiom e.g. assert_equal( sum, a+b, "Testing #{a} + #{b} = #{sum}" ) and you get 1) Failure!!! test_plus(TC_basic) [trst.rb:13]: Testing -10 + 20 = 11. <11> expected but was <10> or something like this def test_plus @@table.each_with_index do |val, index| a, b, sum = val assert_equal( sum, a+b, "Testing #{a} + #{b} = #{sum} (row #{index})" ) end end might help (if you count rows from 0... :-) Hope this helps, Mike -- mike@stok.co.uk | The "`Stok' disclaimers" apply. http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60 http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA