Thanks, Bret, that is a very nice solution to the problem. Had I had this
one from the outset I wouldn't have worked on creating my own. But, since it
so happens that I just completed it, let me share it as
yet-another-multi-assert-solution:
I have taken the approach of overriding the central assert_block method from
assertions.rb. This way all I have to do is to include my
'multi_assertions.rb' file after including 'test/unit' to get the
functionality for all the different assert_ methods.
While I was at it, I also added a line that will invoke an 'on_failure'
method on the test case object, if it responds to it. I use this method to
write the HTML document to a file, and to return a string with the file name
and the IE URL at the time of failure, so this information can be included
in the failure message.
Enough words, here is the code:
- X - - - - - - - - - - - -
# File: multi_assertions.rb
# Note: this overrides a method in test/unit/assertions.rb
# to allow for multiple assertions in one test method to execute
# even if one or more fails.
module Test
module Unit
module Assertions
public
def assert_block(message="assert_block failed.") # :yields:
_wrap_assertion do
if (! yield)
message = message.to_s + on_failure if respond_to?(:on_failure)
# drop the top two caller lines pointing to this file
add_failure(message, caller.slice(2..caller.length))
end # if
end # do
end # def
end # module
end # module
end # module
- X - - - - - - - - - - - -
And a small usage example:
- X - - - - - - - - - - - -
require 'test/unit'
require 'multi_assertions'
class MyTestCase < Test::Unit::TestCase
puts "Should produce: 1 test, 4 assertions, 2 failures, 0 errors"
def on_failure
return "\nSome message....\n"
end # on_failure
def test_multi_assertions
assert(true, 'Assertion 1 is true.')
assert(false, 'Assertion 2 is false.')
assert(true, 'Assertion 3 is true.')
assert(false, 'Assertion 4 is false.')
end
end # end MyTestCase
- X - - - - - - - - - - - -
In the hope that someone could find this yet-another-multi-assert-solution
of interest,
~~ Egil
-----Original Message-----
> Date: Wed, 23 Aug 2006 15:21:25 -0500
> From: Bret Pettichord <[EMAIL PROTECTED]>
> A frequently asked question has been how do you cause a test
> verification to fail the test without immediately aborting it as well.
> Frequent replies have been to wrap assertions with rescue blocks, which
> raises all kinds of other problems. Here is a much better solution.
>
> def verify boolean, message = 'verify failed.'
> add_assertion
> add_failure message, caller unless boolean
> end
>
> Define this method in your test case class, and then use it instead of
> assert.
>
> Bret
_______________________________________________
Wtr-general mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wtr-general