Professional Web Applications Themes

overriding setup in Test::Unit - Ruby

Hello, I have some unit tests that I'm having a little trouble with. This is basically the set-up: class TC_Foo < Test::Unit::TestCase def setup ... end def test_method1 ... end def test_method2 ... end end class TC_Bar < TC_Foo def setup ... end def test_method1 ... end # TC_foo#method2 will get called for end All the tests for TC_Foo work as expected. The problem comes with TC_Bar. I want TC_Bar#setup to be called prior to each of the unit tests in that class, but I can't seem to make that happen. Instead, TC_Foo#setup is called, which is not what I ...

  1. #1

    Default overriding setup in Test::Unit

    Hello,

    I have some unit tests that I'm having a little trouble with.

    This is basically the set-up:

    class TC_Foo < Test::Unit::TestCase
    def setup
    ...
    end

    def test_method1
    ...
    end

    def test_method2
    ...
    end
    end

    class TC_Bar < TC_Foo
    def setup
    ...
    end

    def test_method1
    ...
    end

    # TC_foo#method2 will get called for

    end


    All the tests for TC_Foo work as expected. The problem comes with
    TC_Bar. I want TC_Bar#setup to be called prior to each of the unit tests
    in that class, but I can't seem to make that happen. Instead,
    TC_Foo#setup is called, which is not what I want at all.

    I've tried explicitly undefining the setup method in TC_Bar with
    'undef_method :setup', but even that doesn't work.

    There must be a way to do this, right?

    Ian
    --
    Ian Macdonald | The clothes have no emperor. -- C.A.R.
    System Administrator | Hoare, commenting on ADA.
    org |
    http://www.caliban.org |
    |


    Ian Guest

  2. #2

    Default Re: overriding setup in Test::Unit

    On Wed 04 Feb 2004 at 17:11:26 +0900, Ian Macdonald wrote:
     

    In the example above, I have renamed the classes from their true names.
    Interestingly, I now note that I get the correct overriding behaviour if
    the subclass of TC_Foo has a name that alphabetically sorts earlier.

    In other words, the code as shown above actually works, because TC_Bar
    (the subclass) sorts higher than TC_Foo (the superclass). If I change
    the name of the TC_Bar class to TC_Goo, however, TC_Foo#setup is no
    longer overridden by the method of the same name in the subclass.

    This seems like an odd and arbitrary way to go about things. Is this the
    intention or some obscure side-effect of my trying to do something
    irrational?

    Ian
    --
    Ian Macdonald | War spares not the brave, but the cowardly.
    System Administrator | -- Anacreon
    org |
    http://www.caliban.org |
    |


    Ian Guest

  3. #3

    Default Re: overriding setup in Test::Unit

    Hi,

    At Wed, 4 Feb 2004 17:26:54 +0900,
    Ian Macdonald wrote in [ruby-talk:91524]: 

    Seems fine.

    $ cat test.rb
    class TC_Foo < Test::Unit::TestCase
    def setup
    puts "Foo#setup"
    end

    def test_method1
    assert_nothing_raised {puts "Foo#test_method1"}
    end

    def test_method2
    assert_nothing_raised {puts "Foo#test_method2"}
    end
    end

    class TC_Goo < TC_Foo
    def setup
    puts "Goo#setup"
    end

    def test_method1
    assert_nothing_raised {puts "Goo#test_method1"}
    end

    # TC_foo#method2 will get called for

    end
    $ testrb -vs test.rb
    Foo#setup
    Foo#test_method1
    Foo#setup
    Foo#test_method2
    Goo#setup
    Goo#test_method1
    Goo#setup
    Foo#test_method2

    --
    Nobu Nakada


    nobu.nokada@softhome.net Guest

  4. #4

    Default Re: overriding setup in Test::Unit

    On Wed 04 Feb 2004 at 18:21:33 +0900, net wrote:
     

    The real code is more complex, of course. A lot of files are being
    'require'd and there are modules being mixed in, too, so I think I'm
    experiencing some weird interaction there.

    Thanks for your help.

    Ian
    --
    Ian Macdonald | The only cultural advantage LA has over NY
    System Administrator | is that you can make a right turn on a red
    org | light. -- Woody Allen
    http://www.caliban.org |
    |


    Ian Guest

Similar Threads

  1. Test::Unit non-auto-run test case?
    By Sean O'Dell in forum Ruby
    Replies: 1
    Last Post: September 18th, 06:19 PM
  2. Test::Unit -- multiple errors in test method ???
    By Johan Holmberg in forum Ruby
    Replies: 14
    Last Post: September 15th, 03:33 PM
  3. Test::Unit in 1.8
    By Joel VanderWerf in forum Ruby
    Replies: 1
    Last Post: August 10th, 12:23 AM
  4. Method test::unit::TestSuite#<<(test)
    By Robert Klemme in forum Ruby
    Replies: 1
    Last Post: August 6th, 11:28 PM
  5. Test order in Test::Unit
    By Brian Candler in forum Ruby
    Replies: 0
    Last Post: July 5th, 08:58 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139