Enhancing/overriding Test::Unit::Assertion methods--what's the best way?

J

joe.yakich

Everyone,

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.

Joe
 
R

Ryan Leavengood

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.

Here is how you do that:

require 'test/unit'

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=3D=3D1)
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
 
B

Bret Pettichord

At said:
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.)

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.


_____________________
Bret Pettichord
www.pettichord.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top