Test::Unit: assert in loop?

Hi !

Sometimes I find it convenient to have some of my unit tests
table-driven, rather than just straight lines of code. An example:

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 ?

If not, I think one way for Test::Unit to handle this better would be
to indicate which of all calls to ‘assert_equal’ on line 13 that
caused the error (the first call, the second, the third, …).
(some of the unit-test-modules for Perl does it that way)

I’m running (almost) the latest Test::Unit from Rubys CVS-archive.

/Johan Holmberg

In article vyu1amiai0.fsf@pjakkur.iar.se,

[…]

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… :slight_smile:

Hope this helps,

Mike

···

Johan Holmberg holmberg@iar.se wrote:


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

mike@ratdog.stok.co.uk (Mike Stok) writes:

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}" )

Thanks !!

I totally forgot that possibility.
I was too much in “Perl mode” and expected builtin support for this
in Test::Unit the same way as there …

(I still think it would be a good thing to add to Test::Unit)

/Johan Holmberg