test/unit setup method

Discussion in 'Ruby' started by Phil Tomson, Jun 2, 2006.

  1. Phil Tomson

    Phil Tomson Guest

    Is there a way to initialize a unit test once and only once? I know that
    you can define a #setup method, but it gets run for every test_* method in
    the class.

    I know I could do:

    class TestStuff < Test::Unit::TestCase

    def setup
    unless @setup
    @setup = true
    #do stuff here only once
    end
    #do stuff out here for every test_* method
    end

    def test_foo
    #setup will be called first
    #test stuff
    end

    def test_foo_two
    #setup will be called first
    #test stuff
    end
    end


    ....but I'm wondering if there's a built-in way of doing this sort of
    initialization already?

    Phil
    Phil Tomson, Jun 2, 2006
    #1
    1. Advertising

  2. Phil Tomson wrote:
    > Is there a way to initialize a unit test once and only once? I know that
    > you can define a #setup method, but it gets run for every test_* method in
    > the class.
    >
    > I know I could do:
    >
    > class TestStuff < Test::Unit::TestCase
    >
    > def setup
    > unless @setup
    > @setup = true
    > #do stuff here only once
    > end
    > #do stuff out here for every test_* method
    > end
    >
    > def test_foo
    > #setup will be called first
    > #test stuff
    > end
    >
    > def test_foo_two
    > #setup will be called first
    > #test stuff
    > end
    > end
    >
    >
    > ...but I'm wondering if there's a built-in way of doing this sort of
    > initialization already?
    >
    > Phil


    Why would you want to initialize only once? from a testing point of
    view, you want each test case (method) to be independently runnable.
    Creating dependencies between them makes them very fragile to changes.
    Andrew McDonagh, Jun 2, 2006
    #2
    1. Advertising

  3. Phil Tomson

    Phil Tomson Guest

    In article <e5qa5t$qem$2surf.net>,
    Andrew McDonagh <> wrote:
    >Phil Tomson wrote:
    >> Is there a way to initialize a unit test once and only once? I know that
    >> you can define a #setup method, but it gets run for every test_* method in
    >> the class.
    >>
    >> I know I could do:
    >>
    >> class TestStuff < Test::Unit::TestCase
    >>
    >> def setup
    >> unless @setup
    >> @setup = true
    >> #do stuff here only once
    >> end
    >> #do stuff out here for every test_* method
    >> end
    >>
    >> def test_foo
    >> #setup will be called first
    >> #test stuff
    >> end
    >>
    >> def test_foo_two
    >> #setup will be called first
    >> #test stuff
    >> end
    >> end
    >>
    >>
    >> ...but I'm wondering if there's a built-in way of doing this sort of
    >> initialization already?
    >>
    >> Phil

    >
    >Why would you want to initialize only once? from a testing point of
    >view, you want each test case (method) to be independently runnable.
    >Creating dependencies between them makes them very fragile to changes.
    >


    I need to setup an xmlrpc server and it would be best (I think) to only
    set it up once.

    Phil
    Phil Tomson, Jun 5, 2006
    #3
  4. Phil Tomson wrote:
    > In article <e5qa5t$qem$2surf.net>,
    > Andrew McDonagh <> wrote:
    >> Phil Tomson wrote:
    >>> Is there a way to initialize a unit test once and only once? I know that
    >>> you can define a #setup method, but it gets run for every test_* method in
    >>> the class.
    >>>
    >>> I know I could do:
    >>>
    >>> class TestStuff < Test::Unit::TestCase
    >>>
    >>> def setup
    >>> unless @setup
    >>> @setup = true
    >>> #do stuff here only once
    >>> end
    >>> #do stuff out here for every test_* method
    >>> end
    >>>
    >>> def test_foo
    >>> #setup will be called first
    >>> #test stuff
    >>> end
    >>>
    >>> def test_foo_two
    >>> #setup will be called first
    >>> #test stuff
    >>> end
    >>> end
    >>>
    >>>
    >>> ...but I'm wondering if there's a built-in way of doing this sort of
    >>> initialization already?
    >>>
    >>> Phil

    >> Why would you want to initialize only once? from a testing point of
    >> view, you want each test case (method) to be independently runnable.
    >> Creating dependencies between them makes them very fragile to changes.
    >>

    >
    > I need to setup an xmlrpc server and it would be best (I think) to only
    > set it up once.
    >
    > Phil



    Do you need to have a server - or do you need to test your codes
    requests and responses to 'a' server?

    In other words, fake (or mock) the server for each test, rather than
    have a real server.
    Andrew McDonagh, Jun 5, 2006
    #4
    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. Edvard Majakari
    Replies:
    4
    Views:
    674
    Edvard Majakari
    Feb 25, 2005
  2. VvanN
    Replies:
    5
    Views:
    479
    Phlip
    Apr 28, 2006
  3. Robert Klemme

    Method test::unit::TestSuite#<<(test)

    Robert Klemme, Aug 6, 2003, in forum: Ruby
    Replies:
    0
    Views:
    97
    Robert Klemme
    Aug 6, 2003
  4. Johan Holmberg
    Replies:
    7
    Views:
    158
    Johan Holmberg
    Sep 15, 2003
  5. timr
    Replies:
    2
    Views:
    154
Loading...

Share This Page