I have a little screen capture routine that I'd like to use
automatically if possible when I use any of the Test::Unit assert
flavors, such as assert, assert_equals, assert_match, etc. (whether or
not the assertion returns true or false).
Is there a spiffy way to override or enhance/extend Test::Unit to do
that? (I'm new to Ruby, and haven't grasped what it means to extend it;
I see in the PickAxe book there's mention of using C, but I think
there's another way to accomplish what I'm describing.)
Ideally, I'd be able to do this:
assert(ie.contains_text('some_text')
or this
assert_match(/'s Web Page/, ie.title)
and assert/assert_match would have been overridden (that's probably a
poor choice of words, but it indicates what I'm naively hoping to
accomplish) so that it automatically did something like this:
def assert_with_screen_capture(params)
capture_screen
call_whatever_flavor_of_assert_I_wanted(params)
end
I was hoping to have a drop-in replacement so that existing code would
only have to have another require/include line rather than changing
each assert.* call to some custom one.
Thanks in advance, hope I'm not overlooking something I should have
seen.
module MyAssertions
def assert(bool)
puts "My custom assert"
super
end
def assert_equal(obj1, obj2)
puts "My custom assert_equal"
super
end
end
class MyTest < Test::Unit::TestCase
include MyAssertions
def test_something
assert(1==1)
assert_equal(1,1)
end
end
__END__
The Test::Unit::TestCase class includes the Test::Unit::Assertions
module which has all the assertion methods. As I've shown above you
can create a module with your own assertions that do what you want,
and then when those assertions call super they will use the assertions
in the Test::Unit::Assertions module. As you can see once you've
created your custom module, your test classes only need to include
that module. To make things even easier, I'd recommend creating your
own subclass of Test::Unit::TestCase that includes your assertion
module, then subclass all yours tests from that class.
Ryan
···
On 10/6/05, joe.yakich@gmail.com <joe.yakich@gmail.com> wrote:
I was hoping to have a drop-in replacement so that existing code would
only have to have another require/include line rather than changing
each assert.* call to some custom one.
Assertions is a module that is included in (mixed in) TestCase. You should create a new mixin.
module AssertWithScreenCapture
def assert(*args)
capture_screen
Test::Unit::Assertions::assert(*args)
end
end
Then use "include AssertWithScreenCapture" in the test cases you want the 'overridded' behavior.
···
At 08:16 PM 10/6/2005, joe.yakich@gmail.com wrote:
I have a little screen capture routine that I'd like to use
automatically if possible when I use any of the Test::Unit assert
flavors, such as assert, assert_equals, assert_match, etc. (whether or
not the assertion returns true or false).
Is there a spiffy way to override or enhance/extend Test::Unit to do
that? (I'm new to Ruby, and haven't grasped what it means to extend it;
I see in the PickAxe book there's mention of using C, but I think
there's another way to accomplish what I'm describing.)