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

Discussion in 'Ruby' started by joe.yakich@gmail.com, Oct 7, 2005.

  1. Guest

    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
    , Oct 7, 2005
    #1
    1. Advertising

  2. On 10/6/05, <> 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.


    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
    Ryan Leavengood, Oct 7, 2005
    #2
    1. Advertising

  3. Re: Enhancing/overriding Test::Unit::Assertion methods--what'sthe best way?

    At 08:16 PM 10/6/2005, 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.)


    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
    Bret Pettichord, Oct 7, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. aidy
    Replies:
    2
    Views:
    136
    Tim Pease
    Apr 16, 2007
  2. Curt Sampson
    Replies:
    3
    Views:
    110
    Phlip
    Aug 11, 2007
  3. Replies:
    1
    Views:
    100
  4. John Smith

    Test::unit assertion pass scenario

    John Smith, Dec 30, 2009, in forum: Ruby
    Replies:
    12
    Views:
    252
    Phillip Gawlowski
    Dec 31, 2009
  5. timr
    Replies:
    2
    Views:
    154
Loading...

Share This Page